Skip to content

Commit

Permalink
update some bits of the C++ binding
Browse files Browse the repository at this point in the history
  • Loading branch information
kodebach committed Oct 2, 2020
1 parent 6edcb87 commit 76c5529
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/bindings/cpp/examples/cpp_example_hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace kdb
* @param k the key to search in
* @param n the level
*
* nth_level_of_name(Key("user:/hello", KEY_END), 0) -> "user:"// TODO (kodebach): ?????
* nth_level_of_name(Key("user:/hello", KEY_END), 0) -> "user:"
* nth_level_of_name(Key("user:/hello", KEY_END), 1) -> "hello"
*
* @return the searched string (without slashes)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/cpp/examples/cpp_example_userio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error,

try
{
// TODO (kodebach): use C++ binding version of keyMeta
// TODO: use C++ binding version of keyMeta
kdb::KeySet meta (ckdb::ksDup (ckdb::keyMeta (error.getKey ())));
kdb::Key parent ("meta:/warnings", KEY_END);
auto warnings = meta.cut (parent);
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/cpp/include/kdbio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error,
{
try
{
// TODO (kodebach): use C++ binding version of keyMeta
// TODO: use C++ binding version of keyMeta
KeySet meta (ckdb::ksDup (ckdb::keyMeta (error.getKey ())));
Key parent ("meta:/warnings", KEY_END);
auto warnings = meta.cut (parent);
Expand Down
9 changes: 2 additions & 7 deletions src/bindings/cpp/include/kdbvalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,7 @@ class DefaultSetPolicy
static Key setWithNamespace (KeySet & ks, Key const & spec, std::string const & ns)
{
std::string const & name = spec.getName ();
size_t offset = spec.getNamespace ().length ();
if (offset != 1)
{
++offset;
}
kdb::Key k (ns + "/" + name.substr (offset), KEY_END);
kdb::Key k (ns + "/" + name.substr (name.find ('/')), KEY_END);
ks.append (k);

return k;
Expand Down Expand Up @@ -694,7 +689,7 @@ class Value : public ValueObserver, public ValueSubject, public Wrapped
*/
void unsafeSyncKeySet () const
{
if (m_hasChanged && m_key.getNamespace () == "default")
if (m_hasChanged && m_key.getNamespace () == ElektraNamespace::DEFAULT)
{
m_hasChanged = false;
Key spec (m_spec.dup ());
Expand Down
39 changes: 28 additions & 11 deletions src/bindings/cpp/include/key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ class NameIterator;
class NameReverseIterator;
#endif

enum class ElektraNamespace : std::uint8_t
{
NONE = 0,
CASCADING = 1,
META = 2,
SPEC = 3,
PROC = 4,
DIR = 5,
USER = 6,
SYSTEM = 7,
DEFAULT = 8,
};

/**
* @copydoc key
*
Expand Down Expand Up @@ -213,7 +226,8 @@ class Key


inline bool isValid () const;
inline std::string getNamespace () const;
inline ElektraNamespace getNamespace () const;
inline ssize_t setNamespace (ElektraNamespace ns) const;
inline bool isCascading () const;
inline bool isSpec () const;
inline bool isProc () const;
Expand All @@ -240,7 +254,6 @@ class Key
ckdb::Key * key; ///< holds an elektra key
};


#ifndef ELEKTRA_WITHOUT_ITERATOR
/**
* For C++ forward Iteration over Names.
Expand Down Expand Up @@ -1454,19 +1467,23 @@ inline bool Key::isValid () const
}

/**
* @return namespace as string
* @return namespace of the key
*
* Will return slash for cascading names.
* @see ElektraNamespace, keyGetNamespace
*/
inline ElektraNamespace Key::getNamespace () const
{
return static_cast<ElektraNamespace> (ckdb::keyGetNamespace (key));
}

/**
* Set the namespace of the key
*
* @see getName(), isUser(), isSystem()
* @see ElektraNamespace, keySetNamespace
*/
inline std::string Key::getNamespace () const
inline ssize_t Key::setNamespace (ElektraNamespace ns) const
{
// TODO (kodebach): enum?
std::string name = getName ();
size_t colon = name.find (':');
if (colon == std::string::npos) return "/";
return name.substr (0, colon);
return ckdb::keySetNamespace (key, static_cast<elektraNamespace> (ns));
}


Expand Down
36 changes: 18 additions & 18 deletions src/bindings/cpp/tests/testcpp_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,25 +647,25 @@ TEST (key, conversation)

TEST (key, keynamespace)
{
succeed_if (Key ("user:/", KEY_END).getNamespace () == "user", "namespace wrong");
succeed_if (Key ("user:/a", KEY_END).getNamespace () == "user", "namespace wrong");
succeed_if (Key ("user:/", KEY_END).getNamespace () == ElektraNamespace::USER, "namespace wrong");
succeed_if (Key ("user:/a", KEY_END).getNamespace () == ElektraNamespace::USER, "namespace wrong");
// std::cout << Key ("user:/a", KEY_END).getNamespace () << std::endl;
succeed_if (Key ("user:/a/b/c", KEY_END).getNamespace () == "user", "namespace wrong");
succeed_if (Key ("user:/a/..", KEY_END).getNamespace () == "user", "namespace wrong");
succeed_if (Key ("user:/a/../x/f/v", KEY_END).getNamespace () == "user", "namespace wrong");

succeed_if (Key ("dir:/", KEY_END).getNamespace () == "dir", "namespace wrong");
succeed_if (Key ("proc:/", KEY_END).getNamespace () == "proc", "namespace wrong");
succeed_if (Key ("spec:/", KEY_END).getNamespace () == "spec", "namespace wrong");
succeed_if (Key ("system:/", KEY_END).getNamespace () == "system", "namespace wrong");

succeed_if (Key ("dir:/abc", KEY_END).getNamespace () == "dir", "namespace wrong");
succeed_if (Key ("proc:/abc", KEY_END).getNamespace () == "proc", "namespace wrong");
succeed_if (Key ("spec:/abc", KEY_END).getNamespace () == "spec", "namespace wrong");
succeed_if (Key ("system:/abc", KEY_END).getNamespace () == "system", "namespace wrong");

succeed_if (Key ("/", KEY_END).getNamespace () == "/", "namespace wrong");
succeed_if (Key ("/abc", KEY_END).getNamespace () == "/", "namespace wrong");
succeed_if (Key ("user:/a/b/c", KEY_END).getNamespace () == ElektraNamespace::USER, "namespace wrong");
succeed_if (Key ("user:/a/..", KEY_END).getNamespace () == ElektraNamespace::USER, "namespace wrong");
succeed_if (Key ("user:/a/../x/f/v", KEY_END).getNamespace () == ElektraNamespace::USER, "namespace wrong");

succeed_if (Key ("dir:/", KEY_END).getNamespace () == ElektraNamespace::DIR, "namespace wrong");
succeed_if (Key ("proc:/", KEY_END).getNamespace () == ElektraNamespace::PROC, "namespace wrong");
succeed_if (Key ("spec:/", KEY_END).getNamespace () == ElektraNamespace::SPEC, "namespace wrong");
succeed_if (Key ("system:/", KEY_END).getNamespace () == ElektraNamespace::SYSTEM, "namespace wrong");

succeed_if (Key ("dir:/abc", KEY_END).getNamespace () == ElektraNamespace::DIR, "namespace wrong");
succeed_if (Key ("proc:/abc", KEY_END).getNamespace () == ElektraNamespace::PROC, "namespace wrong");
succeed_if (Key ("spec:/abc", KEY_END).getNamespace () == ElektraNamespace::SPEC, "namespace wrong");
succeed_if (Key ("system:/abc", KEY_END).getNamespace () == ElektraNamespace::SYSTEM, "namespace wrong");

succeed_if (Key ("/", KEY_END).getNamespace () == ElektraNamespace::CASCADING, "namespace wrong");
succeed_if (Key ("/abc", KEY_END).getNamespace () == ElektraNamespace::CASCADING, "namespace wrong");
}

TEST (key, comparision)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/swig/ruby/tests/testruby_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_key_get_name
k.name = name
assert_equal name, k.name
assert_equal name.split('/').reverse[0], k.basename
assert_equal "user", k.namespace
assert_equal Kdb::ElektraNamespace_USER, k.namespace

k.add_basename "b1"
assert_equal "b1", k.basename
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/swig/ruby/tests/testruby_keyset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def test_keySet_each_enumeralbe


# test Enumerable mixin
assert ks.all? { |e| e.namespace == "user" }
assert ks.all? { |e| e.namespace == Kdb::ElektraNamespace_USER }
assert ks.all? { |e| e.has_meta? "owner" }
assert ks.all? { |e| e.is_string? }

Expand Down
1 change: 1 addition & 0 deletions src/include/kdb.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ ssize_t keySetBaseName(Key *key,const char *baseName);
ssize_t keyAddBaseName(Key *key,const char *baseName);

elektraNamespace keyGetNamespace(Key const* key);
ssize_t keySetNamespace(Key * key, elektraNamespace ns);

/* Value Manipulation Methods */
const void *keyValue(const Key *key);
Expand Down
2 changes: 0 additions & 2 deletions src/include/kdbprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,6 @@ int keyIsDir (const Key * key);
int keyIsSystem (const Key * key);
int keyIsUser (const Key * key);

ssize_t keySetNamespace (Key * key, elektraNamespace ns);

elektraNamespace elektraReadNamespace (const char * namespaceStr, size_t len);

int elektraKeyNameValidate (const char * name, const char * prefix, size_t * size, size_t * usize);
Expand Down
6 changes: 3 additions & 3 deletions src/libs/elektra/keyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -1214,8 +1214,8 @@ ssize_t keyGetBaseName (const Key * key, char * returned, size_t maxSize)
return -1;
}

size_t baseSize = strlen (baseName);
if (baseSize >= maxSize)
size_t baseSize = strlen (baseName) + 1;
if (baseSize > maxSize)
{
return -1;
}
Expand Down Expand Up @@ -1507,7 +1507,7 @@ ssize_t keyAddBaseName (Key * key, const char * baseName)
*/
ssize_t keyAddName (Key * key, const char * newName)
{
// TODO (kodebach): error codes
// TODO (kodebach): error codes documentation
if (!key) return -1;
if (test_bit (key->flags, KEY_FLAG_RO_NAME)) return -1;
if (!newName) return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/tools/src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void Backend::setMountpoint (Key mountpoint, KeySet mountConf)
}

// STEP 2: check for wrong namespace (proc)
if (mountpoint.getNamespace () == "proc")
if (mountpoint.getNamespace () == ElektraNamespace::PROC)
{
throw MountpointAlreadyInUseException ("proc:/ mountpoint not allowed");
}
Expand Down
22 changes: 8 additions & 14 deletions src/libs/tools/src/helper/keyhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ namespace helper
string rebasePath (const Key & key, const Key & oldParent, const Key & newParent)
{
string oldKeyPath = key.getName ();
string ns = key.getNamespace ();
if (ns != "/")
{
ns = ns + ":";
}

Key actualOldParent = oldParent.dup ();
if (oldParent.getNamespace () == "/")
if (oldParent.getNamespace () == ElektraNamespace::CASCADING)
{
actualOldParent.setName (ns + oldParent.getName ());
actualOldParent.setName (oldParent.getName ());
actualOldParent.setNamespace (key.getNamespace ());
}

Key actualNewParent = newParent.dup ();
if (newParent.getNamespace () == "/")
if (newParent.getNamespace () == ElektraNamespace::CASCADING)
{
actualNewParent.setName (ns + newParent.getName ());
actualNewParent.setName (newParent.getName ());
actualNewParent.setNamespace (key.getNamespace ());
}

if (!key.isBelowOrSame (actualOldParent))
Expand Down Expand Up @@ -135,12 +132,9 @@ Key commonKeyName (Key key1, Key key2)
}

auto ns = key1.getNamespace ();
if (ns != "/")
{
ns += ":/";
}

Key ret (ns, KEY_END);
Key ret ("/", KEY_END);
ret.setNamespace (ns);
for (auto it1 = ++key1.begin (), it2 = ++key2.begin (); it1 != key1.end () && it2 != key2.end (); ++it1, ++it2)
{
if (*it1 != *it2) break;
Expand Down
8 changes: 2 additions & 6 deletions src/libs/tools/src/plugindatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,8 @@ std::vector<PluginSpec> PluginVariantDatabase::getPluginVariantsFromGenconf (Plu
KeySet ksToIterate (genconf);
for (auto kCurrent : ksToIterate)
{
auto ns = kCurrent.getNamespace ();
if (ns != "/")
{
ns += ":";
}
Key kCurrentTest (ns + "/", KEY_END);
Key kCurrentTest ("/", KEY_END);
kCurrentTest.setNamespace (kCurrent.getNamespace ());
kCurrentTest.addBaseName (kCurrent.getBaseName ()); // e.g. system:/space
if (kCurrentTest == kCurrent)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libs/tools/src/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void serializeConfig (std::string name, KeySet const & ks, KeySet & ret)
for (KeySet::iterator i = ks.begin (); i != ks.end (); ++i)
{
Key k (i->dup ());
if (k.getNamespace () == "user") ret.append (kdb::tools::helper::rebaseKey (k, oldParent, newParent));
if (k.getNamespace () == ElektraNamespace::USER) ret.append (kdb::tools::helper::rebaseKey (k, oldParent, newParent));
}
}
} // namespace
Expand Down
2 changes: 1 addition & 1 deletion src/libs/tools/tests/testtool_mergecases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST_F (ThreeWayMergeTest, CascadingParentsCauseNoCascadingKeys)
merged.rewind ();
while ((current = merged.next ()))
{
EXPECT_FALSE (current.getNamespace () == "/");
EXPECT_FALSE (current.getNamespace () == ElektraNamespace::CASCADING);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ccode/coder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ CppKey Coder::encodeName (CppKey const & key)
{
CppKey escaped{ key.dup () };
auto nspace = key.getNamespace ();
escaped.setName (nspace == "/" ? "/" : nspace + ":/");
escaped.setNamespace (nspace);
auto keyIterator = key.begin ();

while (++keyIterator != key.end ())
Expand All @@ -200,7 +200,7 @@ CppKey Coder::decodeName (CppKey const & key)
{
CppKey unescaped{ key.dup () };
auto nspace = key.getNamespace ();
unescaped.setName (nspace == "/" ? "/" : nspace + ":/");
unescaped.setNamespace (nspace);
auto keyIterator = key.begin ();

while (++keyIterator != key.end ())
Expand Down
2 changes: 1 addition & 1 deletion src/tools/kdb/coloredkdbio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error,
{
try
{
// TODO (kodebach): use C++ binding version of keyMeta
// TODO: use C++ binding version of keyMeta
KeySet meta (ckdb::ksDup (ckdb::keyMeta (error.getKey ())));
Key parent ("meta:/warnings", KEY_END);
auto warnings = meta.cut (parent);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/kdb/get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int GetCommand::execute (Cmdline const & cl)
{
if (cl.verbose)
{
if (k.getNamespace () == "default")
if (k.getNamespace () == ElektraNamespace::DEFAULT)
{
cout << "The key was not found in any other namespace, taking the default" << std::endl;
}
Expand Down

0 comments on commit 76c5529

Please sign in to comment.