idep_fdepitr.cxx File Reference

#include "idep_filedepiter.h"
#include <ctype.h>
#include <cstring>
#include <memory>
#include <iostream>
#include <fstream>
#include <cassert>

Go to the source code of this file.

Classes

struct  idep_FileDepIter_i

Enumerations

enum  { MAX_LINE_LENGTH = 2048 }

Functions

static int loadBuf (std::istream &in, char *buf, int bufSize)
const char * extractDependency (char *buffer)

Enumeration Type Documentation

anonymous enum
Enumerator:
MAX_LINE_LENGTH 

Definition at line 13 of file idep_fdepitr.cxx.

00013 { MAX_LINE_LENGTH  = 2048 };       // Arbitrary maximum length for


Function Documentation

const char* extractDependency ( char *  buffer  ) 

Definition at line 47 of file idep_fdepitr.cxx.

Referenced by idep_FileDepIter::operator++().

00048 {
00049     // We assume we have a null terminated string that possibly contains a 
00050     // valid include directive.  We will assume that such a directive has 
00051     // the following syntax:
00052     // 
00053     // ^#[ \t]*"include"[ \t ]*[<"][ \t]*{filename}[>" \t\n]
00054     //                                   ~~~~~~~~~~
00055     // i.e.,                             ^want this
00056     //  1. The first character on the line MUST be a '#'
00057     //  2. This character may be followed by any number of spaces or tabs.
00058     //  3. The next non-whitespace char must be an 'i' followed by "nclude".
00059     //  4. This string may be followed by any number of spaces or tabs.
00060     //  5. The next character must be either a '<' or a '"'. 
00061     //  6. This character may be followed by any number of spaces or tabs.
00062     //  7. The {filename} follows and is terminated by whitespace, '>', or '"'.
00063     //
00064     // If an include directive is found, the buffer is modified and a pointer 
00065     // to the included filename is returned; otherwise 0 is returned. 
00066 
00067     if ('#' != buffer[0]) {                             // 1.
00068         return 0;
00069     }
00070 
00071     char *p = buffer;           
00072     while (isspace(*++p)) {                             // 2.
00073     }                           
00074 
00075     if ('i' != *p) {                                    // 3a.
00076         return 0;
00077     }
00078 
00079     static char KEY[] = "include";
00080     if (0 != strncmp(p, "include", sizeof KEY - 1)) {   // 3b.
00081         return 0;
00082     }
00083     p += sizeof KEY - 1;        // advance over KEY
00084 
00085     while (isspace(*p)) {                               // 4.
00086         ++p;
00087     }                           
00088 
00089     if ('<' != *p && '"' != *p) {                       // 5.
00090         return 0;
00091     }
00092     ++p;                        
00093 
00094     while (isspace(*p)) {                               // 6.
00095         ++p;
00096     }                           
00097 
00098     // At this point, p points to the start of the file name
00099     // all we need to do is detect the end of the string. 
00100 
00101     int length = strcspn(p, " \t\">"); 
00102     p[length] = 0;                                      // 7.
00103 
00104     return p;
00105 }

static int loadBuf ( std::istream &  in,
char *  buf,
int  bufSize 
) [static]

Definition at line 18 of file idep_fdepitr.cxx.

References SUCCESS.

Referenced by idep_FileDepIter::operator++().

00019 {
00020     enum { END_OF_INPUT = -1, SUCCESS = 0, OVERFLOWN = 1 };
00021  
00022     // The getline (std::istream) method returns the line as a string but extracts
00023     // the trailing '\n'  only if there is space to spare in the buffer.  We
00024     // need to test for this condition by placing a non-null character in the 
00025     // last position of the array before loading it.  If the last character is 
00026     // null after the read then the newline was NOT extracted.
00027  
00028     int nearEnd = bufSize - 1;
00029  
00030     buf[nearEnd] = '\n'; // anything but '\0'
00031  
00032     if (in.getline(buf, bufSize).fail()) {
00033         return END_OF_INPUT;    // nothing left
00034     }
00035  
00036     if ('\0' == buf[nearEnd]) { // line was too long
00037         char c;
00038         while (in && !in.get(c).eof() && c != '\n') { 
00039             // skip to next line
00040         }
00041         return OVERFLOWN;        // buffer contains null-terminated string
00042     }
00043 
00044     return SUCCESS; 
00045 }


Generated on 25 Apr 2018 for loon by  doxygen 1.6.1