Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
vul_awk Class Reference

The core of awk. More...

#include <vul_awk.h>

Public Types

enum  ModeFlags { none = 0x00, verbose = 0x01, strip_comments = 0x02, backslash_continuations = 0x04 }
 

Public Member Functions

 vul_awk (std::istream &s, ModeFlags mode=none)
 Construct from input stream. More...
 
 ~vul_awk ()
 
char const * operator[] (unsigned i) const
 Return field i. Counting starts at 0. More...
 
int NR () const
 Return the current "record number", i.e. line number. More...
 
int NF () const
 Return the number of fields on this line. More...
 
char const * line () const
 Return the entire line. More...
 
char const * line_from (int field_number) const
 Return the remainder of the line, starting from field_number. More...
 
 operator bool () const
 Return true if this line is not the last. More...
 
bool operator! () const
 Return false if this line is not the last. More...
 
vul_awkoperator++ ()
 Advance to the next line. More...
 
void error (std::ostream &, char const *message, int field=-1, int char_within_field=0)
 Display error message, line number. More...
 

Protected Member Functions

void next ()
 
 vul_awk (const vul_awk &that)
 
vul_awkoperator= (const vul_awk &that)
 

Protected Attributes

std::istream & fd_
 
ModeFlags mode_
 
std::string line_
 
char * split_line_
 
std::vector< char * > fields_
 
int line_number_
 
bool done_
 

Detailed Description

The core of awk.

vul_awk reads lines from a std::istream and breaks them into whitespace-separated fields. Its primary advantage is that its name defines the semantics of its methods—except that this C++ version uses zero-based fields. The usage is exemplified in this example, to print the second field in every line:

for (vul_awk awk=cin; awk; ++awk)
std::cout << awk[2] << std::endl;

The constructor takes an integer mode-flag variable. Right now, only the strip_comments flag has any effect, though the ModeFlags enumeration contains other potential flags.

When the strip_comments flag is set then everything from the first '#' character to the end of the line is replaced with a single space. As a special feature, lines that contain an # as the first character are skipped entirely by the next() routine, no attempt will be made to extract fields from them. They will be counted in the line numbering so that error messages can easily refer to the correct line in the file. To extend the above example to handle comments in the file, write:

for (vul_awk awk(cin, vul_awk::strip_comments); awk; ++awk)
std::cout << awk[2] << std::endl;

Definition at line 56 of file vul_awk.h.

Member Enumeration Documentation

◆ ModeFlags

Enumerator
none 
verbose 
strip_comments 
backslash_continuations 

Definition at line 61 of file vul_awk.h.

Constructor & Destructor Documentation

◆ vul_awk() [1/2]

vul_awk::vul_awk ( std::istream &  s,
ModeFlags  mode = none 
)

Construct from input stream.

Definition at line 24 of file vul_awk.cxx.

◆ ~vul_awk()

vul_awk::~vul_awk ( )

Definition at line 35 of file vul_awk.cxx.

◆ vul_awk() [2/2]

vul_awk::vul_awk ( const vul_awk that)
protected

Member Function Documentation

◆ error()

void vul_awk::error ( std::ostream &  ,
char const *  message,
int  field = -1,
int  char_within_field = 0 
)

Display error message, line number.

Also display optional field number and char within field.

◆ line()

char const* vul_awk::line ( ) const
inline

Return the entire line.

Definition at line 88 of file vul_awk.h.

◆ line_from()

char const * vul_awk::line_from ( int  field_number) const

Return the remainder of the line, starting from field_number.

(0 is from the first non-whitespace character)

Definition at line 123 of file vul_awk.cxx.

◆ next()

void vul_awk::next ( )
protected

Definition at line 40 of file vul_awk.cxx.

◆ NF()

int vul_awk::NF ( ) const
inline

Return the number of fields on this line.

Definition at line 85 of file vul_awk.h.

◆ NR()

int vul_awk::NR ( ) const
inline

Return the current "record number", i.e. line number.

Definition at line 82 of file vul_awk.h.

◆ operator bool()

vul_awk::operator bool ( ) const
inlineexplicit

Return true if this line is not the last.

Definition at line 95 of file vul_awk.h.

◆ operator!()

bool vul_awk::operator! ( ) const
inline

Return false if this line is not the last.

Definition at line 99 of file vul_awk.h.

◆ operator++()

vul_awk& vul_awk::operator++ ( )
inline

Advance to the next line.

Definition at line 103 of file vul_awk.h.

◆ operator=()

vul_awk& vul_awk::operator= ( const vul_awk that)
protected

◆ operator[]()

char const* vul_awk::operator[] ( unsigned  i) const
inline

Return field i. Counting starts at 0.

Definition at line 74 of file vul_awk.h.

Member Data Documentation

◆ done_

bool vul_awk::done_
protected

Definition at line 129 of file vul_awk.h.

◆ fd_

std::istream& vul_awk::fd_
protected

Definition at line 113 of file vul_awk.h.

◆ fields_

std::vector<char *> vul_awk::fields_
protected

Definition at line 123 of file vul_awk.h.

◆ line_

std::string vul_awk::line_
protected

Definition at line 118 of file vul_awk.h.

◆ line_number_

int vul_awk::line_number_
protected

Definition at line 126 of file vul_awk.h.

◆ mode_

ModeFlags vul_awk::mode_
protected

Definition at line 115 of file vul_awk.h.

◆ split_line_

char* vul_awk::split_line_
protected

Definition at line 121 of file vul_awk.h.


The documentation for this class was generated from the following files: