From fd289dd5b673abe5aa0ee3fda961ac50071af60e Mon Sep 17 00:00:00 2001 From: M.Gergő Date: Sun, 17 May 2020 01:16:40 +0200 Subject: backport c89 --- .vscode/c_cpp_properties.json | 45 ++++++++++++++++++ src/ini_read.c | 103 +++++++++++++++++++++++++++--------------- src/ini_rw.h | 75 ------------------------------ src/inirw_internal.h | 79 ++++++++++++++++++++++++++++++++ src/main.c | 2 +- 5 files changed, 191 insertions(+), 113 deletions(-) create mode 100644 .vscode/c_cpp_properties.json delete mode 100644 src/ini_rw.h create mode 100644 src/inirw_internal.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..dcec1f6 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,45 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c89", + "cppStandard": "c++98", + "intelliSenseMode": "gcc-x64", + "compilerArgs": [], + "browse": { + "path": [ + "${workspaceFolder}/**" + ], + "limitSymbolsToIncludedHeaders": true + } + }, + { + "name": "win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu11", + "cppStandard": "gnu++14", + "intelliSenseMode": "clang-x64" + }, + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu11", + "cppStandard": "gnu++14", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/ini_read.c b/src/ini_read.c index 40db102..bbfb68e 100644 --- a/src/ini_read.c +++ b/src/ini_read.c @@ -1,33 +1,26 @@ -/*INI fájl olvasás */ +/* INI fájl olvasás */ #include -#include /* strncpy + strerror*/ -#include /* malloc(), atoi(), exit(EXIT_FALIURE)*/ -#include /*, isspace(), tolower() */ -#include /*errno*/ +#include /* strncpy + strerror */ +#include /* malloc(), atoi(), exit(EXIT_FALIURE) */ +#include /* errno */ #include -#include /* int64_t*/ +#include /* int64_t */ #define ini_read_c -#include "ini_rw.h" +#include "inirw_internal.h" -/* -enum ini_states state = Start; -struct lci_data fsmdata; -*/ - size_t strNullLen(const char *str){ if(str == NULL){ return 0; } else { return strlen(str); - } + } } char *strResize(char *ptr, size_t oldsize, size_t newsize){ - /*return ptr;*/ char *tmp; if(newsize <= 0){ /* deleting */ @@ -60,7 +53,7 @@ char *strResize(char *ptr, size_t oldsize, size_t newsize){ } } -char unescape(char c){ +int unescape(int c){ if(c == 'n'){ /* Newline */ return '\n'; } else if(c == 'a'){ /* Bell */ @@ -84,7 +77,7 @@ char unescape(char c){ } } -char eescape(char c){ +int eescape(int c){ if(c == '\n'){ /* Newline */ return 'n'; } else if(c == '\a'){ /* Bell */ @@ -111,6 +104,41 @@ char eescape(char c){ } +int isascalnum(int c){ /* Check if input is ASCII Alpha-numeric */ + + if( 0x30 <= c && c <= 0x39){ /* Numeric */ + return 1; + } else if (0x41 <= c && c <= 0x5a){ /* UPPER */ + return 1; + } else if( 0x61 <= c && c <= 0x7a){ /* lower */ + return 1; + } else { + return 0; + } +} + +int checkspace(int c){ /* Only for ASCII characters */ + + switch (c) { + case 0x20: /* space (SPC) */ + return 1; + case 0x09: /* horizontal tab (TAB) */ + return 1; + case 0x0A: /* newline (LF) */ + return 1; + case 0x0B: /* vertical tab (VT) */ + return 1; + case 0x0C: /* feed (FF) */ + return 1; + case 0x0d: /* carrige return (CR) */ + return 1; + default: + return 0; + } +} + + + size_t getFileMaxLineLen(FILE *tfd){ size_t c=0; @@ -136,18 +164,18 @@ size_t getFileMaxLineLen(FILE *tfd){ } } -struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ +struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t len){ int32_t i,j, vallen=len; enum ini_states pstate=Start, state=Start; - char cc; + /*char cc; */ if(data == NULL){ return NULL; } else { for(i=0, j=0; isection = strResize(data->section, data->sectionLen, len); memset(data->section, 0, len); data->sectionStartPos = i; /* Brackets [] are counted! */ - } else if(isalnum(in[i]) ){ /* Parameter is starting */ + } else if(isascalnum(in[i]) ){ /* Parameter is starting */ j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); @@ -216,14 +244,14 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ data->section = strResize(data->section, data->sectionLen, len); memset(data->section, 0, len); data->sectionStartPos = i; - } else if (isalnum(in[i])){ /* Parameter will be */ + } else if (isascalnum(in[i])){ /* Parameter will be */ j = -1; state = EqW1; data->param = strResize(data->param, data->paramLen, len); memset(data->param, 0, len); data->paramStartPos = i; i--; - } else if(isspace(in[i]) /*|| in[i] == '\0' */ ){ + } else if(checkspace(in[i]) /*|| in[i] == '\0' */ ){ state = BgnSp; } else { state = Error; @@ -255,7 +283,7 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ if(j == 0){ /* empty section */ state = Error; } - } else if(isalnum(in[i]) || in[i]=='/' || in[i]==' ' || in[i]=='_' || in[i]=='-' || in[i]=='.'){ + } else if(isascalnum(in[i]) || in[i]=='/' || in[i]==' ' || in[i]=='_' || in[i]=='-' || in[i]=='.'){ data->section[j] = in[i]; } else /*if((in[i] >= 0x00 && in[i] < 0x20) || in[i] == ';' || in[i] == '#' )*/ { /* túl korai sorvég, SP(), vagy komment */ state = Error; @@ -274,7 +302,7 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ state = Stop; data->nodeState = lci_READY; i--; - }else if(isspace(in[i])){ + }else if(checkspace(in[i])){ state = SectEndD; /* remain here */ } else if (in[i] == ';' || in[i] == '#'){ j = -1; @@ -297,9 +325,9 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ state = ValPSP; data->param[j] = '\0'; data->paramLen = j + 1; - } else if(isalnum(in[i]) || in[i]=='[' || in[i]==']' || in[i]=='_' || in[i]=='-' || in[i]=='.'){ /* Arrays are supported ins parameter name, like in PHP */ + } else if(isascalnum(in[i]) || in[i]=='[' || in[i]==']' || in[i]=='_' || in[i]=='-' || in[i]=='.'){ /* Arrays are supported ins parameter name, like in PHP */ data->param[j] = in[i]; - } else if(isspace(in[i]) && in[i] != '\r' && in[i] != '\n'){ /* Only SPACE allowed between Param_name and EQ_sign */ + } else if(checkspace(in[i]) && in[i] != '\r' && in[i] != '\n'){ /* Only SPACE allowed between Param_name and EQ_sign */ state = EqW2; data->param[j] = '\0'; data->paramLen = j + 1; @@ -315,7 +343,7 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ if(in[i] == '='){ /* EQ_sign arrived */ j = -1; state = ValPSP; - } else if(isspace(in[i])){ /* Another space, remain here */ + } else if(checkspace(in[i])){ /* Another space, remain here */ state = EqW2; } else { state = Error; @@ -349,9 +377,9 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ data->valueStartPos = i; data->nodeState = lci_CONTINUE; data->valueDraw = lci_DQUOTEDVAL; - } else if(isspace(in[i])){ /* Another SP(), remain here */ + } else if(checkspace(in[i])){ /* Another SP(), remain here */ state = ValPSP; - } else if(isalnum(in[i]) || in[i]=='-') { /* Normal_Value collector */ + } else if(isascalnum(in[i]) || in[i]=='-') { /* Normal_Value collector */ j = -1; state = ValW; data->value = strResize(data->value, data->valueLen, len); @@ -389,9 +417,9 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ /* } else if( in[i] == '\\' ){ // Backslash support // j--; //A '\' nem számít bele! // state = Bslsh; */ - } else if( isalnum(in[i]) || in[i]=='_' || in[i]=='-' || in[i]=='.' ){ /* Regular characters without SP() */ + } else if(isascalnum(in[i]) || in[i]=='_' || in[i]=='-' || in[i]=='.' ){ /* Regular characters without SP() */ data->value[j] = in[i]; - } else if(isspace(in[i])){ /* SPACE arrived -> line_end */ + } else if(checkspace(in[i])){ /* SPACE arrived -> line_end */ data->value[j] = '\0'; data->valueLen = j+1; data->nodeState = lci_READY; @@ -444,7 +472,7 @@ struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len){ data->commentStartPos = i; data->commentSign = in[i]; data->nodeState = lci_CONTINUE; - } else if(isspace(in[i])){ + } else if(checkspace(in[i])){ state = ValFSP; /* SP() -> ermain here */ } else { state = Error; @@ -597,10 +625,11 @@ lci_data *destroyNodes( lci_data *head){ /* Destroys Nodes from HEAD to the end struct lci_data *iniReadOut(const char *filename){ - int c=0, *buff; + int c=0; + unsigned char *buff; FILE *fp=NULL; int64_t linemax, line=0, pos=0; - char cc; + /*char cc;*/ struct lci_data *prev=NULL, *curr=NULL, *list = NULL; fp = fopen(filename, "rb"); @@ -612,12 +641,12 @@ struct lci_data *iniReadOut(const char *filename){ } else { linemax = getFileMaxLineLen(fp) +1; - buff = (int *) malloc(linemax*sizeof(int)); + buff = (unsigned char *) malloc(linemax*sizeof(char)); memset(buff, 0, linemax); while( c != EOF){ c = fgetc(fp); - cc = c; + /*cc = c;*/ /* debug */ if( c == '\n' || c == EOF){ line++; diff --git a/src/ini_rw.h b/src/ini_rw.h deleted file mode 100644 index 7894eef..0000000 --- a/src/ini_rw.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef INI_READ_H_INCLUDED -#define INI_READ_H_INCLUDED -/* #include "deftypes.h" */ -#include /* int64_t*/ - - - -typedef struct lci_data { - - enum nodeState {lci_EMPTY, lci_READY, lci_CONTINUE, lci_MULTILINE, lci_ERROR } nodeState; - int32_t lineNum; - int32_t lineLen; - - char *section; - int32_t sectionLen; - int32_t sectionStartPos; - char *param; - int32_t paramLen; - int32_t paramStartPos; - char *value; - int32_t valueLen; - int32_t valueStartPos; - enum valueDraw {lci_EMPTYVAL, lci_SIMPLEVAL, lci_MULTILINEVAL, lci_DQUOTEDVAL} valueDraw; - char *comment; - int32_t commentLen; - int32_t commentStartPos; - char commentSign; - char *errorMsg; - int32_t errorMsgLen; - - struct lci_data *next; -} lci_data; - - - -struct lci_data *iniReadOut(const char *filename); -/* int64_t getFileMaxLineLen(FILE *tfd); */ -char *strResize(char *ptr, size_t oldsize, size_t newsize); -lci_data *destroyNodes( lci_data *head); -lci_data *createNode( lci_data *head, int64_t lineLen ); -size_t getFileMaxLineLen(FILE *tfd); - - -#if defined(ini_read_c) || defined(ini_write_c) -enum ini_states {Start, BgnSp, CommEndW, SectEndW, SectEndD, EqW1, EqW2, ValPSP, ValW, ValFSP, DqmW, Bslsh, Error, Stop }; -size_t strNullLen(const char *str); -struct lci_data *iniFSM(struct lci_data *data, const int *in, int32_t len); -char eescape(char c); - -#ifdef ini_read_c -char unescape(char c); -#endif /* ini_read_c */ -#ifdef ini_write_c -//static const char* komment = ";#"; -#endif /*ini_write_c*/ -#endif /* ini_read_c, ini_write_c*/ - - - -char *lciGETtoStr( const char *section, const char *param, char *dest, size_t dstlen ); -/*int lciGETtoStrlen(const char *section, const char *param, ...); */ -int8_t lciGETtoInt8( const char *filename, const char *section, const char *param); -int16_t lciGETtoInt16(const char *filename, const char *section, const char *param); -int32_t lciGETtoInt32(const char *filename, const char *section, const char *param); -int64_t lciGETtoInt64(const char *filename, const char *section, const char *param); - -double lciGETtoDlb(const char *filename, const char *section, const char *param); -float lciGETtoFlt(const char *filename, const char *section, const char *param); -long int lciGETtoLng(const char *filename, const char *section, const char *param); - - - - -#endif /* INI_READ_H_INCLUDED */ - diff --git a/src/inirw_internal.h b/src/inirw_internal.h new file mode 100644 index 0000000..a75a70b --- /dev/null +++ b/src/inirw_internal.h @@ -0,0 +1,79 @@ +#ifndef INI_READ_H_INCLUDED +#define INI_READ_H_INCLUDED +/* #include "deftypes.h" */ +#include /* int64_t*/ + + + +typedef struct lci_data { + + enum nodeState {lci_EMPTY, lci_READY, lci_CONTINUE, lci_MULTILINE, lci_ERROR } nodeState; + int32_t lineNum; + int32_t lineLen; + + char *section; + int32_t sectionLen; + int32_t sectionStartPos; + char *param; + int32_t paramLen; + int32_t paramStartPos; + char *value; + int32_t valueLen; + int32_t valueStartPos; + enum valueDraw {lci_EMPTYVAL, lci_SIMPLEVAL, lci_MULTILINEVAL, lci_DQUOTEDVAL} valueDraw; + char *comment; + int32_t commentLen; + int32_t commentStartPos; + char commentSign; + char *errorMsg; + int32_t errorMsgLen; + + struct lci_data *next; +} lci_data; + + + +struct lci_data *iniReadOut(const char *filename); +/* int64_t getFileMaxLineLen(FILE *tfd); */ +char *strResize(char *ptr, size_t oldsize, size_t newsize); +lci_data *destroyNodes( lci_data *head); +lci_data *createNode( lci_data *head, int64_t lineLen ); +size_t getFileMaxLineLen(FILE *tfd); + + +#if defined(ini_read_c) || defined(ini_write_c) +enum ini_states {Start, BgnSp, CommEndW, SectEndW, SectEndD, EqW1, EqW2, ValPSP, ValW, ValFSP, DqmW, Bslsh, Error, Stop }; +size_t strNullLen(const char *str); +struct lci_data *iniFSM(struct lci_data *data, const unsigned char *in, int32_t len); +int eescape(int c); +int isascalnum(int c); /* Check if input is ASCII Alpha-numeric */ +int checkspace(int c); /* Only for ASCII characters */ + + + +#ifdef ini_read_c +int unescape(int c); +#endif /* ini_read_c */ +#ifdef ini_write_c +//static const char* komment = ";#"; +#endif /*ini_write_c*/ +#endif /* ini_read_c, ini_write_c*/ + + + +char *lciGETtoStr( const char *section, const char *param, char *dest, size_t dstlen ); +/*int lciGETtoStrlen(const char *section, const char *param, ...); */ +int8_t lciGETtoInt8( const char *filename, const char *section, const char *param); +int16_t lciGETtoInt16(const char *filename, const char *section, const char *param); +int32_t lciGETtoInt32(const char *filename, const char *section, const char *param); +int64_t lciGETtoInt64(const char *filename, const char *section, const char *param); + +double lciGETtoDlb(const char *filename, const char *section, const char *param); +float lciGETtoFlt(const char *filename, const char *section, const char *param); +long int lciGETtoLng(const char *filename, const char *section, const char *param); + + + + +#endif /* INI_READ_H_INCLUDED */ + diff --git a/src/main.c b/src/main.c index 0906321..37ed4f5 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ #include #include -#include "ini_rw.h" +#include "inirw_internal.h" #define main_c -- cgit v1.2.3