core/vul/vul_awk.h

Go to the documentation of this file.
00001 // This is core/vul/vul_awk.h
00002 #ifndef vul_awk_h_
00003 #define vul_awk_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Andrew W. Fitzgibbon, Oxford RRG
00010 // \date   17 May 97
00011 //
00012 // \verbatim
00013 // Modifications
00014 // 970517 AWF Initial version.
00015 // PDA (Manchester) 21/03/2001: Tidied up the documentation
00016 // Peter Vanroose   27/05/2001: Corrected the documentation
00017 // \endverbatim
00018 
00019 
00020 #include <vcl_string.h>
00021 #include <vcl_iosfwd.h>
00022 #include <vcl_vector.h>
00023 
00024 //: The core of awk
00025 // vul_awk reads lines from a vcl_istream and breaks them into whitespace-separated
00026 // fields.  Its primary advantage is that its name defines the semantics of
00027 // its methods---except that this C++ version uses zero-based fields.  The
00028 // usage is exemplified in this example, to print the second field in every
00029 // line:
00030 // \code
00031 //    for (vul_awk awk=cin; awk; ++awk)
00032 //      vcl_cout << awk[2] << vcl_endl;
00033 // \endcode
00034 
00035 class vul_awk
00036 {
00037   VCL_SAFE_BOOL_DEFINE;
00038  public:
00039   // Constructors/Destructors--------------------------------------------------
00040   enum ModeFlags {
00041     none = 0x00,
00042     verbose = 0x01,
00043     strip_comments = 0x02,
00044     backslash_continuations = 0x04
00045   };
00046 
00047   vul_awk(vcl_istream& s, ModeFlags mode = none);
00048   ~vul_awk();
00049 
00050   // Operations----------------------------------------------------------------
00051 
00052 //: Return field i.  Counting starts at 0.
00053   char const* operator[] (unsigned i) const {
00054     if (i < fields_.size())
00055       return fields_[i];
00056     else
00057       return 0;
00058   }
00059 
00060 //: Return the current "record number", i.e. line number
00061   int NR() const { return line_number_; }
00062 
00063 //: Return the number of fields on this line.
00064   int NF() const { return fields_.size(); }
00065 
00066 //: Return the entire line
00067   char const* line() const { return (char const*)line_.c_str(); }
00068 
00069 //: Return the remainder of the line, starting from field_number.
00070 // (0 is from the first non-whitespace character)
00071   char const* line_from(int field_number) const;
00072 
00073 //: Return true if this line is not the last.
00074   operator safe_bool () const
00075     { return (!done_)? VCL_SAFE_BOOL_TRUE : 0; }
00076 
00077 //: Return false if this line is not the last.
00078   bool operator!() const
00079     { return done_; }
00080 
00081 //: Advance to the next line
00082   vul_awk& operator ++ () { next(); return *this; }
00083 
00084 //: Display error message, line number.
00085 // Also display optional field number and  char within field.
00086 
00087   void error(vcl_ostream&, char const* message, int field = -1,
00088              int char_within_field = 0);
00089 
00090  protected:
00091   // Data Members--------------------------------------------------------------
00092   vcl_istream& fd_;
00093 
00094   ModeFlags mode_;
00095 
00096   // The last input line.
00097   vcl_string line_;
00098 
00099   // Copy of last line with null characters at the start of every field
00100   char* split_line_;
00101   // Pointers to the fields within split_line_;
00102   vcl_vector<char *> fields_;
00103 
00104   // May as well keep track of it...
00105   int line_number_;
00106 
00107   // Set to true when the current line is the last one.
00108   bool done_;
00109 
00110   void next();
00111 
00112   vul_awk(const vul_awk& that);
00113   vul_awk& operator=(const vul_awk& that);
00114 };
00115 
00116 #endif // vul_awk_h_

Generated on Tue Dec 2 05:08:47 2008 for core/vul by  doxygen 1.5.1