"Fossies" - the Fresh Open Source Software Archive

Member "wcalc-2.5/src/common/extract_vars.c" (29 Dec 2014, 2640 Bytes) of package /linux/privat/wcalc-2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "extract_vars.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.5.

    1 #ifdef HAVE_CONFIG_H
    2 # include "config.h"
    3 #endif
    4 
    5 /* System Headers */
    6 #include <stdlib.h>                    /* for NULL */
    7 #include <ctype.h>                     /* for isalpha() and isdigit() */
    8 #include <string.h>                    /* for strdup() */
    9 
   10 /* Internal Headers */
   11 #include "list.h"                      /* for List */
   12 #include "isfunc.h"
   13 #include "isconst.h"
   14 #include "extract_vars.h"
   15 
   16 List extract_vars(char *str)
   17 {   /*{{{*/
   18     char *curs, *eov;
   19     List  variables = NULL;
   20     char *varname;
   21 
   22     curs = str;
   23     while (curs && *curs) {
   24         // search for the first letter of a possible variable
   25         while (curs && *curs && !isalpha((int)(*curs))) {
   26             if ((*curs == '\'') || (*curs == '#')) {
   27                 break;
   28             }
   29             curs++;
   30             // skip hex numbers
   31             if (((*curs == 'x') || (*curs == 'X')) && (curs != str) &&
   32                 (*(curs - 1) == '0')) {
   33                 curs++;
   34                 while (curs && *curs &&
   35                        (isdigit((int)(*curs)) ||
   36                         (*curs >= 'a' && *curs <= 'f') || (*curs >= 'A' &&
   37                                                            *curs <= 'F'))) {
   38                     curs++;
   39                 }
   40             }
   41             // skip binary numbers
   42             if ((*curs == 'b') && (curs != str) && (*(curs - 1) == '0')) {
   43                 curs++;
   44             }
   45         }
   46 
   47         // if we didn't find a first letter, we're done looking
   48         if ((*curs == 0) || (*curs == '#') || (*curs == '\'')) {
   49             break;
   50         }
   51         // if we did find something, pull out the variable name
   52         eov = curs;
   53         while (*eov &&
   54                (isalpha((int)(*eov)) || *eov == '_' || *eov == ':' ||
   55                 isdigit((int)(*eov)))) {
   56             eov++;
   57         }
   58         {
   59             char save_char = *eov;
   60             *eov    = 0;
   61             varname = (char *)strdup(curs);
   62             *eov    = save_char;
   63             curs    = eov;
   64         }
   65 
   66         // add it to the set of known variables
   67         if (!isfunc(varname) && !isconst(varname)) {
   68             size_t i      = 0;
   69             char   exists = 0;
   70 
   71             while (i < listLen(variables)) {
   72                 char *peekstr = peekListElement(variables, i);
   73 
   74                 if (!strcmp(peekstr, varname)) {
   75                     exists = 1;
   76                     break;
   77                 }
   78                 i++;
   79             }
   80             if (!exists) {
   81                 addToList(&variables, (void *)strdup(varname));
   82             }
   83         }
   84         free(varname);
   85     }
   86     return variables;
   87 } /*}}}*/
   88 
   89 /* vim:set expandtab: */