00001
00005 #include "system.h"
00006
00007 #include <argv.h>
00008
00009 #include "debug.h"
00010
00011
00017 static inline
00018 void * _free( const void * p)
00019
00020 {
00021 if (p != NULL) free((void *)p);
00022 return NULL;
00023 }
00024
00025 void argvPrint(const char * msg, ARGV_t argv, FILE * fp)
00026 {
00027 ARGV_t av;
00028
00029 if (fp == NULL) fp = stderr;
00030
00031 if (msg)
00032 fprintf(fp, "===================================== %s\n", msg);
00033
00034 if (argv)
00035 for (av = argv; *av; av++)
00036 fprintf(fp, "%s\n", *av);
00037
00038 }
00039
00040 ARGI_t argiFree(ARGI_t argi)
00041 {
00042 if (argi) {
00043 argi->nvals = 0;
00044 argi->vals = _free(argi->vals);
00045 }
00046 argi = _free(argi);
00047 return NULL;
00048 }
00049
00050 ARGV_t argvFree( ARGV_t argv)
00051 {
00052 ARGV_t av;
00053
00054
00055 if (argv)
00056 for (av = argv; *av; av++)
00057 *av = _free(*av);
00058
00059 argv = _free(argv);
00060 return NULL;
00061 }
00062
00063 int argiCount(ARGI_t argi)
00064 {
00065 int nvals = 0;
00066 if (argi)
00067 nvals = argi->nvals;
00068 return nvals;
00069 }
00070
00071 const ARGint_t argiData(const ARGI_t argi)
00072 {
00073 ARGint_t vals = NULL;
00074 if (argi && argi->nvals > 0)
00075 vals = argi->vals;
00076 return vals;
00077 }
00078
00079 int argvCount(const ARGV_t argv)
00080 {
00081 int argc = 0;
00082 if (argv)
00083 while (argv[argc] != NULL)
00084 argc++;
00085 return argc;
00086 }
00087
00088 const ARGV_t argvData(const ARGV_t argv)
00089 {
00090
00091 return argv;
00092
00093 }
00094
00095 int argvCmp(const void * a, const void * b)
00096 {
00097
00098 ARGstr_t astr = *(ARGV_t)a;
00099 ARGstr_t bstr = *(ARGV_t)b;
00100
00101 return strcmp(astr, bstr);
00102 }
00103
00104 int argvSort(ARGV_t argv, int (*compar)(const void *, const void *))
00105 {
00106 if (compar == NULL)
00107 compar = argvCmp;
00108 qsort(argv, argvCount(argv), sizeof(*argv), compar);
00109 return 0;
00110 }
00111
00112 ARGV_t argvSearch(ARGV_t argv, ARGstr_t val,
00113 int (*compar)(const void *, const void *))
00114 {
00115 if (argv == NULL)
00116 return NULL;
00117 if (compar == NULL)
00118 compar = argvCmp;
00119 return bsearch(&val, argv, argvCount(argv), sizeof(*argv), compar);
00120 }
00121
00122 int argiAdd( ARGI_t * argip, int ix, int val)
00123 {
00124 ARGI_t argi;
00125
00126 if (argip == NULL)
00127 return -1;
00128 if (*argip == NULL)
00129 *argip = xcalloc(1, sizeof(**argip));
00130 argi = *argip;
00131 if (ix < 0)
00132 ix = argi->nvals;
00133 if (ix >= argi->nvals) {
00134 argi->vals = xrealloc(argi->vals, (ix + 1) * sizeof(*argi->vals));
00135 memset(argi->vals + argi->nvals, 0,
00136 (ix - argi->nvals) * sizeof(*argi->vals));
00137 argi->nvals = ix + 1;
00138 }
00139 argi->vals[ix] = val;
00140 return 0;
00141 }
00142
00143 int argvAdd( ARGV_t * argvp, ARGstr_t val)
00144 {
00145 ARGV_t argv;
00146 int argc;
00147
00148 if (argvp == NULL)
00149 return -1;
00150 argc = argvCount(*argvp);
00151
00152 *argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp));
00153
00154 argv = *argvp;
00155 argv[argc++] = xstrdup(val);
00156 argv[argc ] = NULL;
00157 return 0;
00158 }
00159
00160 int argvAppend( ARGV_t * argvp, const ARGV_t av)
00161 {
00162 ARGV_t argv = *argvp;
00163 int argc = argvCount(argv);
00164 int ac = argvCount(av);
00165 int i;
00166
00167 argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
00168 for (i = 0; i < ac; i++)
00169 argv[argc + i] = xstrdup(av[i]);
00170 argv[argc + ac] = NULL;
00171 *argvp = argv;
00172 return 0;
00173 }
00174
00175 int argvSplit(ARGV_t * argvp, const char * str, const char * seps)
00176 {
00177 char * dest = alloca(strlen(str) + 1);
00178 ARGV_t argv;
00179 int argc = 1;
00180 const char * s;
00181 char * t;
00182 int c;
00183
00184 for (argc = 1, s = str, t = dest; (c = *s); s++, t++) {
00185 if (strchr(seps, c)) {
00186 argc++;
00187 c = '\0';
00188 }
00189 *t = c;
00190 }
00191 *t = '\0';
00192
00193 argv = xmalloc( (argc + 1) * sizeof(*argv));
00194
00195 for (c = 0, s = dest; s < t; s+= strlen(s) + 1) {
00196 if (*s == '\0')
00197 continue;
00198 argv[c] = xstrdup(s);
00199 c++;
00200 }
00201 argv[c] = NULL;
00202 *argvp = argv;
00203
00204 return 0;
00205
00206 }
00207