Open
Graph Drawing
Framework

 v.2012.05
 

DinoLineBuffer.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00044 #ifdef _MSC_VER
00045 #pragma once
00046 #endif
00047 
00048 #ifndef OGDF_DINO_LINE_BUFFER_H
00049 #define OGDF_DINO_LINE_BUFFER_H
00050 
00051 #include <ogdf/basic/basic.h>
00052 
00053 
00054 namespace ogdf {
00055 
00056     //---------------------------------------------------------
00057     // D i n o L i n e B u f f e r P o s i t i o n
00058     //---------------------------------------------------------
00066     class OGDF_EXPORT DinoLineBufferPosition {
00067         
00068     private:
00069 
00071         int m_lineNumber;
00072 
00076         int m_lineUpdateCount;
00077 
00079         int m_linePosition;
00080 
00081     public:
00082 
00084         DinoLineBufferPosition() : 
00085           m_lineNumber(0), 
00086           m_lineUpdateCount(0), 
00087           m_linePosition(0)
00088         {}
00089 
00091         DinoLineBufferPosition(int lineNumber, 
00092                                int lineUpdateCount, 
00093                                int linePosition);
00094 
00096         DinoLineBufferPosition(const DinoLineBufferPosition &position);
00097 
00099         inline int getLineNumber() const {
00100             return m_lineNumber;
00101         }
00102 
00104         inline int getLineUpdateCount() const {
00105             return m_lineUpdateCount;
00106         }
00107 
00109         inline int getLinePosition() const {
00110             return m_linePosition;
00111         }
00112 
00114         void set(int lineNumber, int lineUpdateCount, int linePosition);
00115 
00117         void incrementPosition();
00118 
00120         bool operator!=(const DinoLineBufferPosition &position) const;
00121 
00123         const DinoLineBufferPosition &operator=(const DinoLineBufferPosition &position);
00124 
00125     }; // DinoLineBufferPosition
00126 
00127     //---------------------------------------------------------
00128     // D i n o L i n e B u f f e r 
00129     //---------------------------------------------------------
00133     class OGDF_EXPORT DinoLineBuffer {
00134 
00135     public:
00136 
00137         // Maximal length of a string handled by extractString()
00138         const static int c_maxStringLength;
00139 
00140         // Maximal length of one line 
00141         const static int c_maxLineLength;
00142 
00143         // Maximal number of lines
00144         const static int c_maxNoOfLines;
00145 
00146     private:
00147 
00148         // Handle to the input file
00149         istream *m_pIs;
00150 
00151         // Contains for each line of the line buffer its update count
00152         // Range is [0 .. c_maxNoOfLines]
00153         int *m_lineUpdateCountArray;
00154 
00155         // Pointer to the line buffer
00156         char *m_pLinBuf;
00157 
00158         // The current position in m_pLinBuf
00159         DinoLineBufferPosition m_currentPosition;
00160 
00161         // The line which has been read from the file most recently; 
00162         // this does not have to be equal to m_currentPosition.m_lineNumber
00163         // because of the lookahead facilities.
00164         // Range is [0 .. c_maxNoOfLines - 1]
00165         int m_numberOfMostRecentlyReadLine;
00166 
00167         // Contains the current line number of the input file; 
00168         int m_inputFileLineCounter;
00169 
00170     public:
00171 
00172         // construction
00173         DinoLineBuffer(const char *fileName);
00174 
00175         // destruction
00176         ~DinoLineBuffer();
00177 
00178         // Returns the current position (as a copy)
00179         DinoLineBufferPosition getCurrentPosition() const{
00180             return m_currentPosition;
00181         }
00182 
00183         // Returns the character which is currently pointed to
00184         inline char getCurrentCharacter() const {
00185             return m_pLinBuf[(m_currentPosition.getLineNumber() * DinoLineBuffer::c_maxLineLength) + 
00186                                  m_currentPosition.getLinePosition()];
00187         }
00188 
00189         // Returns line number of the most recently read line of the input file
00190         inline int getInputFileLineCounter() const {
00191             return m_inputFileLineCounter;
00192         }
00193 
00194         // Moves to the next position;
00195         // reading of new lines and handling of eof are done internally.
00196         // If end of file is reached the position will stuck to EOF character.
00197         // The current character after moving is returned
00198         char moveToNextCharacter();
00199 
00200         // Sets the current position to new positon.
00201         // Takes care if the given newPosition is valid.
00202         // Returns false if given position is invalid
00203         bool setCurrentPosition(const DinoLineBufferPosition &newPosition);
00204 
00205         // Moves to the next character until the currentCharacter is
00206         // no whitespace.
00207         void skipWhitespace();
00208 
00209         // Copys the characters which have been extracted from the 
00210         // line buffer starting from position startPosition (including it) 
00211         // to endPosition (excluding it) to targetString (terminated by '\0').
00212         // The length of strings is limited to c_maxStringLength
00213         //
00214         // Returns false if the startPosition is not valid, i.e. the string
00215         // is too long; targetString will contain the message "String too long!"
00216         bool extractString(const DinoLineBufferPosition &startPostion,
00217                            const DinoLineBufferPosition &endPosition,
00218                            char *targetString);
00219 
00220     private:
00221 
00222         // Returns a pointer to the character which is currently pointed to
00223         inline char *getCurrentCharacterPointer() {
00224             return &m_pLinBuf[(m_currentPosition.getLineNumber() * DinoLineBuffer::c_maxLineLength) + 
00225                                    m_currentPosition.getLinePosition()];
00226         }
00227 
00228         // Sets the given character to the current position
00229         inline void setCurrentCharacter(char c) {
00230             m_pLinBuf[(m_currentPosition.getLineNumber() * DinoLineBuffer::c_maxLineLength) + 
00231                            m_currentPosition.getLinePosition()] = c;
00232         }
00233 
00234         // Checks wether the given position is valid
00235         bool isValidPosition(const DinoLineBufferPosition &position) const;
00236 
00237     }; // class DinoLineBuffer
00238 
00239 } // end namespace ogdf
00240 
00241 #endif