lib/sqlList.c

Go to the documentation of this file.
00001 /* Stuff for processing comma separated lists - a little long so
00002  * in a separate module from jksql.c though interface is still
00003  * in jksql.c. 
00004  *
00005  * This file is copyright 2002 Jim Kent, but license is hereby
00006  * granted for all use - public, private or commercial. */
00007 
00008 #include "common.h"
00009 #include "sqlNum.h"
00010 #include "sqlList.h"
00011 #include "dystring.h"
00012 #include "hash.h"
00013 
00014 static char const rcsid[] = "$Id: sqlList.c,v 1.19 2006/07/17 19:35:30 markd Exp $";
00015 
00016 int sqlByteArray(char *s, signed char *array, int arraySize)
00017 /* Convert comma separated list of numbers to an array.  Pass in 
00018  * array an max size of array. */
00019 {
00020 unsigned count = 0;
00021 for (;;)
00022     {
00023     char *e;
00024     if (s == NULL || s[0] == 0 || count == arraySize)
00025         break;
00026     e = strchr(s, ',');
00027     if (e != NULL)
00028         *e++ = 0;
00029     array[count++] = sqlSigned(s);
00030     s = e;
00031     }
00032 return count;
00033 }
00034 
00035 void sqlByteStaticArray(char *s, signed char **retArray, int *retSize)
00036 /* Convert comma separated list of numbers to an array which will be
00037  * overwritten next call to this function, but need not be freed. */
00038 {
00039 static signed char *array = NULL;
00040 static unsigned alloc = 0;
00041 unsigned count = 0;
00042 
00043 for (;;)
00044     {
00045     char *e;
00046     if (s == NULL || s[0] == 0)
00047         break;
00048     e = strchr(s, ',');
00049     if (e != NULL)
00050         *e++ = 0;
00051     if (count >= alloc)
00052         {
00053         if (alloc == 0)
00054             alloc = 64;
00055         else
00056             alloc <<= 1;
00057         ExpandArray(array, count, alloc);
00058         }
00059     array[count++] = sqlSigned(s);
00060     s = e;
00061     }
00062 *retSize = count;
00063 *retArray = array;
00064 }
00065 
00066 void sqlByteDynamicArray(char *s, signed char **retArray, int *retSize)
00067 /* Convert comma separated list of numbers to an dynamically allocated
00068  * array, which should be freeMem()'d when done. */
00069 {
00070 signed char *sArray, *dArray = NULL;
00071 int size;
00072 
00073 sqlByteStaticArray(s, &sArray, &size);
00074 if (size > 0)
00075     {
00076     AllocArray(dArray,size);
00077     CopyArray(sArray, dArray, size);
00078     }
00079 *retArray = dArray;
00080 *retSize = size;
00081 }
00082 
00083 /*-------------------------*/
00084 
00085 int sqlUbyteArray(char *s, unsigned char *array, int arraySize)
00086 /* Convert comma separated list of numbers to an array.  Pass in 
00087  * array an max size of array. */
00088 {
00089 unsigned count = 0;
00090 for (;;)
00091     {
00092     char *e;
00093     if (s == NULL || s[0] == 0 || count == arraySize)
00094         break;
00095     e = strchr(s, ',');
00096     if (e != NULL)
00097         *e++ = 0;
00098     array[count++] = sqlUnsigned(s);
00099     s = e;
00100     }
00101 return count;
00102 }
00103 
00104 void sqlUbyteStaticArray(char *s, unsigned char **retArray, int *retSize)
00105 /* Convert comma separated list of numbers to an array which will be
00106  * overwritten next call to this function, but need not be freed. */
00107 {
00108 static unsigned char *array = NULL;
00109 static unsigned alloc = 0;
00110 unsigned count = 0;
00111 
00112 for (;;)
00113     {
00114     char *e;
00115     if (s == NULL || s[0] == 0)
00116         break;
00117     e = strchr(s, ',');
00118     if (e != NULL)
00119         *e++ = 0;
00120     if (count >= alloc)
00121         {
00122         if (alloc == 0)
00123             alloc = 64;
00124         else
00125             alloc <<= 1;
00126         ExpandArray(array, count, alloc);
00127         }
00128     array[count++] = sqlUnsigned(s);
00129     s = e;
00130     }
00131 *retSize = count;
00132 *retArray = array;
00133 }
00134 
00135 void sqlUbyteDynamicArray(char *s, unsigned char **retArray, int *retSize)
00136 /* Convert comma separated list of numbers to an dynamically allocated
00137  * array, which should be freeMem()'d when done. */
00138 {
00139 unsigned char *sArray, *dArray = NULL;
00140 int size;
00141 
00142 sqlUbyteStaticArray(s, &sArray, &size);
00143 if (size > 0)
00144     {
00145     AllocArray(dArray,size);
00146     CopyArray(sArray, dArray, size);
00147     }
00148 *retArray = dArray;
00149 *retSize = size;
00150 }
00151 
00152 /*-------------------------*/
00153 
00154 int sqlCharArray(char *s, char *array, int arraySize)
00155 /* Convert comma separated list of chars to an array.  Pass in 
00156  * array and max size of array. */
00157 {
00158 unsigned count = 0;
00159 for (;;)
00160     {
00161     char *e;
00162     if (s == NULL || s[0] == 0 || count == arraySize)
00163         break;
00164     e = strchr(s, ',');
00165     if (e != NULL)
00166         *e++ = 0;
00167     array[count++] = s[0];
00168     s = e;
00169     }
00170 return count;
00171 }
00172 
00173 void sqlCharStaticArray(char *s, char **retArray, int *retSize)
00174 /* Convert comma separated list of chars to an array which will be
00175  * overwritten next call to this function, but need not be freed. */
00176 {
00177 static char *array = NULL;
00178 static unsigned alloc = 0;
00179 unsigned count = 0;
00180 
00181 for (;;)
00182     {
00183     char *e;
00184     if (s == NULL || s[0] == 0)
00185         break;
00186     e = strchr(s, ',');
00187     if (e != NULL)
00188         *e++ = 0;
00189     if (count >= alloc)
00190         {
00191         if (alloc == 0)
00192             alloc = 64;
00193         else
00194             alloc <<= 1;
00195         ExpandArray(array, count, alloc);
00196         }
00197     array[count++] = s[0];
00198     s = e;
00199     }
00200 *retSize = count;
00201 *retArray = array;
00202 }
00203 
00204 void sqlCharDynamicArray(char *s, char **retArray, int *retSize)
00205 /* Convert comma separated list of chars to a dynamically allocated
00206  * array, which should be freeMem()'d when done. */
00207 {
00208 char *sArray, *dArray = NULL;
00209 int size;
00210 
00211 sqlCharStaticArray(s, &sArray, &size);
00212 if (size > 0)
00213     {
00214     AllocArray(dArray,size);
00215     CopyArray(sArray, dArray, size);
00216     }
00217 *retArray = dArray;
00218 *retSize = size;
00219 }
00220 
00221 /*-------------------------*/
00222 
00223 int sqlShortArray(char *s, short *array, int arraySize)
00224 /* Convert comma separated list of numbers to an array.  Pass in 
00225  * array an max size of array. */
00226 {
00227 unsigned count = 0;
00228 for (;;)
00229     {
00230     char *e;
00231     if (s == NULL || s[0] == 0 || count == arraySize)
00232         break;
00233     e = strchr(s, ',');
00234     if (e != NULL)
00235         *e++ = 0;
00236     array[count++] = sqlSigned(s);
00237     s = e;
00238     }
00239 return count;
00240 }
00241 
00242 void sqlShortStaticArray(char *s, short **retArray, int *retSize)
00243 /* Convert comma separated list of numbers to an array which will be
00244  * overwritten next call to this function, but need not be freed. */
00245 {
00246 static short *array = NULL;
00247 static unsigned alloc = 0;
00248 unsigned count = 0;
00249 
00250 for (;;)
00251     {
00252     char *e;
00253     if (s == NULL || s[0] == 0)
00254         break;
00255     e = strchr(s, ',');
00256     if (e != NULL)
00257         *e++ = 0;
00258     if (count >= alloc)
00259         {
00260         if (alloc == 0)
00261             alloc = 64;
00262         else
00263             alloc <<= 1;
00264         ExpandArray(array, count, alloc);
00265         }
00266     array[count++] = sqlSigned(s);
00267     s = e;
00268     }
00269 *retSize = count;
00270 *retArray = array;
00271 }
00272 
00273 void sqlShortDynamicArray(char *s, short **retArray, int *retSize)
00274 /* Convert comma separated list of numbers to an dynamically allocated
00275  * array, which should be freeMem()'d when done. */
00276 {
00277 short *sArray, *dArray = NULL;
00278 int size;
00279 
00280 sqlShortStaticArray(s, &sArray, &size);
00281 if (size > 0)
00282     {
00283     AllocArray(dArray,size);
00284     CopyArray(sArray, dArray, size);
00285     }
00286 *retArray = dArray;
00287 *retSize = size;
00288 }
00289 
00290 /*-------------------------*/
00291 
00292 int sqlUshortArray(char *s, unsigned short *array, int arraySize)
00293 /* Convert comma separated list of numbers to an array.  Pass in 
00294  * array an max size of array. */
00295 {
00296 unsigned count = 0;
00297 for (;;)
00298     {
00299     char *e;
00300     if (s == NULL || s[0] == 0 || count == arraySize)
00301         break;
00302     e = strchr(s, ',');
00303     if (e != NULL)
00304         *e++ = 0;
00305     array[count++] = sqlUnsigned(s);
00306     s = e;
00307     }
00308 return count;
00309 }
00310 
00311 void sqlUshortStaticArray(char *s, unsigned short **retArray, int *retSize)
00312 /* Convert comma separated list of numbers to an array which will be
00313  * overwritten next call to this function, but need not be freed. */
00314 {
00315 static unsigned short *array = NULL;
00316 static unsigned alloc = 0;
00317 unsigned count = 0;
00318 
00319 for (;;)
00320     {
00321     char *e;
00322     if (s == NULL || s[0] == 0)
00323         break;
00324     e = strchr(s, ',');
00325     if (e != NULL)
00326         *e++ = 0;
00327     if (count >= alloc)
00328         {
00329         if (alloc == 0)
00330             alloc = 64;
00331         else
00332             alloc <<= 1;
00333         ExpandArray(array, count, alloc);
00334         }
00335     array[count++] = sqlUnsigned(s);
00336     s = e;
00337     }
00338 *retSize = count;
00339 *retArray = array;
00340 }
00341 
00342 void sqlUshortDynamicArray(char *s, unsigned short **retArray, int *retSize)
00343 /* Convert comma separated list of numbers to an dynamically allocated
00344  * array, which should be freeMem()'d when done. */
00345 {
00346 unsigned short *sArray, *dArray = NULL;
00347 int size;
00348 
00349 sqlUshortStaticArray(s, &sArray, &size);
00350 if (size > 0)
00351     {
00352     AllocArray(dArray,size);
00353     CopyArray(sArray, dArray, size);
00354     }
00355 *retArray = dArray;
00356 *retSize = size;
00357 }
00358 
00359 /*-------------------------*/
00360 int sqlDoubleArray(char *s, double *array, int maxArraySize)
00361 /* Convert comma separated list of floating point numbers to an array.  
00362  * Pass in array and max size of array. */
00363 {
00364 unsigned count = 0;
00365 for (;;)
00366     {
00367     char *e;
00368     if (s == NULL || s[0] == 0 || count == maxArraySize)
00369         break;
00370     e = strchr(s, ',');
00371     if (e != NULL)
00372         *e++ = 0;
00373     array[count++] = atof(s);
00374     s = e;
00375     }
00376 return count;
00377 }
00378 
00379 
00380 int sqlFloatArray(char *s, float *array, int maxArraySize)
00381 /* Convert comma separated list of floating point numbers to an array.  
00382  * Pass in array and max size of array. */
00383 {
00384 unsigned count = 0;
00385 for (;;)
00386     {
00387     char *e;
00388     if (s == NULL || s[0] == 0 || count == maxArraySize)
00389         break;
00390     e = strchr(s, ',');
00391     if (e != NULL)
00392         *e++ = 0;
00393     array[count++] = atof(s);
00394     s = e;
00395     }
00396 return count;
00397 }
00398 
00399 void sqlDoubleStaticArray(char *s, double **retArray, int *retSize)
00400 /* Convert comma separated list of numbers to an array which will be
00401  * overwritten next call to this function, but need not be freed. */
00402 {
00403 static double *array = NULL;
00404 static unsigned alloc = 0;
00405 unsigned count = 0;
00406 
00407 for (;;)
00408     {
00409     char *e;
00410     if (s == NULL || s[0] == 0)
00411         break;
00412     e = strchr(s, ',');
00413     if (e != NULL)
00414         *e++ = 0;
00415     if (count >= alloc)
00416         {
00417         if (alloc == 0)
00418             alloc = 64;
00419         else
00420             alloc <<= 1;
00421         ExpandArray(array, count, alloc);
00422         }
00423     array[count++] = atof(s);
00424     s = e;
00425     }
00426 *retSize = count;
00427 *retArray = array;
00428 }
00429 
00430 void sqlFloatStaticArray(char *s, float **retArray, int *retSize)
00431 /* Convert comma separated list of numbers to an array which will be
00432  * overwritten next call to this function, but need not be freed. */
00433 {
00434 static float *array = NULL;
00435 static unsigned alloc = 0;
00436 unsigned count = 0;
00437 
00438 for (;;)
00439     {
00440     char *e;
00441     if (s == NULL || s[0] == 0)
00442         break;
00443     e = strchr(s, ',');
00444     if (e != NULL)
00445         *e++ = 0;
00446     if (count >= alloc)
00447         {
00448         if (alloc == 0)
00449             alloc = 128;
00450         else
00451             alloc <<= 1;
00452         ExpandArray(array, count, alloc);
00453         }
00454     array[count++] = atof(s);
00455     s = e;
00456     }
00457 *retSize = count;
00458 *retArray = array;
00459 }
00460 
00461 void sqlDoubleDynamicArray(char *s, double **retArray, int *retSize)
00462 /* Convert comma separated list of numbers to an dynamically allocated
00463  * array, which should be freeMem()'d when done. */
00464 {
00465 double *sArray, *dArray = NULL;
00466 int size;
00467 
00468 sqlDoubleStaticArray(s, &sArray, &size);
00469 if (size > 0)
00470     {
00471     AllocArray(dArray,size);
00472     CopyArray(sArray, dArray, size);
00473     }
00474 *retArray = dArray;
00475 *retSize = size;
00476 }
00477 
00478 void sqlFloatDynamicArray(char *s, float **retArray, int *retSize)
00479 /* Convert comma separated list of numbers to an dynamically allocated
00480  * array, which should be freeMem()'d when done. */
00481 {
00482 float *sArray, *dArray = NULL;
00483 int size;
00484 
00485 sqlFloatStaticArray(s, &sArray, &size);
00486 if (size > 0)
00487     {
00488     AllocArray(dArray,size);
00489     CopyArray(sArray, dArray, size);
00490     }
00491 *retArray = dArray;
00492 *retSize = size;
00493 }
00494 
00495 /*-------------------------*/
00496 
00497 int sqlUnsignedArray(char *s, unsigned *array, int arraySize)
00498 /* Convert comma separated list of numbers to an array.  Pass in 
00499  * array and max size of array. */
00500 {
00501 unsigned count = 0;
00502 for (;;)
00503     {
00504     char *e;
00505     if (s == NULL || s[0] == 0 || count == arraySize)
00506         break;
00507     e = strchr(s, ',');
00508     if (e != NULL)
00509         *e++ = 0;
00510     array[count++] = sqlUnsigned(s);
00511     s = e;
00512     }
00513 return count;
00514 }
00515 
00516 void sqlUnsignedStaticArray(char *s, unsigned **retArray, int *retSize)
00517 /* Convert comma separated list of numbers to an array which will be
00518  * overwritten next call to this function, but need not be freed. */
00519 {
00520 static unsigned *array = NULL;
00521 static unsigned alloc = 0;
00522 unsigned count = 0;
00523 
00524 for (;;)
00525     {
00526     char *e;
00527     if (s == NULL || s[0] == 0)
00528         break;
00529     e = strchr(s, ',');
00530     if (e != NULL)
00531         *e++ = 0;
00532     if (count >= alloc)
00533         {
00534         if (alloc == 0)
00535             alloc = 64;
00536         else
00537             alloc <<= 1;
00538         ExpandArray(array, count, alloc);
00539         }
00540     array[count++] = sqlUnsigned(s);
00541     s = e;
00542     }
00543 *retSize = count;
00544 *retArray = array;
00545 }
00546 
00547 void sqlUnsignedDynamicArray(char *s, unsigned **retArray, int *retSize)
00548 /* Convert comma separated list of numbers to an dynamically allocated
00549  * array, which should be freeMem()'d when done. */
00550 {
00551 unsigned *sArray, *dArray = NULL;
00552 int size;
00553 
00554 sqlUnsignedStaticArray(s, &sArray, &size);
00555 if (size > 0)
00556     {
00557     AllocArray(dArray,size);
00558     CopyArray(sArray, dArray, size);
00559     }
00560 *retArray = dArray;
00561 *retSize = size;
00562 }
00563 
00564 /*-------------------------*/
00565 
00566 int sqlSignedArray(char *s, int *array, int arraySize)
00567 /* Convert comma separated list of numbers to an array.  Pass in 
00568  * array an max size of array. */
00569 {
00570 int count = 0;
00571 for (;;)
00572     {
00573     char *e;
00574     if (s == NULL || s[0] == 0 || count == arraySize)
00575         break;
00576     e = strchr(s, ',');
00577     if (e != NULL)
00578         *e++ = 0;
00579     array[count++] = sqlSigned(s);
00580     s = e;
00581     }
00582 return count;
00583 }
00584 
00585 void sqlSignedStaticArray(char *s, int **retArray, int *retSize)
00586 /* Convert comma separated list of numbers to an array which will be
00587  * overwritten next call to this function, but need not be freed. */
00588 {
00589 static int *array = NULL;
00590 static int alloc = 0;
00591 int count = 0;
00592 
00593 for (;;)
00594     {
00595     char *e;
00596     if (s == NULL || s[0] == 0)
00597         break;
00598     e = strchr(s, ',');
00599     if (e != NULL)
00600         *e++ = 0;
00601     if (count >= alloc)
00602         {
00603         if (alloc == 0)
00604             alloc = 64;
00605         else
00606             alloc <<= 1;
00607         ExpandArray(array, count, alloc);
00608         }
00609     array[count++] = sqlSigned(s);
00610     s = e;
00611     }
00612 *retSize = count;
00613 *retArray = array;
00614 }
00615 
00616 void sqlSignedDynamicArray(char *s, int **retArray, int *retSize)
00617 /* Convert comma separated list of numbers to an dynamically allocated
00618  * array, which should be freeMem()'d when done. */
00619 {
00620 int *sArray, *dArray = NULL;
00621 int size;
00622 
00623 sqlSignedStaticArray(s, &sArray, &size);
00624 if (size > 0)
00625     {
00626     AllocArray(dArray,size);
00627     CopyArray(sArray, dArray, size);
00628     }
00629 *retArray = dArray;
00630 *retSize = size;
00631 }
00632 
00633 /*-------------------------*/
00634 
00635 int sqlLongLongArray(char *s, long long *array, int arraySize)
00636 /* Convert comma separated list of numbers to an array.  Pass in 
00637  * array and max size of array. */
00638 {
00639 unsigned count = 0;
00640 for (;;)
00641     {
00642     char *e;
00643     if (s == NULL || s[0] == 0 || count == arraySize)
00644         break;
00645     e = strchr(s, ',');
00646     if (e != NULL)
00647         *e++ = 0;
00648     array[count++] = sqlLongLong(s);
00649     s = e;
00650     }
00651 return count;
00652 }
00653 
00654 void sqlLongLongStaticArray(char *s, long long **retArray, int *retSize)
00655 /* Convert comma separated list of numbers to an array which will be
00656  * overwritten next call to this function, but need not be freed. */
00657 {
00658 static long long *array = NULL;
00659 static unsigned alloc = 0;
00660 unsigned count = 0;
00661 
00662 for (;;)
00663     {
00664     char *e;
00665     if (s == NULL || s[0] == 0)
00666         break;
00667     e = strchr(s, ',');
00668     if (e != NULL)
00669         *e++ = 0;
00670     if (count >= alloc)
00671         {
00672         if (alloc == 0)
00673             alloc = 64;
00674         else
00675             alloc <<= 1;
00676         ExpandArray(array, count, alloc);
00677         }
00678     array[count++] = sqlLongLong(s);
00679     s = e;
00680     }
00681 *retSize = count;
00682 *retArray = array;
00683 }
00684 
00685 void sqlLongLongDynamicArray(char *s, long long **retArray, int *retSize)
00686 /* Convert comma separated list of numbers to an dynamically allocated
00687  * array, which should be freeMem()'d when done. */
00688 {
00689 long long *sArray, *dArray = NULL;
00690 int size;
00691 
00692 sqlLongLongStaticArray(s, &sArray, &size);
00693 if (size > 0)
00694     {
00695     AllocArray(dArray,size);
00696     CopyArray(sArray, dArray, size);
00697     }
00698 *retArray = dArray;
00699 *retSize = size;
00700 }
00701 
00702 /*-------------------------*/
00703 
00704 
00705 int sqlStringArray(char *s, char **array, int maxArraySize)
00706 /* Convert comma separated list of strings to an array.  Pass in 
00707  * array and max size of array.  Returns actual size*/
00708 {
00709 int count = 0;
00710 for (;;)
00711     {
00712     char *e;
00713     if (s == NULL || s[0] == 0 || count == maxArraySize)
00714         break;
00715     e = strchr(s, ',');
00716     if (e != NULL)
00717         *e++ = 0;
00718     array[count++] = s;
00719     s = e;
00720     }
00721 return count;
00722 }
00723 
00724 void sqlStringStaticArray(char *s, char  ***retArray, int *retSize)
00725 /* Convert comma separated list of strings to an array which will be
00726  * overwritten next call to this function or to sqlStringDynamicArray,
00727  * but need not be freed. */
00728 {
00729 static char **array = NULL;
00730 static int alloc = 0;
00731 int count = 0;
00732 
00733 for (;;)
00734     {
00735     char *e;
00736     if (s == NULL || s[0] == 0)
00737         break;
00738     e = strchr(s, ',');
00739     if (e != NULL)
00740         *e++ = 0;
00741     if (count >= alloc)
00742         {
00743         if (alloc == 0)
00744             alloc = 64;
00745         else
00746             alloc <<= 1;
00747         ExpandArray(array, count, alloc);
00748         }
00749     array[count++] = s;
00750     s = e;
00751     }
00752 *retSize = count;
00753 *retArray = array;
00754 }
00755 
00756 void sqlStringDynamicArray(char *s, char ***retArray, int *retSize)
00757 /* Convert comma separated list of strings to an dynamically allocated
00758  * array, which should be freeMem()'d when done. As a speed option all
00759  * of the elements in the array are needMem()'d at the same time. This 
00760  * means that all the entries are free()'d by calling freeMem() on the
00761  * first element. For example:
00762  * sqlStringDynamicArray(s, &retArray, &retSize);
00763  * DoSomeFunction(retArray, retSize);
00764  * freeMem(retArray[0]);
00765  * freeMem(retArray);
00766  */
00767 {
00768 char **sArray, **dArray = NULL;
00769 int size;
00770 
00771 if (s == NULL)
00772     {
00773     *retArray = NULL;
00774     *retSize = 0;
00775     return;
00776     }
00777 s = cloneString(s);
00778 sqlStringStaticArray(s, &sArray, &size);
00779 if (size > 0)
00780     {
00781     AllocArray(dArray,size);
00782     CopyArray(sArray, dArray, size);
00783     }
00784 *retArray = dArray;
00785 *retSize = size;
00786 }
00787 
00788 char *sqlDoubleArrayToString( double *array, int arraySize)
00789 {
00790 int i;
00791 struct dyString *string = newDyString(256);
00792 char *toRet = NULL;
00793 for( i = 0 ; i < arraySize; i++ )
00794     {
00795     dyStringPrintf(string, "%f,", array[i]);
00796     }
00797 toRet = cloneString(string->string);
00798 dyStringFree(&string);
00799 return toRet;
00800 }
00801 
00802 char *sqlFloatArrayToString( float *array, int arraySize)
00803 {
00804 int i;
00805 struct dyString *string = newDyString(256);
00806 char *toRet = NULL;
00807 for( i = 0 ; i < arraySize; i++ )
00808     {
00809     dyStringPrintf(string, "%f,", array[i]);
00810     }
00811 toRet = cloneString(string->string);
00812 dyStringFree(&string);
00813 return toRet;
00814 }
00815 
00816 char *sqlUnsignedArrayToString( unsigned *array, int arraySize)
00817 {
00818 int i;
00819 struct dyString *string = newDyString(256);
00820 char *toRet = NULL;
00821 for( i = 0 ; i < arraySize; i++ )
00822     {
00823     dyStringPrintf(string, "%u,", array[i]);
00824     }
00825 toRet = cloneString(string->string);
00826 dyStringFree(&string);
00827 return toRet;
00828 }
00829 
00830 char *sqlSignedArrayToString( int *array, int arraySize)
00831 {
00832 int i;
00833 struct dyString *string = newDyString(256);
00834 char *toRet = NULL;
00835 for( i = 0 ; i < arraySize; i++ )
00836     {
00837     dyStringPrintf(string, "%d,", array[i]);
00838     }
00839 toRet = cloneString(string->string);
00840 dyStringFree(&string);
00841 return toRet;
00842 }
00843 
00844 char *sqlShortArrayToString( short *array, int arraySize)
00845 {
00846 int i;
00847 struct dyString *string = newDyString(256);
00848 char *toRet = NULL;
00849 for( i = 0 ; i < arraySize; i++ )
00850     {
00851     dyStringPrintf(string, "%d,", array[i]);
00852     }
00853 toRet = cloneString(string->string);
00854 dyStringFree(&string);
00855 return toRet;
00856 }
00857 
00858 char *sqlUshortArrayToString( unsigned short *array, int arraySize)
00859 {
00860 int i;
00861 struct dyString *string = newDyString(256);
00862 char *toRet = NULL;
00863 for( i = 0 ; i < arraySize; i++ )
00864     {
00865     dyStringPrintf(string, "%u,", array[i]);
00866     }
00867 toRet = cloneString(string->string);
00868 dyStringFree(&string);
00869 return toRet;
00870 }
00871 
00872 char *sqlByteArrayToString( signed char *array, int arraySize)
00873 {
00874 int i;
00875 struct dyString *string = newDyString(256);
00876 char *toRet = NULL;
00877 for( i = 0 ; i < arraySize; i++ )
00878     {
00879     dyStringPrintf(string, "%d,", array[i]);
00880     }
00881 toRet = cloneString(string->string);
00882 dyStringFree(&string);
00883 return toRet;
00884 }
00885 
00886 char *sqlUbyteArrayToString( unsigned char *array, int arraySize)
00887 {
00888 int i;
00889 struct dyString *string = newDyString(256);
00890 char *toRet = NULL;
00891 for( i = 0 ; i < arraySize; i++ )
00892     {
00893     dyStringPrintf(string, "%u,", array[i]);
00894     }
00895 toRet = cloneString(string->string);
00896 dyStringFree(&string);
00897 return toRet;
00898 }
00899 
00900 char *sqlCharArrayToString( char *array, int arraySize)
00901 {
00902 int i;
00903 struct dyString *string = newDyString(256);
00904 char *toRet = NULL;
00905 for( i = 0 ; i < arraySize; i++ )
00906     {
00907     dyStringPrintf(string, "%c,", array[i]);
00908     }
00909 toRet = cloneString(string->string);
00910 dyStringFree(&string);
00911 return toRet;
00912 }
00913 
00914 char *sqlLongLongArrayToString( long long *array, int arraySize)
00915 {
00916 int i;
00917 struct dyString *string = newDyString(256);
00918 char *toRet = NULL;
00919 for( i = 0 ; i < arraySize; i++ )
00920     {
00921     dyStringPrintf(string, "%lld,", array[i]);
00922     }
00923 toRet = cloneString(string->string);
00924 dyStringFree(&string);
00925 return toRet;
00926 }
00927 
00928 char *sqlStringArrayToString( char **array, int arraySize)
00929 {
00930 int i;
00931 struct dyString *string = newDyString(256);
00932 char *toRet = NULL;
00933 for( i = 0 ; i < arraySize; i++ )
00934     {
00935     dyStringPrintf(string, "%s,", array[i]);
00936     }
00937 toRet = cloneString(string->string);
00938 dyStringFree(&string);
00939 return toRet;
00940 }
00941 
00942 
00943 /* -------------- */
00944 
00945 
00946 
00947 void sqlStringFreeDynamicArray(char ***pArray)
00948 /* Free up a dynamic array (ends up freeing array and first string on it.) */
00949 {
00950 char **array;
00951 if ((array = *pArray) != NULL)
00952     {
00953     freeMem(array[0]);
00954     freez(pArray);
00955     }
00956 }
00957 
00958 int sqlUnsignedComma(char **pS)
00959 /* Return signed number at *pS.  Advance *pS past comma at end */
00960 {
00961 char *s = *pS;
00962 char *e = strchr(s, ',');
00963 unsigned ret;
00964 
00965 *e++ = 0;
00966 *pS = e;
00967 ret = sqlUnsigned(s);
00968 return ret;
00969 }
00970 
00971 
00972 int sqlSignedComma(char **pS)
00973 /* Return signed number at *pS.  Advance *pS past comma at end */
00974 {
00975 char *s = *pS;
00976 char *e = strchr(s, ',');
00977 int ret;
00978 
00979 *e++ = 0;
00980 *pS = e;
00981 ret = sqlSigned(s);
00982 return ret;
00983 }
00984 
00985 char sqlCharComma(char **pS)
00986 /* Return char at *pS.  Advance *pS past comma after char */
00987 {
00988 char *s = *pS;
00989 char *e = strchr(s, ',');
00990 int ret;
00991 
00992 *e++ = 0;
00993 *pS = e;
00994 ret = s[0];
00995 return ret;
00996 }
00997 
00998 long long sqlLongLongComma(char **pS)
00999 /* Return offset (often 64 bits) at *pS.  Advance *pS past comma at 
01000  * end */
01001 {
01002 char *s = *pS;
01003 char *e = strchr(s, ',');
01004 long long ret;
01005 
01006 *e++ = 0;
01007 *pS = e;
01008 ret = sqlLongLong(s);
01009 return ret;
01010 }
01011 
01012 float sqlFloatComma(char **pS)
01013 /* Return signed number at *pS.  Advance *pS past comma at end */
01014 {
01015 char *s = *pS;
01016 char *e = strchr(s, ',');
01017 float ret;
01018 
01019 *e++ = 0;
01020 *pS = e;
01021 ret = atof(s);
01022 return ret;
01023 }
01024 
01025 double sqlDoubleComma(char **pS)
01026 /* Return signed number at *pS.  Advance *pS past comma at end */
01027 {
01028 char *s = *pS;
01029 char *e = strchr(s, ',');
01030 double ret;
01031 
01032 *e++ = 0;
01033 *pS = e;
01034 ret = atof(s);
01035 return ret;
01036 }
01037 
01038 
01039 static char *findStringEnd(char *start, char endC)
01040 /* Return end of string. */
01041 {
01042 char c;
01043 char *s = start;
01044 
01045 for (;;)
01046     {
01047     c = *s;
01048     if (c == endC)
01049         return s;
01050     else if (c == 0)
01051         errAbort("Unterminated string");
01052     ++s;
01053     }
01054 }
01055 
01056 static char *sqlGetOptQuoteString(char **pS)
01057 /* Return string at *pS.  (Either quoted or not.)  Advance *pS. */
01058 {
01059 char *s = *pS;
01060 char *e;
01061 char c = *s;
01062 
01063 if (c  == '"' || c == '\'')
01064     {
01065     s += 1;
01066     e = findStringEnd(s, c);
01067     *e++ = 0;
01068     if (*e++ != ',')
01069         errAbort("Expecting comma after string");
01070     }
01071 else
01072     {
01073     e = strchr(s, ',');
01074     *e++ = 0;
01075     }
01076 *pS = e;
01077 return s;
01078 }
01079 
01080 char *sqlStringComma(char **pS)
01081 /* Return string at *pS.  (Either quoted or not.)  Advance *pS. */
01082 {
01083 return cloneString(sqlGetOptQuoteString(pS));
01084 }
01085 
01086 void sqlFixedStringComma(char **pS, char *buf, int bufSize)
01087 /* Copy string at *pS to buf.  Advance *pS. */
01088 {
01089 strncpy(buf, sqlGetOptQuoteString(pS), bufSize);
01090 }
01091 
01092 char *sqlEatChar(char *s, char c)
01093 /* Make sure next character is 'c'.  Return past next char */
01094 {
01095 if (*s++ != c)
01096     errAbort("Expecting %c got %c (%d) in database", c, s[-1], s[-1]);
01097 return s;
01098 }
01099 
01100 static struct hash *buildSymHash(char **values, boolean isEnum)
01101 /* build a hash of values for either enum or set symbolic column */
01102 {
01103 struct hash *valHash = hashNew(0);
01104 unsigned setVal = 1; /* not used for enum */
01105 int iVal;
01106 for (iVal = 0; values[iVal] != NULL; iVal++)
01107     {
01108     if (isEnum)
01109         hashAddInt(valHash, values[iVal], iVal);
01110     else
01111         {
01112         hashAddInt(valHash, values[iVal], setVal);
01113         setVal = setVal << 1;
01114         }
01115     }
01116 return valHash;
01117 }
01118 
01119 unsigned sqlEnumParse(char *valStr, char **values, struct hash **valHashPtr)
01120 /* parse an enumerated column value */
01121 {
01122 if (*valHashPtr == NULL)
01123     *valHashPtr = buildSymHash(values, TRUE);
01124 return hashIntVal(*valHashPtr, valStr);
01125 }
01126 
01127 unsigned sqlEnumComma(char **pS, char **values, struct hash **valHashPtr)
01128 /* Return enum at *pS.  (Either quoted or not.)  Advance *pS. */
01129 {
01130 return sqlEnumParse(sqlGetOptQuoteString(pS), values, valHashPtr);
01131 }
01132 
01133 void sqlEnumPrint(FILE *f, unsigned value, char **values)
01134 /* print an enumerated column value */
01135 {
01136 fputs(values[value], f);
01137 }
01138 
01139 unsigned sqlSetParse(char *valStr, char **values, struct hash **valHashPtr)
01140 /* parse a set column value */
01141 {
01142 if (*valHashPtr == NULL)
01143     *valHashPtr = buildSymHash(values, FALSE);
01144 /* parse comma separated string */
01145 unsigned value = 0;
01146 char *val = strtok(valStr, ",");
01147 while (val != NULL)
01148     {
01149     value |= hashIntVal(*valHashPtr, val);
01150     val = strtok(NULL, ",");
01151     }
01152 
01153 return value;
01154 }
01155 
01156 unsigned sqlSetComma(char **pS, char **values, struct hash **valHashPtr)
01157 /* Return set at *pS.  (Either quoted or not.)  Advance *pS. */
01158 {
01159 return sqlSetParse(sqlGetOptQuoteString(pS), values, valHashPtr);
01160 }
01161 
01162 void sqlSetPrint(FILE *f, unsigned value, char **values)
01163 /* print a set column value */
01164 {
01165 int iVal;
01166 unsigned curVal = 1;
01167 int cnt = 0;
01168 for (iVal = 0; values[iVal] != NULL; iVal++, curVal = curVal << 1)
01169     {
01170     if (curVal & value)
01171         {
01172         if (cnt > 0)
01173             fputc(',', f);
01174         fputs(values[iVal], f);
01175         cnt++;
01176         }
01177     }
01178 }

Generated on Tue Dec 25 18:39:32 2007 for blat by  doxygen 1.5.2