Changeset 16168 for trunk/Ohana/src/opihi/cmd.basic/strsub.c
- Timestamp:
- Jan 20, 2008, 3:36:42 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.basic/strsub.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.basic/strsub.c
r16166 r16168 1 1 # include "basic.h" 2 2 3 int str pop(int argc, char **argv) {3 int strsub (int argc, char **argv) { 4 4 5 char *p, *q, *string; 5 int N, Nkey, Noutput; 6 char *varName; 7 char *p, *q, *input, *output, *key, *value; 6 8 7 9 // clear all sections … … 13 15 } 14 16 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"); 17 20 return (FALSE); 18 21 } 19 22 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]; 23 25 24 /* thisword is an allocated string */ 25 p = thisword (string); 26 // find this in the string 27 key = argv[2]; 28 Nkey = strlen(key); 26 29 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)); 32 49 } 33 50 34 if ( argc == 3) {35 set_str_variable ( argv[2], p);51 if (varName) { 52 set_str_variable (varName, output); 36 53 } else { 37 gprint (GP_LOG, "%s\n", p);54 gprint (GP_LOG, "%s\n", output); 38 55 } 39 56 40 free (p); 41 free (string); 57 free (output); 42 58 return (TRUE); 43 59 } 44 45 strsub /data/@HOST@ @HOST@ foobar46
Note:
See TracChangeset
for help on using the changeset viewer.
