** Returns an array with one entry per mapfile token. Useful to manipulate ** mapfiles in MapScript. ** ** The returned array should be freed using msFreeCharArray(). */
| 6027 | ** The returned array should be freed using msFreeCharArray(). |
| 6028 | */ |
| 6029 | static char **tokenizeMapInternal(char *filename, int *ret_numtokens) |
| 6030 | { |
| 6031 | char **tokens = NULL; |
| 6032 | int numtokens=0, numtokens_allocated=0; |
| 6033 | size_t buffer_size = 0; |
| 6034 | |
| 6035 | *ret_numtokens = 0; |
| 6036 | |
| 6037 | if(!filename) { |
| 6038 | msSetError(MS_MISCERR, "Filename is undefined.", "msTokenizeMap()"); |
| 6039 | return NULL; |
| 6040 | } |
| 6041 | |
| 6042 | /* |
| 6043 | ** Check map filename to make sure it's legal |
| 6044 | */ |
| 6045 | if(getenv("MS_MAPFILE_PATTERN")) { /* user override */ |
| 6046 | if(msEvalRegex(getenv("MS_MAPFILE_PATTERN"), filename) != MS_TRUE) { |
| 6047 | msSetError(MS_REGEXERR, "MS_MAPFILE_PATTERN validation failed." , "msLoadMap()"); |
| 6048 | return(NULL); |
| 6049 | } |
| 6050 | } else { /* check the default */ |
| 6051 | if(msEvalRegex(MS_DEFAULT_MAPFILE_PATTERN, filename) != MS_TRUE) { |
| 6052 | msSetError(MS_REGEXERR, "MS_DEFAULT_MAPFILE_PATTERN validation failed." , "msLoadMap()"); |
| 6053 | return(NULL); |
| 6054 | } |
| 6055 | } |
| 6056 | |
| 6057 | if((msyyin = fopen(filename,"r")) == NULL) { |
| 6058 | msSetError(MS_IOERR, "(%s)", "msTokenizeMap()", filename); |
| 6059 | return NULL; |
| 6060 | } |
| 6061 | |
| 6062 | msyystate = MS_TOKENIZE_FILE; /* restore lexer state to INITIAL, and do return comments */ |
| 6063 | msyylex(); |
| 6064 | msyyreturncomments = 1; /* want all tokens, including comments */ |
| 6065 | |
| 6066 | msyyrestart(msyyin); /* start at line begining, line 1 */ |
| 6067 | msyylineno = 1; |
| 6068 | |
| 6069 | numtokens = 0; |
| 6070 | numtokens_allocated = 256; |
| 6071 | tokens = (char **) malloc(numtokens_allocated*sizeof(char*)); |
| 6072 | if(tokens == NULL) { |
| 6073 | msSetError(MS_MEMERR, NULL, "msTokenizeMap()"); |
| 6074 | fclose(msyyin); |
| 6075 | return NULL; |
| 6076 | } |
| 6077 | |
| 6078 | for(;;) { |
| 6079 | |
| 6080 | if(numtokens_allocated <= numtokens) { |
| 6081 | numtokens_allocated *= 2; /* double size of the array every time we reach the limit */ |
| 6082 | tokens = (char **)realloc(tokens, numtokens_allocated*sizeof(char*)); |
| 6083 | if(tokens == NULL) { |
| 6084 | msSetError(MS_MEMERR, "Realloc() error.", "msTokenizeMap()"); |
| 6085 | fclose(msyyin); |
| 6086 | return NULL; |
no test coverage detected