| ... | ... |
@@ -42,8 +42,11 @@ field_quotedstr(char *ptr, char *str, long strsize) |
| 42 | 42 |
int flaglit; |
| 43 | 43 |
while(strchr(" \t",*ptr)!=NULL)
|
| 44 | 44 |
ptr++; |
| 45 |
- if(*ptr!='\"') |
|
| 45 |
+ if(*ptr!='\"') {
|
|
| 46 |
+ if(str!=NULL && strsize>0) |
|
| 47 |
+ *str='\0'; |
|
| 46 | 48 |
return(ptr); |
| 49 |
+ } |
|
| 47 | 50 |
/* skip initial '\"' */ |
| 48 | 51 |
ptr++; |
| 49 | 52 |
/* read the string unescaping as we go */ |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,101 @@ |
| 1 |
+/* |
|
| 2 |
+ * parselib.c |
|
| 3 |
+ * |
|
| 4 |
+ * Convenience functions for parsing config-file lines. |
|
| 5 |
+ * |
|
| 6 |
+ * History: |
|
| 7 |
+ * 14/03/2014 Creation |
|
| 8 |
+ * |
|
| 9 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
| 10 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
| 11 |
+ */ |
|
| 12 |
+ |
|
| 13 |
+#include <string.h> |
|
| 14 |
+#include "parselib.h" |
|
| 15 |
+ |
|
| 16 |
+char * |
|
| 17 |
+field_int(char *ptr, int *value) |
|
| 18 |
+{
|
|
| 19 |
+ *value=0; |
|
| 20 |
+ while(strchr(" \t",*ptr)!=NULL)
|
|
| 21 |
+ ptr++; |
|
| 22 |
+ for(;*ptr>='0' && *ptr<='9';ptr++) |
|
| 23 |
+ *value=(*value)*10+(*ptr-'0'); |
|
| 24 |
+ return(ptr); |
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+char * |
|
| 28 |
+field_long(char *ptr, long *value) |
|
| 29 |
+{
|
|
| 30 |
+ *value=0; |
|
| 31 |
+ while(strchr(" \t",*ptr)!=NULL)
|
|
| 32 |
+ ptr++; |
|
| 33 |
+ for(;*ptr>='0' && *ptr<='9';ptr++) |
|
| 34 |
+ *value=(*value)*10+(*ptr-'0'); |
|
| 35 |
+ return(ptr); |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+char * |
|
| 39 |
+field_quotedstr(char *ptr, char *str, long strsize) |
|
| 40 |
+{
|
|
| 41 |
+ char *dest; |
|
| 42 |
+ int flaglit; |
|
| 43 |
+ while(strchr(" \t",*ptr)!=NULL)
|
|
| 44 |
+ ptr++; |
|
| 45 |
+ if(*ptr!='\"') |
|
| 46 |
+ return(ptr); |
|
| 47 |
+ /* skip initial '\"' */ |
|
| 48 |
+ ptr++; |
|
| 49 |
+ /* read the string unescaping as we go */ |
|
| 50 |
+ for(dest=str,flaglit=0;*ptr!='\0' && !(!flaglit && *ptr=='\"');ptr++) {
|
|
| 51 |
+ if(flaglit==0 && *ptr=='\\') {
|
|
| 52 |
+ flaglit=1; |
|
| 53 |
+ continue; |
|
| 54 |
+ } |
|
| 55 |
+ flaglit=0; |
|
| 56 |
+ if(str!=NULL && (dest-str)<strsize) |
|
| 57 |
+ *(dest++)=*ptr; |
|
| 58 |
+ } |
|
| 59 |
+ if((dest-str)<strsize) |
|
| 60 |
+ *(dest++)='\0'; |
|
| 61 |
+ else if(str!=NULL && strsize>0) |
|
| 62 |
+ str[strsize-1]='\0'; |
|
| 63 |
+ /* skip the final '\"' */ |
|
| 64 |
+ if(*ptr=='\"') |
|
| 65 |
+ ptr++; |
|
| 66 |
+ return(ptr); |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+char * |
|
| 70 |
+field_unquotedstr(char *ptr, char *str, long strsize, char *separators) |
|
| 71 |
+{
|
|
| 72 |
+ char *dest; |
|
| 73 |
+ while(strchr(" \t",*ptr)!=NULL)
|
|
| 74 |
+ ptr++; |
|
| 75 |
+ /* read the string */ |
|
| 76 |
+ for(dest=str;*ptr!='\0' && strchr(separators,*ptr)==NULL;ptr++) {
|
|
| 77 |
+ if(str!=NULL && (dest-str)<strsize) |
|
| 78 |
+ *(dest++)=*ptr; |
|
| 79 |
+ } |
|
| 80 |
+ if((dest-str)<strsize) |
|
| 81 |
+ *(dest++)='\0'; |
|
| 82 |
+ else if(str!=NULL && strsize>0) |
|
| 83 |
+ str[strsize-1]='\0'; |
|
| 84 |
+ return(ptr); |
|
| 85 |
+} |
|
| 86 |
+ |
|
| 87 |
+char * |
|
| 88 |
+field_hexint(char *ptr, int *value) |
|
| 89 |
+{
|
|
| 90 |
+ *value=0; |
|
| 91 |
+ while(strchr(" \t",*ptr)!=NULL)
|
|
| 92 |
+ ptr++; |
|
| 93 |
+ for(;(*ptr>='0' && *ptr<='9')||(*ptr>='a' && *ptr<='f')||(*ptr>='A' && *ptr<='F');ptr++) {
|
|
| 94 |
+ *value=((*value)*16)+ |
|
| 95 |
+ (*ptr>='a' && *ptr<='f')?*ptr-'a'+10: |
|
| 96 |
+ (*ptr>='A' && *ptr<='F')?*ptr-'A'+10: |
|
| 97 |
+ *ptr-'0'; |
|
| 98 |
+ } |
|
| 99 |
+ return(ptr); |
|
| 100 |
+} |
|
| 101 |
+ |