static rc_t ConfigRepoSet(DLList* repos, KConfig * kfg, const char* kfgPath, const char *dflt, uint8_t type) { const KConfigNode *node; rc_t rc = KConfigOpenNodeRead ( kfg, & node, kfgPath ); if ( rc == 0 ) { KNamelist* children; rc = KConfigNodeListChild ( node, &children ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount ( children, &count ); if ( rc == 0 ) { uint32_t i; for (i = 0; i < count; ++i) { const char* name; rc = KNamelistGet ( children, i, &name ); if ( rc == 0 ) { #define BufSize 4096 char buf[ BufSize ]; size_t bSize = string_size(kfgPath); string_copy(buf, BufSize, kfgPath, bSize); if (bSize + string_size(name) < sizeof(buf)) { NCBIRepository* repo; string_copy(buf + bSize, sizeof(buf) - bSize, name, string_size(name) + 1); rc = ConfigRepo( kfg, dflt, buf, buf, type, &repo ); DLListPushTail( repos, (DLNode*) repo ); } #undef BufSize } else { rc = RC ( rcSRA, rcMgr, rcConstructing, rcString, rcExcessive ); } if ( rc != 0 ) { break; } } } KNamelistRelease ( children ); } KConfigNodeRelease ( node ); } if (GetRCState(rc) == rcNotFound) { return 0; } return rc; }
static rc_t ShowConfig(const KConfig* cfg, Params* prm) { rc_t rc = 0; bool hasAny = false; bool hasQuery = false; bool xml = true; assert(cfg && prm); xml = prm->xml; while (rc == 0) { KNamelist* names = NULL; const KConfigNode* node = NULL; uint32_t count = 0; uint32_t i = 0; int indent = 0; const char* root = NULL; const char* nodeName = NULL; size_t nodeNameL = 1; rc = ParamsGetNextParam(prm, &root); if (rc == 0) { if (root == NULL) { if (hasQuery) { break; } else { root = "/"; } } else { hasQuery = true; } assert(root); } if (rc == 0) { int64_t len = strlen(root); assert(len > 0); while (len > 0) { if (root[len - 1] == '/') { --len; } else { break; } } assert(len >= 0); if (len == 0) { root += strlen(root) - 1; nodeName = root; } else { char *c = memrchr(root, '/', len); if (c != NULL) { nodeName = c + 1; } else { nodeName = root; } } assert(nodeName && nodeName[0]); nodeNameL = strlen(nodeName); while (nodeNameL > 1 && nodeName[nodeNameL - 1] == '/') { --nodeNameL; } } if (rc == 0) { rc = KConfigOpenNodeRead(cfg, &node, root); DISP_RC(rc, root); } if (rc == 0) { rc = KConfigNodeListChild(node, &names); } if (rc == 0) { rc = KNamelistCount(names, &count); } if (rc == 0 && count == 0) { char buf[512] = ""; size_t num_read = 0; rc = KConfigNodeReadData(node, buf, sizeof buf, &num_read); if (rc == 0 && num_read > 0) { if (prm->showMultiple) { OUTMSG(("<!-- Configuration node %s -->\n", root)); } if (xml) { VDB_CONGIG_OUTMSG(("<%.*s>", nodeNameL, nodeName)); VDB_CONGIG_OUTMSG(("%.*s", (int)num_read, buf)); VDB_CONGIG_OUTMSG(("</%.*s>\n", nodeNameL, nodeName)); } else { OUTMSG(("%.*s = \"%.*s\"\n", nodeNameL, nodeName, (int)num_read, buf)); } hasAny = true; } } else { if (rc == 0) { if (nodeName[0] != '/') { if (prm->showMultiple) { OUTMSG(("<!-- Configuration node %s -->\n", root)); } VDB_CONGIG_OUTMSG(("<%.*s>\n", nodeNameL, nodeName)); } else { if (prm->showMultiple) { OUTMSG(("<!-- Current configuration -->\n")); } VDB_CONGIG_OUTMSG(("<Config>\n")); } hasAny = true; ++indent; } for (i = 0; i < count && rc == 0; ++i) { const char* name = NULL; if (rc == 0) { rc = KNamelistGet(names, i, &name); } if (rc == 0) { char* fullname = NULL; if (strcmp(root, "/") == 0) { fullname = malloc(strlen(name) + 2); if (fullname == NULL) { rc = RC(rcExe, rcStorage, rcAllocating, rcMemory, rcExhausted); } sprintf(fullname, "/%s", name); } else { fullname = strdup(root); if (fullname == NULL) { rc = RC(rcExe, rcStorage, rcAllocating, rcMemory, rcExhausted); } } if (rc == 0) { rc = KConfigNodePrintChildNames (xml, node, name, indent, fullname); hasAny = true; } free(fullname); fullname = NULL; } } if (rc == 0) { if (nodeName[0] != '/') { VDB_CONGIG_OUTMSG(("</%.*s>\n", nodeNameL, nodeName)); } else { VDB_CONGIG_OUTMSG(("</Config>\n")); } } } RELEASE(KConfigNode, node); RELEASE(KNamelist, names); if (rc == 0) { if (hasAny) { OUTMSG(("\n")); } else if (nodeNameL > 0 && nodeName != NULL) { VDB_CONGIG_OUTMSG(("<%.*s/>\n", nodeNameL, nodeName)); } } if (!hasQuery) { break; } } return rc; }
static rc_t KConfigNodePrintChildNames(bool xml, const KConfigNode* self, const char* name, int indent, const char* aFullpath) { rc_t rc = 0; uint32_t count = 0; int i = 0; char buffer[512] = ""; size_t num_read = 0; bool hasChildren = false; bool hasData = false; const KConfigNode* node = NULL; KNamelist* names = NULL; assert(self && name); if (rc == 0) { rc = KConfigNodeOpenNodeRead(self, &node, name); } if (rc == 0) { rc = KConfigNodeReadData(node, buffer, sizeof buffer, &num_read); hasData = num_read; if (hasData) { /* VDB_CONGIG_OUTMSG(("\n%s = \"%.*s\"\n\n", aFullpath, num_read, buffer)); */ } } if (rc == 0) { rc = KConfigNodeListChild(node, &names); } if (rc == 0) { rc = KNamelistCount(names, &count); hasChildren = count; } Indent(xml, indent); VDB_CONGIG_OUTMSG(("<%s", name)); if (!hasChildren && !hasData) { VDB_CONGIG_OUTMSG(("/>\n")); } else { VDB_CONGIG_OUTMSG((">")); } if (hasData) { if (xml) { VDB_CONGIG_OUTMSG(("%.*s", (int)num_read, buffer)); } else { OUTMSG(("%s = \"%.*s\"\n", aFullpath, (int)num_read, buffer)); } } if (hasChildren) { VDB_CONGIG_OUTMSG(("\n"));} if (hasChildren) { for (i = 0; i < count && rc == 0; ++i) { char* fullpath = NULL; const char* name = NULL; rc = KNamelistGet(names, i, &name); if (rc == 0) { fullpath = malloc(strlen(aFullpath) + 1 + strlen(name) + 1); if (fullpath == NULL) { rc = RC (rcExe, rcStorage, rcAllocating, rcMemory, rcExhausted); } else { sprintf(fullpath, "%s/%s", aFullpath, name); } } if (rc == 0) { rc = KConfigNodePrintChildNames (xml, node, name, indent + 1, fullpath); } free(fullpath); } } if (hasChildren) { Indent(xml, indent); } if (hasChildren || hasData) { VDB_CONGIG_OUTMSG(("</%s>\n",name)); } RELEASE(KNamelist, names); RELEASE(KConfigNode, node); return rc; }
static rc_t RefSeqMgr_KfgReadRepositories(const KConfig* kfg, char* paths, size_t paths_sz) { /* servers are children of refseq/repository, e.g.: /refseq/repository/main="..." */ /* volumes are in refseq/repository/<serverName>/volumes, e.g.: /refseq/repository/main/volumes="..." */ /* all server/volume combinations are returned in paths separated by ':' */ rc_t rc = 0; const KConfigNode *node; #define KFG_PATH "/refseq/repository/" paths[0] = 0; rc = KConfigOpenNodeRead ( kfg, & node, KFG_PATH ); if ( rc == 0 ) { KNamelist* children; rc = KConfigNodeListChild ( node, &children ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount ( children, &count ); if ( rc == 0 ) { uint32_t i; for (i = 0; i < count; ++i) /* for all servers */ { const char* name; rc = KNamelistGet ( children, i, &name ); if ( rc == 0 ) { #define BufSize 4096 char server[ BufSize ]; char buf[ BufSize ]; size_t num_writ; rc = string_printf(buf, BufSize, &num_writ, KFG_PATH "%s", name); if (rc == 0) { rc = RefSeqMgr_ConfigValue ( kfg, buf, server, sizeof(server) ); if (rc == 0) { rc = string_printf(buf, BufSize, &num_writ, KFG_PATH "%s/volumes", name); if (rc == 0) { char volumes[ BufSize ]; rc = RefSeqMgr_ConfigValue ( kfg, buf, volumes, sizeof(volumes) ); if (rc == 0) { /* create a server/volume pair for every combination, append to paths, ':' - separate */ char *vol_rem = volumes; char *vol_sep; do { char const *volume = vol_rem; vol_sep = string_chr(volume, string_size(volume), ':'); if(vol_sep) { vol_rem = vol_sep + 1; *vol_sep = 0; } string_copy(paths + string_size(paths), paths_sz - string_size(paths), server, string_size(server)); if (paths[string_size(paths)-1] != '/') { string_copy(paths + string_size(paths), paths_sz - string_size(paths), "/", 1); } string_copy(paths + string_size(paths), paths_sz - string_size(paths), volume, string_size(volume)); string_copy(paths + string_size(paths), paths_sz - string_size(paths), ":", 1); } while(vol_sep); } } } } #undef BufSize } if ( rc != 0 ) { break; } } } KNamelistRelease ( children ); } KConfigNodeRelease ( node ); } if (GetRCState(rc) == rcNotFound) { paths[0] = '\0'; return 0; } return 0; }