wcalc  2.5
About: Wcalc is a natural-expression command-line calculator.
  Fossies Dox: wcalc-2.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

extract_vars.c
Go to the documentation of this file.
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: */
peekListElement
void * peekListElement(List, size_t)
Definition: list.c:284
isfunc
int isfunc(const char *str)
Definition: isfunc.c:88
isfunc.h
free
void free(void *)
isconst.h
extract_vars
List extract_vars(char *str)
Definition: extract_vars.c:16
listLen
size_t listLen(List)
Definition: list.c:352
_list
Definition: list.c:19
addToList
void addToList(List *, void *)
Definition: list.c:204
isconst
int isconst(const char *str)
Definition: isconst.c:122
extract_vars.h
list.h