[mapserver-commits] r10751 - sandbox/sdlime/common-expressions/mapserver

svn at osgeo.org svn at osgeo.org
Mon Nov 22 00:51:59 EST 2010


Author: sdlime
Date: 2010-11-21 21:51:59 -0800 (Sun, 21 Nov 2010)
New Revision: 10751

Modified:
   sandbox/sdlime/common-expressions/mapserver/mapfile.c
   sandbox/sdlime/common-expressions/mapserver/maplayer.c
   sandbox/sdlime/common-expressions/mapserver/mapparser.c
   sandbox/sdlime/common-expressions/mapserver/mapparser.y
   sandbox/sdlime/common-expressions/mapserver/mapserver.h
   sandbox/sdlime/common-expressions/mapserver/maputil.c
Log:
Switched token list from a pre-allocated array to a linked list.

Modified: sandbox/sdlime/common-expressions/mapserver/mapfile.c
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/mapfile.c	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/mapfile.c	2010-11-22 05:51:59 UTC (rev 10751)
@@ -1867,36 +1867,47 @@
   exp->string = NULL;
   exp->compiled = MS_FALSE;
   exp->flags = 0;
-  exp->numtokens = 0;
+
+  exp->tokens = exp->curtoken = NULL;
 }
 
 void freeExpression(expressionObj *exp)
 {
-  int i=0;
+  tokenListNodeObjPtr node = NULL;
+  tokenListNodeObjPtr nextNode = NULL;
 
   if(!exp) return;
 
   msFree(exp->string);
   if((exp->type == MS_REGEX) && exp->compiled) ms_regfree(&(exp->regex));
 
-  for(i=0; i<exp->numtokens; i++) {
-    switch(exp->tokens[i].token) {
-    case MS_TOKEN_BINDING_DOUBLE:
-    case MS_TOKEN_BINDING_INTEGER:
-    case MS_TOKEN_BINDING_STRING:
-    case MS_TOKEN_BINDING_TIME:
-      msFree(exp->tokens[i].tokenval.bindval.item);
-      break;
-    case MS_TOKEN_LITERAL_TIME:
-      /* anything to do? */
-      break;
-    case MS_TOKEN_LITERAL_STRING:
-      msFree(exp->tokens[i].tokenval.strval);
-      break;
-    case MS_TOKEN_LITERAL_SHAPE:
-      msFreeShape(exp->tokens[i].tokenval.shpval);
-      free(exp->tokens[i].tokenval.shpval);
-      break;
+  // HERE
+  if(exp->tokens) {
+    node = exp->tokens;
+    while (node != NULL) {
+      nextNode = node->next;
+
+      switch(node->token) {
+      case MS_TOKEN_BINDING_DOUBLE:
+      case MS_TOKEN_BINDING_INTEGER:
+      case MS_TOKEN_BINDING_STRING:
+      case MS_TOKEN_BINDING_TIME:
+        msFree(node->tokenval.bindval.item);
+        break;
+      case MS_TOKEN_LITERAL_TIME:
+        /* anything to do? */
+        break;
+      case MS_TOKEN_LITERAL_STRING:
+        msFree(node->tokenval.strval);
+        break;
+      case MS_TOKEN_LITERAL_SHAPE:
+        msFreeShape(node->tokenval.shpval);
+        free(node->tokenval.shpval);
+        break;
+      }
+
+      msFree(node);
+      node = nextNode;
     }
   }
 

Modified: sandbox/sdlime/common-expressions/mapserver/maplayer.c
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/maplayer.c	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/maplayer.c	2010-11-22 05:51:59 UTC (rev 10751)
@@ -348,7 +348,7 @@
 
 int msTokenizeExpression(expressionObj *expression, char **list, int *listsize)
 {
-  int n=0;
+  tokenListNodeObjPtr node;
   int token;
 
   /* TODO: make sure the constants can't somehow reference invalid expression types */
@@ -361,23 +361,31 @@
   // fprintf(stderr, "string is %s\n", msyystring);
 
   while((token = msyylex()) != 0) { /* keep processing tokens until the end of the string (\0) */
-    // fprintf(stderr, "%d: %d >> %s <<\n", n, token, msyytext);
+    // fprintf(stderr, "%d >> %s <<\n", token, msyytext);
 
+    if((node = (tokenListNodeObjPtr) malloc(sizeof(tokenListNodeObj))) == NULL) {
+      msSetError(MS_MEMERR, NULL, "msTokenizeExpression()");
+      goto parse_error;
+    }
+
+    node->tailifhead = NULL;
+    node->next = NULL;
+
     switch(token) {
     case MS_TOKEN_LITERAL_NUMBER:
-      expression->tokens[n].token = token;
-      expression->tokens[n].tokenval.dblval = msyynumber;
+      node->token = token;
+      node->tokenval.dblval = msyynumber;
       break;
     case MS_TOKEN_LITERAL_STRING:
-      expression->tokens[n].token = token;
+      node->token = token;
       // fprintf(stderr, "    token value is: %s\n", msyytext_esc);
-      expression->tokens[n].tokenval.strval = strdup(msyytext_esc);
+      node->tokenval.strval = strdup(msyytext_esc);
       free(msyytext_esc); msyytext_esc=NULL;
       break;
     case MS_TOKEN_LITERAL_TIME:
-      expression->tokens[n].token = token;
-      msTimeInit(&(expression->tokens[n].tokenval.tmval));
-      if(msParseTime(msyytext, &(expression->tokens[n].tokenval.tmval)) != MS_TRUE) {
+      node->token = token;
+      msTimeInit(&(node->tokenval.tmval));
+      if(msParseTime(msyytext, &(node->tokenval.tmval)) != MS_TRUE) {
         msSetError(MS_PARSEERR, "Parsing time value failed.", "msTokenizeExpression()");
         goto parse_error;
       }
@@ -386,12 +394,12 @@
     case MS_TOKEN_BINDING_INTEGER:
     case MS_TOKEN_BINDING_STRING:
     case MS_TOKEN_BINDING_TIME: 
-      expression->tokens[n].token = token; /* binding type */
-      expression->tokens[n].tokenval.bindval.item = strdup(msyytext);
-      if(list) expression->tokens[n].tokenval.bindval.index = string2list(list, listsize, msyytext);
+      node->token = token; /* binding type */
+      node->tokenval.bindval.item = strdup(msyytext);
+      if(list) node->tokenval.bindval.index = string2list(list, listsize, msyytext);
       break;
     case MS_TOKEN_BINDING_SHAPE:
-      expression->tokens[n].token = token;
+      node->token = token;
       break;
     case MS_TOKEN_FUNCTION_FROMTEXT: /* we want to process a shape from WKT once and not for every feature being evaluated */
       if((token = msyylex()) != 40) { /* ( */
@@ -404,11 +412,13 @@
         goto parse_error;
       }
 
-      expression->tokens[n].token = MS_TOKEN_LITERAL_SHAPE;
-      expression->tokens[n].tokenval.shpval = msShapeFromWKT(msyytext_esc);
+      // fprintf(stderr, "\tWKT=%s\n", msyytext_esc);
+
+      node->token = MS_TOKEN_LITERAL_SHAPE;
+      node->tokenval.shpval = msShapeFromWKT(msyytext_esc);
       free(msyytext_esc);
 
-      if(!expression->tokens[n].tokenval.shpval) {
+      if(!node->tokenval.shpval) {
         msSetError(MS_PARSEERR, "Parsing fromText function failed, WKT processing failed.", "msTokenizeExpression()");
         goto parse_error;
       }
@@ -421,14 +431,25 @@
       }
       break;
     default:
-      expression->tokens[n].token = token; /* for everything else */
+      node->token = token; /* for everything else */
       break;
     }
 
-    n++;
+    /* add node to token list */
+    if(expression->tokens == NULL) {
+      expression->tokens = node;
+    } else {
+      if(expression->tokens->tailifhead != NULL) /* this should never be NULL, but just in case */
+	expression->tokens->tailifhead->next = node; /* put the node at the end of the list */
+    }
+
+    /* repoint the head of the list to the end  - our new element                                                                                                   
+       this causes a loop if we are at the head, be careful not to                                                                                                  
+       walk in a loop */
+    expression->tokens->tailifhead = node;
   }
 
-  expression->numtokens = n;
+  expression->curtoken = expression->tokens; /* point at the first token */
 
   msReleaseLock(TLOCK_PARSER);
   return MS_SUCCESS;

Modified: sandbox/sdlime/common-expressions/mapserver/mapparser.c
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/mapparser.c	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/mapparser.c	2010-11-22 05:51:59 UTC (rev 10751)
@@ -2259,30 +2259,31 @@
 
 int yylex(void) 
 {
-  int token, i=yyexpr->curtoken;
+  int token;
 
-  // printf("in yylex() - curtoken=%d of %d (value=%d)...\n", i, yyexpr->numtokens, yyexpr->tokens[i].token);
+  if(yyexpr->curtoken == NULL) return(0); /* done */
 
-  if(yyexpr->curtoken == yyexpr->numtokens) return(0); /* done */
+  // fprintf(stderr, "in yylex() - curtoken=%d...\n", yyexpr->curtoken->token);
 
-  token = yyexpr->tokens[i].token; /* may override */
-  switch(yyexpr->tokens[i].token) {
+  token = yyexpr->curtoken->token; /* may override */
+  switch(yyexpr->curtoken->token) {
   case MS_TOKEN_LITERAL_NUMBER:
     token = NUMBER;    
-    yylval.dblval = yyexpr->tokens[i].tokenval.dblval;
+    yylval.dblval = yyexpr->curtoken->tokenval.dblval;
     break;
   case MS_TOKEN_LITERAL_SHAPE:
     token = SHAPE;
-    yylval.shpval = yyexpr->tokens[i].tokenval.shpval;
+    // fprintf(stderr, "token value = %s\n", msShapeToWKT(yyexpr->curtoken->tokenval.shpval));
+    yylval.shpval = yyexpr->curtoken->tokenval.shpval;
     break;
   case MS_TOKEN_LITERAL_STRING:
-    // printf("token value = %s\n", yyexpr->tokens[i].tokenval.strval); 
+    // printf("token value = %s\n", yyexpr->curtoken->tokenval.strval); 
     token = STRING;
-    yylval.strval = strdup(yyexpr->tokens[i].tokenval.strval);    
+    yylval.strval = strdup(yyexpr->curtoken->tokenval.strval);    
     break;
   case MS_TOKEN_LITERAL_TIME:
     token = TIME;
-    yylval.tmval = yyexpr->tokens[i].tokenval.tmval;
+    yylval.tmval = yyexpr->curtoken->tokenval.tmval;
     break;
 
   case MS_TOKEN_COMPARISON_EQ: token = EQ; break;
@@ -2312,20 +2313,21 @@
   case MS_TOKEN_BINDING_DOUBLE:
   case MS_TOKEN_BINDING_INTEGER:
     token = NUMBER;
-    yylval.dblval = atof(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index]);
+    yylval.dblval = atof(yyshape->values[yyexpr->curtoken->tokenval.bindval.index]);
     break;
   case MS_TOKEN_BINDING_STRING:
     token = STRING;
-    yylval.strval = strdup(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index]);
+    yylval.strval = strdup(yyshape->values[yyexpr->curtoken->tokenval.bindval.index]);
     break;
   case MS_TOKEN_BINDING_SHAPE:
     token = SHAPE;
+    // fprintf(stderr, "token value = %s\n", msShapeToWKT(yyshape));
     yylval.shpval = yyshape;
     break;
   case MS_TOKEN_BINDING_TIME:
     token = TIME;
     msTimeInit(&(yylval.tmval));
-    if(msParseTime(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index], &(yylval.tmval)) != MS_TRUE) {
+    if(msParseTime(yyshape->values[yyexpr->curtoken->tokenval.bindval.index], &(yylval.tmval)) != MS_TRUE) {
       yyerror("Parsing time value failed.");
       return(-1);
     }
@@ -2343,7 +2345,7 @@
     break;
   }
 
-  yyexpr->curtoken++;
+  yyexpr->curtoken = yyexpr->curtoken->next; /* re-position */ 
   return(token);
 }
 

Modified: sandbox/sdlime/common-expressions/mapserver/mapparser.y
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/mapparser.y	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/mapparser.y	2010-11-22 05:51:59 UTC (rev 10751)
@@ -576,30 +576,31 @@
 
 int yylex(void) 
 {
-  int token, i=yyexpr->curtoken;
+  int token;
 
-  // printf("in yylex() - curtoken=%d of %d (value=%d)...\n", i, yyexpr->numtokens, yyexpr->tokens[i].token);
+  if(yyexpr->curtoken == NULL) return(0); /* done */
 
-  if(yyexpr->curtoken == yyexpr->numtokens) return(0); /* done */
+  // fprintf(stderr, "in yylex() - curtoken=%d...\n", yyexpr->curtoken->token);
 
-  token = yyexpr->tokens[i].token; /* may override */
-  switch(yyexpr->tokens[i].token) {
+  token = yyexpr->curtoken->token; /* may override */
+  switch(yyexpr->curtoken->token) {
   case MS_TOKEN_LITERAL_NUMBER:
     token = NUMBER;    
-    yylval.dblval = yyexpr->tokens[i].tokenval.dblval;
+    yylval.dblval = yyexpr->curtoken->tokenval.dblval;
     break;
   case MS_TOKEN_LITERAL_SHAPE:
     token = SHAPE;
-    yylval.shpval = yyexpr->tokens[i].tokenval.shpval;
+    // fprintf(stderr, "token value = %s\n", msShapeToWKT(yyexpr->curtoken->tokenval.shpval));
+    yylval.shpval = yyexpr->curtoken->tokenval.shpval;
     break;
   case MS_TOKEN_LITERAL_STRING:
-    // printf("token value = %s\n", yyexpr->tokens[i].tokenval.strval); 
+    // printf("token value = %s\n", yyexpr->curtoken->tokenval.strval); 
     token = STRING;
-    yylval.strval = strdup(yyexpr->tokens[i].tokenval.strval);    
+    yylval.strval = strdup(yyexpr->curtoken->tokenval.strval);    
     break;
   case MS_TOKEN_LITERAL_TIME:
     token = TIME;
-    yylval.tmval = yyexpr->tokens[i].tokenval.tmval;
+    yylval.tmval = yyexpr->curtoken->tokenval.tmval;
     break;
 
   case MS_TOKEN_COMPARISON_EQ: token = EQ; break;
@@ -629,20 +630,21 @@
   case MS_TOKEN_BINDING_DOUBLE:
   case MS_TOKEN_BINDING_INTEGER:
     token = NUMBER;
-    yylval.dblval = atof(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index]);
+    yylval.dblval = atof(yyshape->values[yyexpr->curtoken->tokenval.bindval.index]);
     break;
   case MS_TOKEN_BINDING_STRING:
     token = STRING;
-    yylval.strval = strdup(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index]);
+    yylval.strval = strdup(yyshape->values[yyexpr->curtoken->tokenval.bindval.index]);
     break;
   case MS_TOKEN_BINDING_SHAPE:
     token = SHAPE;
+    // fprintf(stderr, "token value = %s\n", msShapeToWKT(yyshape));
     yylval.shpval = yyshape;
     break;
   case MS_TOKEN_BINDING_TIME:
     token = TIME;
     msTimeInit(&(yylval.tmval));
-    if(msParseTime(yyshape->values[yyexpr->tokens[i].tokenval.bindval.index], &(yylval.tmval)) != MS_TRUE) {
+    if(msParseTime(yyshape->values[yyexpr->curtoken->tokenval.bindval.index], &(yylval.tmval)) != MS_TRUE) {
       yyerror("Parsing time value failed.");
       return(-1);
     }
@@ -660,7 +662,7 @@
     break;
   }
 
-  yyexpr->curtoken++;
+  yyexpr->curtoken = yyexpr->curtoken->next; /* re-position */ 
   return(token);
 }
 

Modified: sandbox/sdlime/common-expressions/mapserver/mapserver.h
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/mapserver.h	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/mapserver.h	2010-11-22 05:51:59 UTC (rev 10751)
@@ -510,7 +510,7 @@
 typedef struct listNode {
   shapeObj shape;
   struct listNode *next;
-  struct listNode *tailifhead; /* this is the tail node in the list, if this is the head element, otherwise NULL */
+  struct listNode *tailifhead; /* this is the tail node in the list if this is the head element, otherwise NULL */
 } featureListNodeObj;
 
 typedef featureListNodeObj * featureListNodeObjPtr;
@@ -566,11 +566,15 @@
   attributeBindingObj bindval;
 } tokenValueObj;
 
-typedef struct {
+typedef struct tokenListNode {
   int token;
   tokenValueObj tokenval;
-} tokenObj;
+  struct tokenListNode *next;
+  struct tokenListNode *tailifhead; /* this is the tail node in the list if this is the head element, otherwise NULL */
+} tokenListNodeObj;
 
+typedef tokenListNodeObj * tokenListNodeObjPtr;
+
 typedef struct {
   char *string;
   int type;
@@ -579,9 +583,8 @@
   int flags;
     
   /* logical expression options */
-  tokenObj tokens[100];
-  int numtokens;
-  int curtoken;
+  tokenListNodeObjPtr tokens;
+  tokenListNodeObjPtr curtoken;
 
   /* regular expression options */
   ms_regex_t regex; /* compiled regular expression to be matched */

Modified: sandbox/sdlime/common-expressions/mapserver/maputil.c
===================================================================
--- sandbox/sdlime/common-expressions/mapserver/maputil.c	2010-11-22 05:35:42 UTC (rev 10750)
+++ sandbox/sdlime/common-expressions/mapserver/maputil.c	2010-11-22 05:51:59 UTC (rev 10751)
@@ -384,7 +384,7 @@
     yyshape = shape;
     yylayer = layer;
     yyexpr = expression;
-    yyexpr->curtoken = 0; /* reset */
+    yyexpr->curtoken = yyexpr->tokens; /* reset to the start of the list */
     yypresult_type=MS_PARSE_RESULT_BOOLEAN;
 
     status = yyparse();



More information about the mapserver-commits mailing list