Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vul_awk.cxx

Go to the documentation of this file.
00001 // This is core/vul/vul_awk.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 //
00008 // \author Andrew W. Fitzgibbon, Oxford RRG
00009 // \date   17 May 97
00010 //
00011 //-----------------------------------------------------------------------------
00012 
00013 #include "vul_awk.h"
00014 
00015 #include <vcl_cctype.h>
00016 #include <vcl_cstring.h>
00017 #include <vcl_iostream.h>
00018 #include <vcl_cstdio.h> // for EOF
00019 
00020 //: Construct from input stream
00021 vul_awk::vul_awk(vcl_istream& s, ModeFlags mode):
00022   fd_(s),
00023   mode_(mode)
00024 {
00025   done_ = false;
00026   line_number_ = 0;
00027   split_line_ = 0;
00028 
00029   next();
00030 }
00031 
00032 vul_awk::~vul_awk()
00033 {
00034   delete [] split_line_;
00035 }
00036 
00037 void vul_awk::next()
00038 {
00039   // Read line -- should be quite fast after the first one.
00040   line_.erase();
00041   //bool do_backslash_continuations = (int(mode_) & int(backslash_continuations)) != 0;
00042   //  bool do_strip_comments = (int(mode_) & int(strip_comments)) != 0;
00043 
00044   while (true) {
00045     int c = fd_.get();
00046     if (c == EOF  ||  fd_.eof()) {
00047       done_ = true;
00048       break;
00049     }
00050     if (c == '\n')
00051       break;
00052     line_ += c;
00053   }
00054 
00055   char const* linep = line_.c_str();
00056 
00057   // copy string
00058   delete [] split_line_;
00059   split_line_ = new char[line_.size() + 1];
00060   vcl_strcpy(split_line_, linep);
00061 
00062   // Chop line up into fields
00063   fields_.clear();
00064   char* cp = split_line_;
00065 
00066   while (true) {
00067     // Eat white
00068     while (*cp && vcl_isspace(*cp))
00069       ++cp;
00070     if (!*cp) break;
00071 
00072     // Push
00073     fields_.push_back(cp);
00074 
00075     // Find nonwhite
00076     while (*cp && !vcl_isspace(*cp))
00077       ++cp;
00078     if (!*cp) break;
00079 
00080     // Zap space
00081     *cp++ = '\0';
00082   }
00083 
00084   // Increment line number
00085   ++line_number_;
00086 }
00087 
00088 char const* vul_awk::line_from(int field_number) const
00089 {
00090   char const *p = line_.c_str();
00091   if (field_number >= NF())
00092     field_number = NF() - 1;
00093   if (field_number < 0) {
00094     vcl_cerr << "vul_awk::line_from("<< field_number <<") -- ZOIKS\n";
00095     return line();
00096   }
00097 
00098   return p + (fields_[field_number] - split_line_);
00099 }
00100 
00101 void testvul_awk()
00102 {
00103   vcl_cout << "Start\n";
00104   for (vul_awk awk(vcl_cin); awk; ++awk) {
00105     vcl_cout << awk.NF() << ':' << awk[2] << vcl_endl;
00106   }
00107 }

Generated on Thu Jan 10 14:41:00 2008 for core/vul by  doxygen 1.4.4