00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef COMMON_H
00010 #define COMMON_H
00011
00012
00013 #ifndef _LARGEFILE_SOURCE
00014 #define _LARGEFILE_SOURCE 1
00015 #endif
00016
00017 #ifndef _GNU_SOURCE
00018 #define _GNU_SOURCE
00019 #endif
00020
00021 #ifndef _FILE_OFFSET_BITS
00022 #define _FILE_OFFSET_BITS 64
00023 #endif
00024
00025
00026 #ifndef _REENTRANT
00027 #define _REENTRANT
00028 #endif
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdarg.h>
00033 #include <string.h>
00034 #include <ctype.h>
00035 #include <sys/types.h>
00036 #include <sys/stat.h>
00037 #include <strings.h>
00038 #include <fcntl.h>
00039 #include <assert.h>
00040 #include <setjmp.h>
00041 #include <time.h>
00042 #include <math.h>
00043 #include <errno.h>
00044 #include <unistd.h>
00045 #include <libgen.h>
00046
00047 #if defined(MACHTYPE_ppc)
00048 #include <sys/wait.h>
00049 #endif
00050
00051 #if defined(__APPLE__)
00052 #if defined(__i686__)
00053
00054 #define warn jkWarn
00055 #endif
00056 #endif
00057
00058 #ifdef __CYGWIN32__
00059 #include <mingw/math.h>
00060 #endif
00061
00062 #ifndef NAN
00063 #define NAN (0.0 / 0.0)
00064 #endif
00065
00066 #ifndef WIFEXITED
00067 #define WIFEXITED(stat) (((*((int *) &(stat))) & 0xff) == 0)
00068 #endif
00069
00070 #ifndef WEXITSTATUS
00071 #define WEXITSTATUS(stat) (short)(((*((int *) &(stat))) >> 8) & 0xffff)
00072 #endif
00073
00074 #ifndef WIFSIGNALED
00075 #define WIFSIGNALED(stat) (((*((int *) &(stat)))) && ((*((int *) &(stat))) == ((*((int *) &(stat))) &0x00ff)))
00076 #endif
00077
00078 #ifndef WTERMSIG
00079 #define WTERMSIG(stat) ((*((int *) &(stat))) & 0x7f)
00080 #endif
00081
00082 #ifndef WIFSTOPPED
00083 #define WIFSTOPPED(stat) (((*((int *) &(stat))) & 0xff) == 0177)
00084 #endif
00085
00086 #ifndef WSTOPSIG
00087 #define WSTOPSIG(stat) (((*((int *) &(stat))) >> 8) & 0xff)
00088 #endif
00089
00090 #ifndef HUGE
00091 #define HUGE MAXFLOAT
00092 #endif
00093
00094
00095
00096 #define TRUE 1
00097 #define FALSE 0
00098 #define boolean int
00099 #ifndef __cplusplus
00100 #ifndef bool
00101 #define bool char
00102 #endif
00103 #endif
00104
00105
00106 #define UBYTE unsigned char
00107 #define BYTE signed char
00108 #define UWORD unsigned short
00109 #define WORD short
00110 #define bits64 unsigned long long
00111 #define bits32 unsigned
00112 #define bits16 unsigned short
00113 #define bits8 unsigned char
00114 #define signed32 int
00115 #define bits8 unsigned char
00116
00117 #define BIGNUM 0x3fffffff
00118
00119 #define LIMIT_2or8GB (2147483647 * ((sizeof(size_t)/4)*(sizeof(size_t)/4)))
00120
00121 #define LIMIT_2or6GB (2147483647 + (2147483647 * ((sizeof(size_t)/4)-1)) + \
00122 (2147483647 * ((sizeof(size_t)/4)-1)))
00123
00124
00125
00126 #define PATH_LEN 512
00127
00128
00129
00130
00131
00132
00133 #ifndef INLINE
00134 #define INLINE static inline
00135 #endif
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 #if defined(__va_copy) && !defined(va_copy)
00156 # define va_copy __va_copy
00157 #endif
00158 #if !defined(va_copy)
00159 # define va_copy(to, from) ((to) = (from))
00160 #endif
00161
00162
00163
00164
00165
00166 #define ptrToLL(p) ((long long)((size_t)p))
00167
00168
00169 #define ArraySize(a) (sizeof(a)/sizeof((a)[0]))
00170
00171 #define uglyf printf
00172 #define uglyAbort errAbort
00173 #define uglyOut stdout
00174
00175 void *needMem(size_t size);
00176
00177
00178
00179 void *needLargeMem(size_t size);
00180
00181
00182
00183 void *needLargeZeroedMem(size_t size);
00184
00185
00186 void *needLargeMemResize(void* vp, size_t size);
00187
00188
00189
00190 void *needLargeZeroedMemResize(void* vp, size_t oldSize, size_t newSize);
00191
00192
00193
00194 void *needHugeMem(size_t size);
00195
00196
00197 void *needHugeZeroedMem(size_t size);
00198
00199
00200 void *needHugeMemResize(void* vp, size_t size);
00201
00202
00203
00204
00205 void *needHugeZeroedMemResize(void* vp, size_t oldSize, size_t newSize);
00206
00207
00208
00209
00210 void *needMoreMem(void *old, size_t copySize, size_t newSize);
00211
00212
00213 void *cloneMem(void *pt, size_t size);
00214
00215
00216 #define CloneVar(pt) cloneMem(pt, sizeof((pt)[0]))
00217
00218
00219 void *wantMem(size_t size);
00220
00221
00222
00223 void freeMem(void *pt);
00224
00225
00226 void freez(void *ppt);
00227
00228
00229
00230
00231
00232
00233 #define AllocVar(pt) (pt = needMem(sizeof(*pt)))
00234
00235
00236
00237 #define AllocArray(pt, size) (pt = needLargeZeroedMem(sizeof(*pt) * (size)))
00238
00239 #define AllocA(type) needMem(sizeof(type))
00240
00241
00242 #define AllocN(type,count) ((type*)needLargeZeroedMem(sizeof(type) * (count)))
00243
00244
00245 #define ExpandArray(array, oldCount, newCount) \
00246 (array = needMoreMem((array), (oldCount)*sizeof((array)[0]), (newCount)*sizeof((array)[0])))
00247
00248
00249 #define CopyArray(source, dest,count) memcpy(dest,source,(count)*sizeof(dest[0]))
00250
00251
00252 #define CloneArray(a, count) cloneMem(a, (count)*sizeof(a[0]))
00253
00254
00255 void errAbort(char *format, ...)
00256
00257 #if defined(__GNUC__) && defined(JK_WARN)
00258 __attribute__((format(printf, 1, 2)))
00259 #endif
00260 ;
00261
00262 void errnoAbort(char *format, ...)
00263
00264 #if defined(__GNUC__) && defined(JK_WARN)
00265 __attribute__((format(printf, 1, 2)))
00266 #endif
00267 ;
00268
00269 #define internalErr() errAbort("Internal error %s %d", __FILE__, __LINE__)
00270
00271
00272 void warn(char *format, ...)
00273
00274 #if defined(__GNUC__) && defined(JK_WARN)
00275 __attribute__((format(printf, 1, 2)))
00276 #endif
00277 ;
00278
00279 void verbose(int verbosity, char *format, ...)
00280
00281
00282
00283 #if defined(__GNUC__) && defined(JK_WARN)
00284 __attribute__((format(printf, 2, 3)))
00285 #endif
00286 ;
00287
00288 void verboseDot();
00289
00290
00291 int verboseLevel();
00292
00293
00294 void verboseSetLevel(int verbosity);
00295
00296
00297
00298 void zeroBytes(void *vpt, int count);
00299
00300
00301 #define ZeroVar(v) zeroBytes(v, sizeof(*v))
00302
00303 void reverseBytes(char *bytes, long length);
00304
00305
00306 void reverseInts(int *a, int length);
00307
00308
00309 void reverseUnsigned(unsigned *a, int length);
00310
00311
00312 void reverseDoubles(double *a, int length);
00313
00314
00315 void reverseStrings(char **a, int length);
00316
00317
00318 void swapBytes(char *a, char *b, int length);
00319
00320
00321
00322
00323 struct slList
00324 {
00325 struct slList *next;
00326 };
00327
00328 int slCount(void *list);
00329
00330
00331 void *slElementFromIx(void *list, int ix);
00332
00333
00334
00335 int slIxFromElement(void *list, void *el);
00336
00337
00338 void slSafeAddHead(void *listPt, void *node);
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 #define slAddHead(listPt, node) \
00350 ((node)->next = *(listPt), *(listPt) = (node))
00351
00352 void slAddTail(void *listPt, void *node);
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 void *slPopHead(void *listPt);
00363
00364
00365 void *slPopTail(void *listPt);
00366
00367
00368 void *slCat(void *a, void *b);
00369
00370
00371
00372
00373
00374
00375
00376
00377 void *slLastEl(void *list);
00378
00379
00380 void slReverse(void *listPt);
00381
00382
00383
00384
00385
00386 typedef int CmpFunction(const void *elem1, const void *elem2);
00387
00388 void slSort(void *pList, CmpFunction *compare);
00389
00390
00391
00392
00393 void slUniqify(void *pList, CmpFunction *compare, void (*free)());
00394
00395
00396
00397
00398
00399 boolean slRemoveEl(void *vpList, void *vToRemove);
00400
00401
00402
00403
00404 void slFreeList(void *listPt);
00405
00406
00407
00408
00409
00410 struct slInt
00411
00412 {
00413 struct slInt *next;
00414 int val;
00415 };
00416
00417 struct slInt *slIntNew(int x);
00418 #define newSlInt slIntNew
00419
00420
00421 int slIntCmp(const void *va, const void *vb);
00422
00423
00424 int slIntCmpRev(const void *va, const void *vb);
00425
00426
00427 void doubleSort(int count, double *array);
00428
00429
00430 double doubleMedian(int count, double *array);
00431
00432
00433
00434 struct slDouble
00435
00436 {
00437 struct slDouble *next;
00438 double val;
00439 };
00440
00441 struct slDouble *slDoubleNew(double x);
00442 #define newSlDouble slDoubleNew
00443
00444
00445 int slDoubleCmp(const void *va, const void *vb);
00446
00447
00448 double slDoubleMedian(struct slDouble *list);
00449
00450
00451 void intSort(int count, int *array);
00452
00453
00454 int intMedian(int count, int *array);
00455
00456
00457
00458 struct slName
00459
00460
00461 {
00462 struct slName *next;
00463 char name[1];
00464 };
00465
00466 struct slName *newSlName(char *name);
00467
00468 #define slNameNew newSlName
00469
00470
00471 #define slNameFree freez
00472
00473
00474 #define slNameFreeList slFreeList
00475
00476
00477 struct slName *slNameNewN(char *name, int size);
00478
00479
00480 int slNameCmpCase(const void *va, const void *vb);
00481
00482
00483 int slNameCmp(const void *va, const void *vb);
00484
00485
00486 void slNameSortCase(struct slName **pList);
00487
00488
00489 void slNameSort(struct slName **pList);
00490
00491
00492 boolean slNameInList(struct slName *list, char *string);
00493
00494
00495 void *slNameFind(void *list, char *string);
00496
00497
00498
00499 int slNameFindIx(struct slName *list, char *string);
00500
00501
00502
00503
00504 char *slNameStore(struct slName **pList, char *string);
00505
00506
00507
00508 struct slName *slNameAddHead(struct slName **pList, char *name);
00509
00510
00511 struct slName *slNameAddTail(struct slName **pList, char *name);
00512
00513
00514
00515 struct slName *slNameCloneList(struct slName *list);
00516
00517
00518 struct slName *slNameListFromString(char *s, char delimiter);
00519
00520
00521
00522
00523 #define slNameListFromComma(s) slNameListFromString(s, ',')
00524
00525
00526 struct slName *slNameLoadReal(char *fileName);
00527
00528
00529
00530 struct slRef
00531
00532 {
00533 struct slRef *next;
00534 void *val;
00535 };
00536
00537 struct slRef *slRefNew(void *val);
00538
00539
00540 struct slRef *refOnList(struct slRef *refList, void *val);
00541
00542
00543 void refAdd(struct slRef **pRefList, void *val);
00544
00545
00546 void refAddUnique(struct slRef **pRefList, void *val);
00547
00548
00549 struct slRef *refListFromSlList(void *list);
00550
00551
00552 struct slPair
00553
00554 {
00555 struct slPair *next;
00556 char *name;
00557 void *val;
00558 };
00559
00560 struct slPair *slPairNew(char *name, void *val);
00561
00562
00563 void slPairAdd(struct slPair **pList, char *name, void *val);
00564
00565
00566 void slPairFree(struct slPair **pEl);
00567
00568
00569 void slPairFreeList(struct slPair **pList);
00570
00571
00572 void slPairFreeVals(struct slPair *list);
00573
00574
00575 void slPairFreeValsAndList(struct slPair **pList);
00576
00577
00578 struct slPair *slPairFind(struct slPair *list, char *name);
00579
00580
00581 void *slPairFindVal(struct slPair *list, char *name);
00582
00583
00584
00585 void gentleFree(void *pt);
00586
00587
00588
00589
00590
00591 char *cloneStringZ(char *s, int size);
00592
00593
00594 char *cloneString(char *s);
00595
00596
00597 char *cloneLongString(char *s);
00598
00599
00600 int differentWord(char *s1, char *s2);
00601
00602
00603
00604
00605 #define sameWord(a,b) (!differentWord(a,b))
00606
00607
00608 #define differentString(a,b) (strcmp(a,b))
00609
00610
00611 int differentStringNullOk(char *a, char *b);
00612
00613
00614
00615
00616
00617
00618
00619
00620 #define sameOk(a,b) (differentStringNullOk(a,b) == 0)
00621
00622
00623 #define sameString(a,b) (strcmp(a,b)==0)
00624
00625
00626 #define sameStringN(a,b,c) (strncmp(a,b,c)==0)
00627
00628
00629 #define isEmpty(string) (string == NULL || string[0] == 0)
00630 #define isNotEmpty(string) (! isEmpty(string))
00631
00632 boolean startsWith(char *start,char *string);
00633
00634
00635 boolean startsWithWord(char *firstWord, char *line);
00636
00637
00638
00639 #define stringIn(needle, haystack) strstr(haystack, needle)
00640
00641
00642
00643 char *rStringIn(char *needle, char *haystack);
00644
00645
00646 char *stringBetween(char *start, char *end, char *haystack);
00647
00648
00649
00650
00651 boolean endsWith(char *string, char *end);
00652
00653
00654 char lastChar(char *s);
00655
00656
00657 boolean wildMatch(char *wildCard, char *string);
00658
00659
00660
00661
00662
00663 char *memMatch(char *needle, int nLen, char *haystack, int hLen);
00664
00665
00666
00667 void toUpperN(char *s, int n);
00668
00669
00670 void toLowerN(char *s, int n);
00671
00672
00673 void toggleCase(char *s, int size);
00674
00675
00676 void touppers(char *s);
00677
00678
00679 void tolowers(char *s);
00680
00681
00682 char *replaceChars(char *string, char *oldStr, char *newStr);
00683
00684
00685
00686
00687
00688
00689
00690 void subChar(char *s, char oldChar, char newChar);
00691
00692
00693 void stripChar(char *s, char c);
00694
00695
00696 void stripString(char *s, char *strip);
00697
00698
00699 int countChars(char *s, char c);
00700
00701
00702 int countCharsN(char *s, char c, int size);
00703
00704
00705 int countLeadingChars(char *s, char c);
00706
00707
00708 int countSame(char *a, char *b);
00709
00710
00711 int chopString(char *in, char *sep, char *outArray[], int outSize);
00712
00713
00714
00715
00716
00717
00718
00719
00720 extern char crLfChopper[];
00721 extern char whiteSpaceChopper[];
00722
00723
00724 int chopByWhite(char *in, char *outArray[], int outSize);
00725
00726
00727 #define chopLine(line, words) chopByWhite(line, words, ArraySize(words))
00728
00729
00730 int chopByChar(char *in, char chopper, char *outArray[], int outSize);
00731
00732
00733 #define chopTabs(string, words) chopByChar(string, '\t', words, ArraySize(words))
00734
00735
00736 #define chopCommas(string, words) chopByChar(string, ',', words, ArraySize(words))
00737
00738
00739
00740 char *skipLeadingSpaces(char *s);
00741
00742
00743 char *skipToSpaces(char *s);
00744
00745
00746 void eraseTrailingSpaces(char *s);
00747
00748
00749 void eraseWhiteSpace(char *s);
00750
00751
00752 char *trimSpaces(char *s);
00753
00754
00755 void spaceOut(FILE *f, int count);
00756
00757
00758 void starOut(FILE *f, int count);
00759
00760
00761 boolean hasWhiteSpace(char *s);
00762
00763
00764 char *firstWordInLine(char *line);
00765
00766
00767
00768 char *lastWordInLine(char *line);
00769
00770
00771
00772
00773 char *nextWord(char **pLine);
00774
00775
00776
00777 char *nextTabWord(char **pLine);
00778
00779
00780 int stringArrayIx(char *string, char *array[], int arraySize);
00781
00782
00783 int ptArrayIx(void *pt, void *array, int arraySize);
00784
00785
00786 #define stringIx(string, array) stringArrayIx( (string), (array), ArraySize(array))
00787
00788
00789 #ifndef SEEK_SET
00790 #define SEEK_SET 0
00791 #endif
00792
00793 #ifndef SEEK_CUR
00794 #define SEEK_CUR 1
00795 #endif
00796
00797 #ifndef SEEK_END
00798 #define SEEK_END 2
00799 #endif
00800
00801 void splitPath(char *path, char dir[256], char name[128], char extension[64]);
00802
00803
00804
00805
00806
00807 char *addSuffix(char *head, char *suffix);
00808
00809
00810
00811 void chopSuffix(char *s);
00812
00813
00814 void chopSuffixAt(char *s, char c);
00815
00816
00817
00818 char *chopPrefix(char *s);
00819
00820
00821
00822
00823
00824 char *chopPrefixAt(char *s, char c);
00825
00826
00827 FILE *mustOpen(char *fileName, char *mode);
00828
00829
00830 void mustWrite(FILE *file, void *buf, size_t size);
00831
00832
00833 #define writeOne(file, var) mustWrite((file), &(var), sizeof(var))
00834
00835
00836 void mustRead(FILE *file, void *buf, size_t size);
00837
00838
00839 #define mustReadOne(file, var) mustRead((file), &(var), sizeof(var))
00840
00841
00842 #define readOne(file, var) (fread(&(var), sizeof(var), 1, (file)) == 1)
00843
00844
00845 void writeString(FILE *f, char *s);
00846
00847
00848
00849
00850 char *readString(FILE *f);
00851
00852
00853
00854
00855 char *mustReadString(FILE *f);
00856
00857
00858 boolean fastReadString(FILE *f, char buf[256]);
00859
00860
00861
00862
00863 void writeBits64(FILE *f, bits64 x);
00864
00865
00866 bits64 readBits64(FILE *f);
00867
00868
00869 void carefulClose(FILE **pFile);
00870
00871
00872 boolean carefulCloseWarn(FILE **pFile);
00873
00874
00875
00876
00877 char *firstWordInFile(char *fileName, char *wordBuf, int wordBufSize);
00878
00879
00880
00881 int roundingScale(int a, int p, int q);
00882
00883
00884 int intAbs(int a);
00885
00886
00887 #define logBase2(x)(log(x)/log(2))
00888
00889
00890 #define round(a) ((int)((a)+0.5))
00891
00892
00893 #define roundll(a) ((long long)((a)+0.5))
00894
00895
00896 #ifndef min
00897 #define min(a,b) ( (a) < (b) ? (a) : (b) )
00898
00899 #endif
00900
00901 #ifndef max
00902 #define max(a,b) ( (a) > (b) ? (a) : (b) )
00903
00904 #endif
00905
00906 int rangeIntersection(int start1, int end1, int start2, int end2);
00907
00908
00909
00910 int positiveRangeIntersection(int start1, int end1, int start2, int end2);
00911
00912
00913
00914 bits32 byteSwap32(bits32 a);
00915
00916
00917 void removeReturns(char* dest, char* src);
00918
00919
00920
00921
00922 int intExp(char *text);
00923
00924
00925
00926 double doubleExp(char *text);
00927
00928
00929
00930 char* readLine(FILE* fh);
00931
00932
00933 off_t fileSize(char *fileName);
00934
00935
00936 boolean fileExists(char *fileName);
00937
00938
00939
00940
00941
00942 char *containsStringNoCase(char *haystack, char *needle);
00943
00944 char *strstrNoCase(char *haystack, char *needle);
00945
00946
00947 int vasafef(char* buffer, int bufSize, char *format, va_list args);
00948
00949
00950
00951 int safef(char* buffer, int bufSize, char *format, ...)
00952
00953
00954 #ifdef __GNUC__
00955 __attribute__((format(printf, 3, 4)))
00956 #endif
00957 ;
00958
00959 void safecpy(char *buf, size_t bufSize, const char *src);
00960
00961
00962 void safencpy(char *buf, size_t bufSize, const char *src, size_t n);
00963
00964
00965
00966 void safecat(char *buf, size_t bufSize, const char *src);
00967
00968
00969 void safencat(char *buf, size_t bufSize, const char *src, size_t n);
00970
00971
00972 char *naForNull(char *s);
00973
00974
00975 char *naForEmpty(char *s);
00976
00977
00978 char *emptyForNull(char *s);
00979
00980
00981 char *nullIfAllSpace(char *s);
00982
00983
00984 char *trueFalseString(boolean b);
00985
00986
00987 void uglyTime(char *label, ...);
00988
00989
00990
00991
00992 #if !defined(INFINITY)
00993 #define INFINITY (1.0/0.0)
00994 #endif
00995
00996 void makeDirs(char* path);
00997
00998
00999 char *skipNumeric(char *s);
01000
01001
01002 char *skipToNumeric(char *s);
01003
01004
01005 char *splitOffNonNumeric(char *s);
01006
01007
01008 char *splitOffNumber(char *db);
01009
01010
01011 #endif