1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (c) 2001 Jani Kajala
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Jani Kajala makes no representations about the suitability
* of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "TextFile.h"
#include <stdio.h>
#include <ctype.h>
//-----------------------------------------------------------------------------
namespace dev
{
class TextFile::TextFileImpl
{
public:
TextFile::ErrorType err;
int line;
explicit TextFileImpl( const char* filename )
{
err = TextFile::ERROR_NONE;
line = 1;
m_peeked = false;
m_peekedChar = 0;
m_file = fopen( filename, "rt" );
if ( !m_file )
err = TextFile::ERROR_OPEN;
}
~TextFileImpl()
{
if ( m_file )
{
fclose( m_file );
m_file = 0;
}
}
bool eof() const
{
if ( err )
return true;
return 0 != feof(m_file);
}
bool peekChar( char* ch )
{
if ( err )
return false;
if ( !m_peeked )
{
int c = getc( m_file );
if ( EOF != c )
{
m_peeked = true;
m_peekedChar = (char)c;
}
else
{
if ( ferror(m_file) )
err = TextFile::ERROR_READ;
}
}
if ( m_peeked )
*ch = m_peekedChar;
return m_peeked;
}
bool readChar( char* ch )
{
if ( err )
return false;
bool more = peekChar( ch );
m_peeked = false;
if ( more && *ch == '\n' )
++line;
return more;
}
bool skipWhitespace()
{
if ( err )
return false;
char ch;
while ( peekChar(&ch) )
{
if ( !isspace(ch) )
break;
readChar( &ch );
}
return !eof();
}
bool readString( char* buf, int size )
{
if ( err )
return false;
skipWhitespace();
int count = 0;
char ch;
while ( peekChar(&ch) )
{
if ( isspace(ch) )
break;
if ( count+1 < size )
buf[count++] = ch;
readChar( &ch );
}
if ( size > 0 )
buf[count] = 0;
return count > 0;
}
void skipLine()
{
if ( err )
return;
char ch;
while ( readChar(&ch) )
{
if ( ch == '\n' )
break;
}
}
long readHex()
{
if ( err )
return 0;
skipWhitespace();
// hex must start with alphanumeric character
char ch;
if ( !peekChar(&ch) || !isalnum(ch) )
{
err = TextFile::ERROR_PARSE;
return 0;
}
long x = 0;
while ( peekChar(&ch) )
{
switch ( ch )
{
case '0': x <<= 4; x += 0; break;
case '1': x <<= 4; x += 1; break;
case '2': x <<= 4; x += 2; break;
case '3': x <<= 4; x += 3; break;
case '4': x <<= 4; x += 4; break;
case '5': x <<= 4; x += 5; break;
case '6': x <<= 4; x += 6; break;
case '7': x <<= 4; x += 7; break;
case '8': x <<= 4; x += 8; break;
case '9': x <<= 4; x += 9; break;
case 'a':
case 'A': x <<= 4; x += 0xA; break;
case 'b':
case 'B': x <<= 4; x += 0xB; break;
case 'c':
case 'C': x <<= 4; x += 0xC; break;
case 'd':
case 'D': x <<= 4; x += 0xD; break;
case 'e':
case 'E': x <<= 4; x += 0xE; break;
case 'f':
case 'F': x <<= 4; x += 0xF; break;
default: return x;
}
readChar( &ch );
}
return x;
}
private:
bool m_peeked;
char m_peekedChar;
FILE* m_file;
TextFileImpl( const TextFileImpl& );
TextFileImpl& operator=( const TextFileImpl& );
};
//-----------------------------------------------------------------------------
TextFile::TextFile( const char* filename )
{
m_this = new TextFileImpl( filename );
}
TextFile::~TextFile()
{
delete m_this;
}
bool TextFile::readString( char* buf, int size )
{
return m_this->readString( buf, size );
}
void TextFile::skipLine()
{
m_this->skipLine();
}
long TextFile::readHex()
{
return m_this->readHex();
}
bool TextFile::skipWhitespace()
{
return m_this->skipWhitespace();
}
TextFile::ErrorType TextFile::error() const
{
return m_this->err;
}
bool TextFile::readChar( char* ch )
{
return m_this->readChar( ch );
}
bool TextFile::peekChar( char* ch )
{
return m_this->peekChar( ch );
}
bool TextFile::eof() const
{
return m_this->eof();
}
int TextFile::line() const
{
return m_this->line;
}
} // dev
|