| 1 | #ifndef __COMMON_H | 
|---|
| 2 | #define __COMMON_H | 
|---|
| 3 |  | 
|---|
| 4 | #include <stdio.h> | 
|---|
| 5 |  | 
|---|
| 6 | /************************************************** | 
|---|
| 7 |  * Stale dotyczace wejscia/wyjscia | 
|---|
| 8 |  */ | 
|---|
| 9 |  | 
|---|
| 10 | #define MAXLINE 1024 | 
|---|
| 11 |  | 
|---|
| 12 | #define EMPTYFORM '*' | 
|---|
| 13 | #define INFIELD_SEP ':' | 
|---|
| 14 | #define MAXAUX 16 | 
|---|
| 15 | #define FIELD_SEP " \t\n" | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | /***************************************************************/ | 
|---|
| 19 | /* problems with casing                                        */ | 
|---|
| 20 | /* sprawdzenie wielkosci liter                                 */ | 
|---|
| 21 | /* warto¶æ zwracana:                                           */ | 
|---|
| 22 | /* 0 - wszystkie ma³e litery, 1 - pierwsza wielka, reszta male */ | 
|---|
| 23 | /* 2 - wszystkie wielkie, 3 - inne                             */ | 
|---|
| 24 | /***************************************************************/ | 
|---|
| 25 | inline int casing(char* s) | 
|---|
| 26 | { | 
|---|
| 27 |   int ret = isupper(*s) ? 1 : 0; | 
|---|
| 28 |   while(*++s != '\0') | 
|---|
| 29 |   { | 
|---|
| 30 |     if(isupper(*s)) | 
|---|
| 31 |     { | 
|---|
| 32 |       if(ret==1) ret=2; | 
|---|
| 33 |       else if(ret==0) ret=3; | 
|---|
| 34 |     } | 
|---|
| 35 |     else | 
|---|
| 36 |     { | 
|---|
| 37 |       if(ret==2) ret=3; | 
|---|
| 38 |     } | 
|---|
| 39 |   } | 
|---|
| 40 |   return ret; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //  | 
|---|
| 44 | inline void tolowers(char* s, char* d) | 
|---|
| 45 | { | 
|---|
| 46 |   *d=tolower(*s); | 
|---|
| 47 |   while(*s != '\0') * ++d = tolower(* ++s); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | // przepisuje s do d | 
|---|
| 52 | // nadajac wielko¶æ liter zgodnie z warto¶ci± casing | 
|---|
| 53 | // casing - warto¶æ zwracana przez casing() | 
|---|
| 54 | // je¶li casing==3 przepisuje bez zmian (za ma³o informacji) | 
|---|
| 55 | inline void restorecasing(char *s, char *d, int casing) | 
|---|
| 56 | { | 
|---|
| 57 |   switch(casing) | 
|---|
| 58 |   { | 
|---|
| 59 |   case 0: | 
|---|
| 60 |   case 3: | 
|---|
| 61 |     *d=*s; | 
|---|
| 62 |     while(*s != '\0') * ++d = * ++s; | 
|---|
| 63 |     break; | 
|---|
| 64 |   case 1: | 
|---|
| 65 |     *d=toupper(*s); | 
|---|
| 66 |     while(*s != '\0') * ++d = * ++s; | 
|---|
| 67 |     break; | 
|---|
| 68 |   case 2: | 
|---|
| 69 |     *d=toupper(*s); | 
|---|
| 70 |     while(*s != '\0') * ++d = toupper(* ++s); | 
|---|
| 71 |     break; | 
|---|
| 72 |   } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | /**************************************************/ | 
|---|
| 77 | /* | 
|---|
| 78 | parameters: | 
|---|
| 79 |   -seg  - segment | 
|---|
| 80 |   -name - field name | 
|---|
| 81 |   +val  - field contents | 
|---|
| 82 | return value: | 
|---|
| 83 |   1 if specified field exists, 0 otherwise | 
|---|
| 84 | */ | 
|---|
| 85 |  | 
|---|
| 86 | inline int getfield(char* seg, const char* pref, char* val) | 
|---|
| 87 | { | 
|---|
| 88 |   char* p=seg; | 
|---|
| 89 |  | 
|---|
| 90 |   while(isspace(*p)) ++p; | 
|---|
| 91 |    | 
|---|
| 92 |  pos: | 
|---|
| 93 |   if(isdigit(*p) or *p=='*') while(!isspace(*p)) ++p;  | 
|---|
| 94 |   else goto type; | 
|---|
| 95 |  | 
|---|
| 96 |   while(isspace(*p)) ++p; | 
|---|
| 97 |    | 
|---|
| 98 |  len: | 
|---|
| 99 |   if(isdigit(*p) or *p=='*') while(!isspace(*p)) ++p;  | 
|---|
| 100 |   else goto type; | 
|---|
| 101 |  | 
|---|
| 102 |   while(isspace(*p)) ++p; | 
|---|
| 103 |    | 
|---|
| 104 |  type: | 
|---|
| 105 |   while(isspace(*p)) ++p; while(!isspace(*p)) ++p; | 
|---|
| 106 |  | 
|---|
| 107 |   while(isspace(*p)) ++p; | 
|---|
| 108 |  | 
|---|
| 109 |  form: | 
|---|
| 110 |   while(isspace(*p)) ++p; while(!isspace(*p)) ++p; | 
|---|
| 111 |  | 
|---|
| 112 |  annotation: | 
|---|
| 113 |   do p=strstr(p,pref); while(p!=NULL && *(p-1)!=' ' && *(p-1)!='\t'); | 
|---|
| 114 |    | 
|---|
| 115 |   if(p==NULL) return 0; | 
|---|
| 116 |   else | 
|---|
| 117 |   { | 
|---|
| 118 |     p+=strlen(pref); | 
|---|
| 119 |     int len=strcspn(p,FIELD_SEP "\n\r\f\0"); | 
|---|
| 120 |     strncpy(val,p,len); | 
|---|
| 121 |     val[len]='\0'; | 
|---|
| 122 |     return 1; | 
|---|
| 123 |   } | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 | /* | 
|---|
| 128 | parameters: | 
|---|
| 129 |   +seg - segment | 
|---|
| 130 |   -pref - prefix of the new field | 
|---|
| 131 |   -val  - contents of the new field | 
|---|
| 132 | return value: | 
|---|
| 133 |   1 - success, 0 - fail (limit on segment length exceeded) | 
|---|
| 134 | */ | 
|---|
| 135 | inline int addfield(char *seg, const char *pref, const char *val) | 
|---|
| 136 |      // zalozenie, ze seg konczy sie znakiem \n | 
|---|
| 137 | { | 
|---|
| 138 |   if(strlen(seg)+strlen(pref)+strlen(val) >= MAXLINE) return 0; // bezpieczniej, ale wolniej | 
|---|
| 139 |  | 
|---|
| 140 |   int seglen=strlen(seg); | 
|---|
| 141 |   sprintf(seg+(seglen-1)," %s%s\n",pref,val); | 
|---|
| 142 |   return 1; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | #endif | 
|---|