IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 6, 2006, 11:42:18 AM (20 years ago)
Author:
eugene
Message:

added variable existence tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/variable.c

    r3689 r6327  
    9797}
    9898
     99static char variable_true[] = "1";
     100static char variable_false[] = "0";
     101
    99102char *get_variable_ptr (char *name) {
    100103 
    101104  int i;
    102105  char *local, *MacroName;
     106
     107  if (name == NULL) return (NULL);
     108  if (*name == 0) return (NULL);
     109
     110  /* check for the existence of the given name (after ?) */
     111  /* return a string which should not be freed */
     112  if (*name == '?') {
     113    value = get_variable_ptr (name[1]);
     114    if (value == NULL) {
     115      return variable_false;
     116    }
     117    return variable_true;
     118  }
    103119
    104120  MacroName = GetMacroName ();
     
    128144  int i;
    129145  char *local, *MacroName;
     146  char *value;
     147
     148  if (name == NULL) return (NULL);
     149  if (*name == 0) return (NULL);
     150
     151  /* check for the existence of the given name (after ?) */
     152  /* return a string which can be freed */
     153  if (*name == '?') {
     154    value = get_variable_ptr (name[1]);
     155    if (value == NULL) {
     156      value = strcreate (variable_false);
     157      return value;
     158    }
     159    value = strcreate (variable_true);
     160    return value;
     161  }
    130162
    131163  MacroName = GetMacroName ();
     
    149181  }
    150182  return (NULL);
     183}
     184
     185/* return TRUE / FALSE if name is an existant variable */
     186int get_variable_exists (char *name) {
     187 
     188  int i;
     189  char *local, *MacroName;
     190
     191  MacroName = GetMacroName ();
     192
     193  /* look for local variable first */
     194  ALLOCATE (local, char, strlen(name) + strlen(MacroName) + 2);
     195  sprintf (local, "%s.%s", MacroName, name);
     196  for (i = 0; i < Nvariables; i++) { /* find the variable mentioned */
     197    if (!strcmp(local, variables[i].name)) {
     198      free (local);
     199      return (TRUE);
     200    }
     201  }
     202  free (local);
     203
     204  /* look for global variable */
     205  for (i = 0; i < Nvariables; i++) { /* find the variable mentioned */
     206    if (!strcmp(name, variables[i].name)) {
     207      return (TRUE);
     208    }
     209  }
     210  return (FALSE);
    151211}
    152212
Note: See TracChangeset for help on using the changeset viewer.