home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / lib / argiter.c next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  3.8 KB  |  152 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: argiter.c - implementation of CmdLineArgIter subclasses
  3. //
  4. // ^DESCRIPTION:
  5. //    This file implements the derived classes of a CmdLineArgIter that
  6. //    are declazred in <cmdline.h>.
  7. //
  8. // ^HISTORY:
  9. //    04/03/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  10. //-^^---------------------------------------------------------------------
  11.  
  12. #include <stdlib.h>
  13. #include <iostream.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16.  
  17. #include "cmdline.h"
  18.  
  19. //-------------------------------------------------------- class CmdLineArgIter
  20.  
  21. CmdLineArgIter::CmdLineArgIter(void) {}
  22.  
  23. CmdLineArgIter::~CmdLineArgIter(void) {}
  24.  
  25. //----------------------------------------------------------- class CmdArgvIter
  26.  
  27. CmdArgvIter::~CmdArgvIter(void) {}
  28.  
  29. const char *
  30. CmdArgvIter::operator()(void) {
  31.    return  ((index != count) && (array[index])) ? array[index++] : 0 ;
  32. }
  33.  
  34. int
  35. CmdArgvIter::is_temporary(void) const { return  0; }
  36.  
  37. //--------------------------------------------------------- class CmdStrTokIter
  38.  
  39. static const char WHITESPACE[] = " \t\n\r\v\f" ;
  40.  
  41.    // Constructor
  42. CmdStrTokIter::CmdStrTokIter(const char * tokens, const char * delimiters)
  43.    : tokstr(NULL), seps(NULL), token(NULL)
  44. {
  45.    reset(tokens, delimiters);
  46. }
  47.  
  48.    // Destructor
  49. CmdStrTokIter::~CmdStrTokIter(void)
  50. {
  51.    delete  tokstr;
  52. }
  53.  
  54.    // Use a new string and a new set of delimiters
  55. void
  56. CmdStrTokIter::reset(const char * tokens, const char * delimiters)
  57. {
  58.    seps = delimiters;
  59.    if (seps == NULL)  seps = WHITESPACE ;  // use default delimiters
  60.  
  61.    delete  tokstr;
  62.    tokstr = NULL;
  63.    token = NULL;
  64.    if (tokens) {
  65.       // Make a copy of the token-string (because ::strtok() modifies it)
  66.       // and get the first token from the string
  67.       //
  68.       tokstr = new char[::strlen(tokens) + 1] ;
  69.       (void) ::strcpy(tokstr, tokens);
  70.       token = ::strtok(tokstr, seps);
  71.    }
  72. }
  73.  
  74.    // Iterator function -- operator()
  75.    //   Just use ::strtok to get the next token from the string
  76.    //
  77. const char *
  78. CmdStrTokIter::operator()(void)
  79. {
  80.    if (seps == NULL)  seps = WHITESPACE ;
  81.    const char * result = token;
  82.    if (token) token = ::strtok(NULL, seps);
  83.    return  result;
  84. }
  85.  
  86.    // The storage that we return pointers to (in operator())
  87.    // always points to temporary space.
  88.    //
  89. int
  90. CmdStrTokIter::is_temporary(void) const
  91. {
  92.    return  1;
  93. }
  94.  
  95. //-------------------------------------------------------- class CmdIstreamIter
  96.  
  97. const unsigned  CmdIstreamIter::MAX_LINE_LEN = 1024 ;
  98.  
  99.    // Constructor
  100. CmdIstreamIter::CmdIstreamIter(istream & input) : is(input), tok_iter(NULL)
  101. {
  102. }
  103.  
  104.    // Destructor
  105. CmdIstreamIter::~CmdIstreamIter(void)
  106. {
  107.    delete  tok_iter;
  108. }
  109.  
  110.    // Iterator function -- operator()
  111.    //
  112.    // What we do is this: for each line of text in the istream, we use
  113.    // a CmdStrTokIter to iterate over each token on the line.
  114.    //
  115.    // If the first non-white character on a line is c_COMMENT, then we
  116.    // consider the line to be a comment and we ignore it.
  117.    //
  118. const char *
  119. CmdIstreamIter::operator()(void)
  120. {
  121.    const char * result = NULL;
  122.    if (tok_iter)  result = tok_iter->operator()();
  123.    if (result)  return  result;
  124.    if (! is)  return  NULL;
  125.  
  126.    char buf[CmdIstreamIter::MAX_LINE_LEN];
  127.    do {
  128.       *buf = '\0';
  129.       is.getline(buf, sizeof(buf));
  130.       char * ptr = buf;
  131.       while (isspace(*ptr)) ++ptr;
  132.       if (*ptr && (*ptr != CmdIstreamIter::c_COMMENT)) {
  133.          if (tok_iter) {
  134.             tok_iter->reset(ptr);
  135.          } else {
  136.             tok_iter = new CmdStrTokIter(ptr);
  137.          }
  138.          return  tok_iter->operator()();
  139.       }
  140.    } while (is);
  141.    return  NULL;
  142. }
  143.  
  144.    // We use a CmdStrTokIterator that is always temporary, thus the
  145.    // the tokens we return are always in temporary storage
  146.    //
  147. int
  148. CmdIstreamIter::is_temporary(void) const
  149. {
  150.    return  1;
  151. }
  152.