home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / execdemo.zip / executil.c < prev    next >
Text File  |  1994-09-03  |  3KB  |  142 lines

  1. /*
  2.  * EXECUTIL.C
  3.  * Copyright 1994 Bryan Walker DBA WalkerWerks.  All rights reserved.
  4.  *
  5.  * Support functions for EXECPRES.EXE
  6.  *
  7.  * This sample is provided to help illustrate examples for the class
  8.  * "Executing Programs in OS/2" for ColoradOS/2 1994.  The code is for
  9.  * example purposes only and not as a teaching tool for general programming principals.
  10.  * The author has no liability for any use of the code either private or commercial,
  11.  * or for any damages arrising from such use.
  12.  *
  13.  */
  14. #define TRUE  1
  15. #define FALSE 0
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. /*
  21.  * A replacement for strtok.  It was used
  22.  * because early versions of Cset++ didn't
  23.  * handle two tokens side by side correctly.
  24.  * (i.e. ,, )
  25.  */
  26. char *StrTok(char *string1, char *string2)
  27. {
  28. static char *curptr ;
  29. char *rtnptr ;
  30. char *token ;
  31.  
  32. if(string1 != NULL)
  33.    curptr = string1 ;
  34.  
  35. if(curptr == NULL || *curptr == 0)
  36.    return NULL ;
  37.  
  38. token = rtnptr = curptr ;
  39. while(*token != 0)
  40.    {
  41.    if(strchr(string2, *token) != NULL)
  42.       {
  43.       *token = 0 ;
  44.       curptr = token + 1 ;
  45.       break;
  46.       }
  47.    else
  48.       curptr = NULL ;
  49.    token++ ;
  50.    } ;
  51. return rtnptr ;
  52. }
  53.  
  54.  
  55. /*
  56.  * Parses a PSZ in the CSV format where
  57.  * each field is separated by a comma,
  58.  * fields with imbedded commas are
  59.  * surrounded by double quotes, and
  60.  * literal double quotes are identified
  61.  * as by two double quotes side by side
  62.  * (i.e. "").  Works exactly like strtok
  63.  * except the string2 identifier is not
  64.  * necessary because the only acceptable
  65.  * token is the comma.  So to continue
  66.  * parsing the same string send NULL
  67.  * as string1.  Returns NULL if at
  68.  * end of PSZ.
  69.  */
  70. char *CsvTok(char *string1)
  71. {
  72. static char *curcsvptr ;
  73. static int  LastWasQuote ;
  74. char *rtnptr ;
  75. char *token, *tmp ;
  76. char *string2 = "," ;
  77. long len ;
  78.  
  79. if(string1 != NULL)
  80.    {
  81.    curcsvptr = string1 ;
  82.    LastWasQuote = FALSE ;
  83.    }
  84.  
  85. if(curcsvptr == NULL || *curcsvptr == 0)
  86.    return NULL ;
  87. if( *curcsvptr == '"' && curcsvptr[1] != '"')
  88.    {
  89.    LastWasQuote = TRUE ;
  90.    curcsvptr ++ ;
  91.    }
  92. token = rtnptr = curcsvptr ;
  93. if(LastWasQuote)
  94.    {
  95.    while(*token != 0)
  96.       {
  97.       if(*token == '"' && token[1] != '"')
  98.          {
  99.          *token = 0 ;
  100.          curcsvptr = token + 2 ;
  101.          break;
  102.          }
  103.       else if (*token == '"' && token[1] == '"')
  104.          token++ ;
  105.       else
  106.          curcsvptr = NULL ;
  107.       token++ ;
  108.       } ;
  109.    LastWasQuote = FALSE ;
  110.    }
  111. else
  112.    {
  113.    while(*token != 0)
  114.       {
  115.       if(strchr(string2, *token) != NULL)
  116.          {
  117.          *token = 0 ;
  118.          curcsvptr = token + 1 ;
  119.          break;
  120.          }
  121.       else
  122.          curcsvptr = NULL ;
  123.       token++ ;
  124.       } ;
  125.    }
  126.  
  127. /*
  128.  * turn any double quotes into single quotes
  129.  */
  130. token = rtnptr;
  131. while( (tmp = strstr(token, "\"\"")) != NULL)
  132.    {
  133.    token = tmp+1 ;
  134.    len = strlen(token) ;
  135.    memmove(tmp, token, len) ;
  136.    tmp[len] = 0 ;
  137.    token = tmp ;
  138.    } ;
  139.  
  140. return rtnptr ;
  141. }
  142.