+
+/** \brief parse a parameter string */
+/* string is like 'name=value' */
+struct nameval *parse_nameval( char *arg ) {
+ struct nameval *nv = (struct nameval *) xmalloc(sizeof(*nv));
+ char *p=arg;
+ int len;
+ if (!p || !*p)
+ return 0; /* yeah, leaks a bit of memory. so what */
+ while ( *p && ( *p != '=' ) )
+ p++;
+ len = p - arg;
+ if (!len)
+ return 0;
+ nv->name = (char *) xmalloc(len+1);
+ strncpy(nv->name, arg, len);
+ nv->name[len]='\0';
+ if (*p == '=' )
+ p++; /* skip the '=' */
+ else
+ return 0; /* oops, no '=' */
+ if (!*p)
+ return 0; /* no value */
+ nv->val=xstrdup(p);
+ nv->next=0;
+ yaz_log(YLOG_DEBUG,"parse_nameval: n='%s' v='%s'", nv->name, nv->val );
+ return nv;
+}
+
+/** \brief append nv to the list of namevals */
+void append_nameval (struct prog_args *args, struct nameval *nv) {
+ if (!nv)
+ return;
+ if (args->namevals) {
+ nv->next=args->namevals->next; /* first */
+ args->namevals->next=nv;
+ args->namevals=nv;
+ } else {
+ nv->next=nv;
+ args->namevals=nv;
+ }
+} /* append_nameval */
+
+/** \brief parse a parameter file */
+void parse_paramfile(char *arg, struct prog_args *args) {
+ FILE *f=fopen(arg,"r");
+#define BUFSIZE 4096
+ char buf[BUFSIZE];
+ int len;
+ struct nameval *nv;
+ if (!f) {
+ yaz_log(YLOG_FATAL,"Could not open param file '%s' ", arg);
+ printf("Could not open file '%s' \n",arg);
+ exit(1);
+ }
+ yaz_log(YLOG_DEBUG,"Opened input file '%s' ",arg );
+ while (fgets(buf,BUFSIZE,f)) {
+ if (buf[0] != '#' ) {
+ len=strlen(buf)-1;
+ if (buf[len] == '\n')
+ buf[len] = '\0' ;
+ nv=parse_nameval(buf);
+ append_nameval(args, nv);
+ } /* not a comment */
+ }
+ (void) fclose(f);
+
+ if (0) {
+ nv=args->namevals;
+ printf("nv chain: ================ \n");
+ printf("(last:) %p: '%s' = '%s' (%p) \n",nv, nv->name, nv->val, nv->next );
+ do {
+ nv=nv->next;
+ printf("%p: '%s' = '%s' (%p)\n",nv, nv->name, nv->val, nv->next );
+ } while (nv != args->namevals );
+ exit(1);
+ }
+
+} /* parse_paramfile */
+
+