home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / elem-c.zip / STRINGS.C < prev   
Text File  |  1985-05-09  |  11KB  |  299 lines

  1.  
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6.  
  7. /*
  8. ;;      file:   _STRING.C
  9. ;;
  10. ;; --------------- Command Tail Processing Functions ---------------
  11. ;;
  12. ;;      This next set of functions are meant to be used together. Examples
  13. ;;on their use follows.
  14. ;;
  15. ;;
  16. ;;int num_args(string)  Returns the number of args in the string, seperated
  17. ;;                      by delims (see delim(), below). Leading delimiters
  18. ;;                      are ignored.
  19. ;;
  20. ;;char *next_arg(string)        Returns a pointer to the next arg, delimited 
  21. ;;                      by a delim, skipping over the current arg. Use via
  22. ;;                      ptr= nextarg(ptr) to skip to each argument. All 
  23. ;;                      switches at the end of the current arg are skipped.
  24. ;;
  25. ;;
  26. ;;char *skip_delim(string) Skips leading delims in a string. returns a pointer.
  27. ;;
  28. ;;cpyarg(to,from)               Copies a string, up to the next delim or switch.
  29. ;;                      Leading and trailing delimiters are stripped (from 
  30. ;;                      the output string) and a null terminator is added.
  31. ;;
  32. ;;                      after cpyarg()          FROM: foo/b
  33. ;;                                              TO: foo
  34. ;;
  35. ;;delim(c)              Returns true if the character is a delimiter.
  36. ;;                      Nulls are not considered a delimiter. The list of
  37. ;;                      delimiters is contained in the array '_dlmlst', and
  38. ;;                      can be changed via newdelim().
  39. ;;
  40. ;;newdelim(s)           Replace the list of delimiters. The string 's' must
  41. ;;                      be less than 20 chars.
  42. ;;
  43. ;;isswitch(c)           Returns true if the character is the current DOS
  44. ;;                      switch character.
  45. ;;
  46. ;;char filtchar(c)      Convert a character to one legal for an MSDOS filename.
  47. ;;                      Illegal characters such as switch or path seperators,
  48. ;;                      control characters, etc are changed to '$'. Bit 7 is
  49. ;;                      masked off. (Disallows foreign language support??)
  50. ;;
  51. ;;wild(string)          Returns true if the string contains a star or question.
  52. ;;
  53. ;;char *strip_path(out,in) Copies the disk specifier or pathname to the output
  54. ;;                      array, and returns a pointer to the name in the input
  55. ;;                      string. Drive specs are considered a path, and are
  56. ;;                      treated as such by DOS. Stripping "a:foo" and
  57. ;;                      "bin/foo/framis.asm" result in:
  58. ;;
  59. ;;                              IN:     "A:"
  60. ;;                              IN:     "bin\foo"
  61. ;;
  62. ;;                              OUT:    "A:"
  63. ;;                              OUT:    "bin\"
  64. ;;
  65. ;;strip_switch(out,in)  Copy the switches from the in string, remove the switch
  66. ;;                      character and put all the characters in the out array.
  67. ;;                      Each is converted to upper case, and the string is null
  68. ;;                      terminated.
  69. ;;
  70. ;;ispath_delim(c)       Returns true if the character is a legal pathname
  71. ;;char c;               component seperator. The only two characters legal
  72. ;;                      (here at least) are \ and-or /. For example:
  73. ;;
  74. ;;                      Switch character = /
  75. ;;                              ispath_delim('/') == 0
  76. ;;                              ispath_delim('\\') == 1
  77. ;;                      Switch character = -
  78. ;;                              ispath_delim('/') == 1
  79. ;;                              ispath_delim('\\') == 1
  80. ;;                      Switch character = \
  81. ;;                              ispath_delim('/') == 1
  82. ;;                              ispath_delim('\\') == 0
  83. ;;
  84. ;;
  85. ;; --------- Command Tail Processing Examples ----------
  86. ;;
  87. ;;      This is an example of one way to use the above functions to process
  88. ;;an MSDOS program command tail. You do not need to know what the system switch
  89. ;;character or legal path seperators are. If you can't handle paths, strip them
  90. ;;off. Assume there is a pointer, p, that points to a command tail string:
  91. ;;
  92. ;;      p= "  source.ext abc.asm/v+\bin\def.com /x/y/z"
  93. ;;
  94. ;;      p= skip_delim(p);               /* p= "source.exe abc...... */
  95. ;;
  96. ;;      p= next_arg(p);                 /* p= "abc.asm/v.... "  */
  97. ;;      cpyarg(work,p);                 /* work= "abc.asm"  */
  98. ;;      s= strip_path(work,p));         /* s= "abc.asm"   path= ""  */
  99. ;;      strip_switch(sw,p);             /* sw= "V"  */
  100. ;;
  101. ;;      p= next_arg(p);                 /* p= "\bin\def.com"  */
  102. ;;      cpyarg(work,p);                 /* work= "\bin\def.com"  */
  103. ;;      s= strip_path(path,p);          /* s= "def.com"  path= "\bin\"  */
  104. ;;      strip_switch(sw,p);             /* sw= ""  */
  105. ;;
  106. ;;      p= next_arg(p);                 /* p= "/x/y/z"  */
  107. ;;      cpyarg(work,p);                 /* work= ""  */
  108. ;;      s= strip_path(path,p);          /* s= ""  path= ""  */
  109. ;;      strip_switch(sw,p);             /* sw= "XYZ"  */
  110. ;;
  111. ;;      p= next_arg(p);                 /* p= ""  */
  112. ;;
  113. ;;      while (num_args(p)              /* while not end of string ... */
  114. ;;      while (*p)                      /* another way ... */
  115. ;;
  116. ;;
  117. ;;char *name,sw[4],path[40];
  118. ;;
  119. ;;      p= skip_delim(p);
  120. ;;      while (num_args(p) > 0) {       /* *p points to the current arg, */
  121. ;;              name= strip_path(path,p);
  122. ;;              strip_switch(sw,p);
  123. ;;              printf("Path= %s, Name= %s, Switches= %s\n",path,name,sw);
  124. ;;      }
  125. ;;
  126. */
  127. /* Return the number of args left in the string. */
  128.  
  129. num_args(string)
  130. char *string;
  131. {
  132. int count;
  133.  
  134.         count= 0;
  135.         string= (char *)skip_delim(string);     /* skip leading blanks, */
  136.         while (*string) {
  137.                 ++count;                        /* count one, */
  138.                 string= (char *)next_arg(string); /* find next, */
  139.         }
  140.         return(count);
  141. }
  142. /* Return a pointer to the next argument in the string. */
  143.  
  144. next_arg(string)
  145. char *string;
  146. {
  147.         while ((!delim(*string)) && *string)            /* skip this one, */
  148.                 ++string;                               /* up to delim, */
  149.         string= (char *)skip_delim(string);             /* then skip delims, */
  150.         return(string);
  151. }
  152.  
  153. /* Skip over the leading delimiters in a string. */
  154.  
  155. skip_delim(string)
  156. char *string;
  157. {
  158.         while (delim(*string) && *string)
  159.                 ++string;
  160.         return(string);
  161. }
  162. /* Copy the string to the destination array, stopping if we find one
  163. of our delimiters or switches. */
  164.  
  165. cpyarg(to,from)
  166. char *to;
  167. char *from;
  168. {
  169.         while ( (!delim(*from)) && (!isswitch(*from)) && *from) 
  170.                 *to++= *from++;
  171.         *to= '\0';
  172.         return;
  173. }
  174. /* Strip any switches from the input string, put into the output array. */
  175.  
  176. strip_switch(out,in)
  177. char *out;
  178. char *in;
  179. {
  180.         while (*in && (!isswitch(*in)))         /* skip to end of string */
  181.                 ++in;                           /* or first switch, */
  182.  
  183.         while (*in && isswitch(*in)) {          /* copy switch args while */
  184.                 ++in;
  185.                 *out++ = toupper(*in);          /* stripping switch chars, */
  186.                 ++in;
  187.         }
  188.         *out= '\0';                             /* terminate it, */
  189.         return;
  190. }
  191. /* ----- List of legal delimiters. This is the default list ----- */
  192.  
  193. char _dlmlst[20] = { " \t,+" }; /* space, tab, comma, plus */
  194.  
  195. /* Change the list of delimiters. */
  196.  
  197. newdelim(s)
  198. char *s;
  199. {
  200.         strcpy(_dlmlst,s);
  201.         return;
  202. }
  203.  
  204. /* Return true if the character is a delimiter from the list above. */
  205.  
  206. delim(c)
  207. char c;
  208. {
  209. int i;
  210.         for (i= 0; _dlmlst[i]; ++i) {
  211.                 if (c == _dlmlst[i]) return(1);
  212.         }
  213.         return(0);
  214. }
  215. /* return true if the character is the current switch character. */
  216.  
  217. isswitch(c)
  218. char c;
  219. {
  220.         return(c == _charop(0,0));
  221. }
  222. /* Clean up the character for a legal MSDOS filename. Convert undesireable
  223. characters to a dollar. */
  224.  
  225. char filtchar(c)
  226. char c;
  227. {
  228.         c&= 0x7f;                       /* strip bit 7, */
  229.         if (isswitch(c) || ispath_delim(c) || (c < ' ') || (c > '~') )
  230.                 c= '$';                 /* dont allow illegal chars */
  231.         c= toupper(c);                  /* all uppercase, */
  232.         return(c);
  233. }
  234. /* Return 1 if the string is a wild filespec. */
  235.  
  236. wild(string)
  237. char *string;
  238. {
  239. char *p;
  240.  
  241.         p= string;
  242.         while (*p) {
  243.                 if (*p == '?')
  244.                         return(1);
  245.                 if (*p == '*')
  246.                         return(1);
  247.                 ++p;
  248.         }
  249.         return(0);                              /* not wild. */
  250. }
  251. /* Strip the pathname or disk specifier from a filename, return it in a
  252. seperate array. We do this by initially copying the entire name in, then
  253. searching for the colon or slash. Right after the last one we find,
  254. stuff a null, removing the name part. 
  255.  
  256. Also return a pointer to the name part in the input name. */
  257.  
  258. strip_path(out,in)
  259. char *out;
  260. char *in;
  261. {
  262. char *name;
  263. char *endpath;
  264.  
  265.         strcpy(out,in);                 /* duplicate, for working, */
  266.         name= in;                       /* point to name, */
  267.         endpath= out;                   /* and end of path part, */
  268.  
  269.         while (*in) {                   /* look for slashes or colons, */
  270.                 if (*in == ':') {       /* if a colon, */
  271.                         endpath= ++out; /* point to name, */
  272.                         name= ++in;
  273.                 } else if (ispath_delim(*in)) {
  274.                         endpath= ++out; /* move the pointer up, */
  275.                         name= ++in;
  276.                 } else {
  277.                         ++in;
  278.                         ++out;
  279.                 }
  280.         }
  281.         *endpath= '\0';                 /* delete the name part, */
  282.         return(name);                   /* return ptr to name part. */
  283. }
  284.  
  285. /* Return true if the character is a legal path name component seperator.
  286. The legal ones here are \ or /, depending on what the switch character is. */
  287.  
  288. ispath_delim(c)
  289. char c;
  290. {
  291.         if ((c == '\\') && (!isswitch('\\'))) return(1);
  292.         if ((c == '/') && (!isswitch('/'))) return(1);
  293.         return(0);
  294. }
  295.  
  296.  
  297. 4% ') && (!isswitch('\\'))) return(1);
  298.         if ((c == '/') && (!isswitch('/'))) return(1);
  299.         return(0