IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 20, 2008, 3:36:42 PM (18 years ago)
Author:
eugene
Message:

adding strsub function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.basic/strsub.c

    r16166 r16168  
    11# include "basic.h"
    22
    3 int strpop (int argc, char **argv) {
     3int strsub (int argc, char **argv) {
    44
    5   char *p, *q, *string;
     5  int N, Nkey, Noutput;
     6  char *varName;
     7  char *p, *q, *input, *output, *key, *value;
    68
    79  // clear all sections
     
    1315  }
    1416
    15   if (argc != 2) {
    16     gprint (GP_ERR, "USAGE: strsub (string) (replace) (value) [-var out]\n");
     17  if (argc != 4) {
     18    gprint (GP_ERR, "USAGE: strsub (string) (key) (value) [-var out]\n");
     19    gprint (GP_ERR, "  replace instances of 'key' with the 'value'\n");
    1720    return (FALSE);
    1821  }
    1922
    20   /* string is a copy of the value on the variable stack */
    21   string = get_variable (argv[1]);
    22   if (string == NULL) return (FALSE);
     23  // input string
     24  input = argv[1];
    2325 
    24   /* thisword is an allocated string */
    25   p = thisword (string);
     26  // find this in the string
     27  key = argv[2];
     28  Nkey = strlen(key);
    2629
    27   q = nextword (string);
    28   if (q == NULL) {
    29     set_str_variable (argv[1], "NULL");
    30   } else {
    31     set_str_variable (argv[1], q);
     30  // replace with this
     31  value = argv[3];
     32
     33  Noutput = MAX (16, strlen(input));
     34  ALLOCATE (output, char, Noutput);
     35  memset (output, 0, Noutput);
     36
     37  // 0123456789
     38  // wordKEYnew
     39  // p = 0, q = 4, q-p = 4 -> p = 7
     40
     41  p = input;
     42  while (*p && ((q = strstr (p, key)) != NULL)) {
     43    output = opihi_append (output, &Noutput, p, q);
     44    output = opihi_append (output, &Noutput, value, strlen(value));
     45    p = q + Nkey;
     46  }
     47  if (*p) {
     48    output = opihi_append (output, &Noutput, p, strlen(p));
    3249  }
    3350 
    34   if (argc == 3) {
    35     set_str_variable (argv[2], p);
     51  if (varName) {
     52    set_str_variable (varName, output);
    3653  } else {
    37     gprint (GP_LOG, "%s\n", p);
     54    gprint (GP_LOG, "%s\n", output);
    3855  }
    3956
    40   free (p);
    41   free (string);
     57  free (output);
    4258  return (TRUE);
    4359}
    44 
    45 strsub /data/@HOST@ @HOST@ foobar
    46 
Note: See TracChangeset for help on using the changeset viewer.