diff options
| author | Igor Pashev <pashev.igor@gmail.com> | 2012-04-25 14:20:49 +0400 |
|---|---|---|
| committer | Igor Pashev <pashev.igor@gmail.com> | 2012-04-25 14:20:49 +0400 |
| commit | 0a7d62bc391eed25a569e51248930969cdd0de4a (patch) | |
| tree | a7025f8b89473c6dbadd79c0d33b8d3d0d77bc50 | |
| parent | 15ef504ee2226a565f3f836ec33813b91031bed1 (diff) | |
| download | node-augeas-0a7d62bc391eed25a569e51248930969cdd0de4a.tar.gz | |
Added method match() returning an array of nodes matching given path expression
| -rw-r--r-- | libaugeas.cc | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/libaugeas.cc b/libaugeas.cc index 42b7579..792a80c 100644 --- a/libaugeas.cc +++ b/libaugeas.cc @@ -65,6 +65,7 @@ protected: static Handle<Value> mv (const Arguments& args); static Handle<Value> save (const Arguments& args); static Handle<Value> nmatch (const Arguments& args); + static Handle<Value> match (const Arguments& args); static Handle<Value> load (const Arguments& args); static Handle<Value> loadFile (const Arguments& args); static Handle<Value> saveFile (const Arguments& args); @@ -116,6 +117,7 @@ void LibAugeas::Init(Handle<Object> target) _NEW_METHOD(mv); _NEW_METHOD(save); _NEW_METHOD(nmatch); + _NEW_METHOD(match); _NEW_METHOD(load); _NEW_METHOD(loadFile); _NEW_METHOD(saveFile); @@ -477,6 +479,41 @@ Handle<Value> LibAugeas::nmatch(const Arguments& args) } /* + * Wrapper of aug_match(, , non-NULL). + * Returns an array of nodes matching given path expression + */ +Handle<Value> LibAugeas::match(const Arguments& args) +{ + HandleScope scope; + + if (args.Length() != 1) { + ThrowException(Exception::TypeError(String::New("Wrong number of arguments"))); + return scope.Close(Undefined()); + } + + LibAugeas *obj = ObjectWrap::Unwrap<LibAugeas>(args.This()); + String::Utf8Value p_str(args[0]); + + const char *path = *p_str; + char **matches = NULL; + + int rc = aug_match(obj->m_aug, path, &matches); + if (rc >= 0) { + assert(matches != NULL); + Local<Array> result = Array::New(rc); + for (int i = 0; i < rc; ++i) { + result->Set(Number::New(i), String::New(matches[i])); + free(matches[i]); + } + free(matches); + return scope.Close(result); + } else { + throw_aug_error_msg(obj->m_aug); + return scope.Close(Undefined()); + } +} + +/* * Wrapper of aug_load() - load /files */ Handle<Value> LibAugeas::load(const Arguments& args) |
