home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / cmd / quoted.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  3.9 KB  |  158 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: quoted.c - implement quoted strings
  3. //
  4. // ^DESCRIPTION:
  5. //     This file implements that class defined in "quoted.h"
  6. //
  7. // ^HISTORY:
  8. //    05/01/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  9. //-^^---------------------------------------------------------------------
  10.  
  11. #include <stdlib.h>
  12. #include <iostream.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. #include "quoted.h"
  17.  
  18. //--------------------------------------------------------------- Constructors
  19.  
  20. QuotedString::QuotedString(unsigned  max_size)
  21.    : size(max_size)
  22. {
  23.    buffer = new char[size] ;
  24. }
  25.    
  26.  
  27. QuotedString::QuotedString(const char * str)
  28. {
  29.    size = ::strlen(str + 1);
  30.    buffer = new char[size];
  31.    if (buffer)  ::strcpy(buffer, str);
  32. }
  33.  
  34. QuotedString::QuotedString(const char * str, unsigned  max_size)
  35.    : size(max_size)
  36. {
  37.    buffer = new char[size];
  38.    if (buffer)  ::strcpy(buffer, str);
  39. }
  40.  
  41. QuotedString::QuotedString(const QuotedString & qstr)
  42.    : size(qstr.size)
  43. {
  44.    buffer = new char[size];
  45.    if (buffer)  ::strcpy(buffer, qstr.buffer);
  46. }
  47.  
  48. //--------------------------------------------------------------- Destructor
  49.  
  50. QuotedString::~QuotedString(void)
  51. {
  52.    delete [] buffer ;
  53. }
  54.  
  55. //--------------------------------------------------------------- Assignment
  56.  
  57. QuotedString &
  58. QuotedString::operator=(const QuotedString & qstr)
  59. {
  60.    delete [] buffer ;
  61.    size = qstr.size;
  62.    buffer = new char[size];
  63.    if (buffer)  ::strcpy(buffer, qstr.buffer);
  64.    return  *this ;
  65. }
  66.  
  67. QuotedString &
  68. QuotedString::operator=(const char * str)
  69. {
  70.    delete [] buffer ;
  71.    size = ::strlen(str) + 1;
  72.    buffer = new char[size];
  73.    if (buffer)  ::strcpy(buffer, str);
  74.    return  *this ;
  75. }
  76.  
  77. //--------------------------------------------------------------- operator>>
  78.  
  79. istream &
  80. operator>>(istream & is, QuotedString & qstr)
  81. {
  82.       // get the first non-white character
  83.    char  ch;
  84.    is >> ch;
  85.    if (! is) {
  86.       if (is.eof()) {
  87.          cerr << "Premature end of input.\n"
  88.               << "\texpecting a single or a double quote." << endl ;
  89.       } else {
  90.          cerr << "Unable to extract quoted string from input." << endl ;
  91.       }
  92.       return  is;
  93.    }
  94.  
  95.    int  single_quote = 0, double_quote = 0;
  96.  
  97.    switch (ch) {
  98.    case '\'' :
  99.       single_quote = 1;  break;
  100.    case '"'  :
  101.       double_quote = 1;  break;
  102.    default :
  103.       cerr << "Unexpected character '" << ch << "'.\n" 
  104.            << "\texpecting a single or a double quotation mark." << endl ;
  105.       is.clear(ios::failbit);
  106.       return  is;
  107.    } //switch
  108.  
  109.  
  110.    // Now fetch into "dest" until we see the ending quote.
  111.    char    * dest = qstr.buffer;
  112.    unsigned  end_quote = 0;
  113.    unsigned  len = 0;
  114.    int  c;
  115.    while (! end_quote) {
  116.       int  escape = 0;
  117.       c = is.get();
  118.       if (! is) {
  119.          if (is.eof()) {
  120.             cerr << "Unmatched " << (single_quote ? "\"'\"" : "'\"'")
  121.                  << "quote." << endl ;
  122.          } else {
  123.             cerr << "Unable to extract quoted string from input." << endl ;
  124.          }
  125.          return  is;
  126.       }
  127.       if (c == '\\') {
  128.          escape = 1;
  129.          c = is.get();
  130.          if (! is) {
  131.             if (is.eof()) {
  132.                cerr << "Unmatched " << (single_quote ? "\"'\"" : "'\"'")
  133.                     << " quote." << endl ;
  134.             } else {
  135.                cerr << "Unable to extract quoted string from input." << endl ;
  136.             }
  137.             return  is;
  138.          }
  139.       }
  140.       if ((c == '\'') && single_quote && !escape) {
  141.          end_quote = 1;
  142.       } else if ((c == '"') && double_quote && !escape) {
  143.          end_quote = 1;
  144.       } else if (len < qstr.size) {
  145.          dest[len++] = c;
  146.       } else {
  147.          cerr << "Error - quoted string is too long.\n\tmust be less than "
  148.               << qstr.size << " characters." << endl ;
  149.          is.clear(ios::failbit);
  150.          return  is;
  151.       }
  152.    } //while
  153.    
  154.    dest[len++] = '\0' ;   // dont forget to NUL-terminate
  155.    return  is;
  156. }
  157.  
  158.