src/types.cpp

changeset 288
2980d7fd948e
parent 287
3fcccd8c3357
child 310
c62edce5668c
equal deleted inserted replaced
287:3fcccd8c3357 288:2980d7fd948e
23 #include <assert.h> 23 #include <assert.h>
24 #include "common.h" 24 #include "common.h"
25 #include "types.h" 25 #include "types.h"
26 #include "misc.h" 26 #include "misc.h"
27 27
28 const File nullfile (null); 28 const File nullfile;
29 29
30 str DoFormat (vector<StringFormatArg> args) { 30 str DoFormat (vector<StringFormatArg> args) {
31 assert (args.size () >= 1); 31 assert (args.size () >= 1);
32 str text = args[0].value (); 32 str text = args[0].value ();
33 33
274 StringFormatArg::StringFormatArg (const floatconfig& v) { 274 StringFormatArg::StringFormatArg (const floatconfig& v) {
275 m_val.number (v.value); 275 m_val.number (v.value);
276 } 276 }
277 277
278 // ============================================================================= 278 // =============================================================================
279 File::File (const std::nullptr_t&) { 279 File::File () {
280 // Make a null file 280 // Make a null file
281 m_file = null; 281 m_file = null;
282 m_textstream = null; 282 m_textstream = null;
283 } 283 }
284 284
346 346
347 void File::write (str msg) { 347 void File::write (str msg) {
348 m_file->write (msg.toUtf8 (), msg.length ()); 348 m_file->write (msg.toUtf8 (), msg.length ());
349 } 349 }
350 350
351 str File::readLine () { 351 bool File::readLine (str& line) {
352 return m_textstream->readLine (); 352 if (!m_textstream || m_textstream->atEnd ())
353 return false;
354
355 line = m_textstream->readLine ();
356 return true;
353 } 357 }
354 358
355 bool File::atEnd () const { 359 bool File::atEnd () const {
356 return m_textstream->atEnd (); 360 if (!m_textstream)
361 fatal ("cannot use atEnd on a null file");
362
363 return m_textstream->atEnd ();
357 } 364 }
358 365
359 bool File::isNull () const { 366 bool File::isNull () const {
360 return m_file == null; 367 return m_file == null;
361 } 368 }
379 386
380 bool File::flush () { 387 bool File::flush () {
381 return m_file->flush (); 388 return m_file->flush ();
382 } 389 }
383 390
384 void File::iterator::operator++() { 391 File::operator bool () const {
385 m_text = m_file->readLine (); 392 return !isNull ();
386 } 393 }
387 394
388 str File::iterator::operator*() { 395 void File::rewind () {
396 m_file->seek (0);
397 }
398
399 File::iterator::iterator (File* f) : m_file (f) {
400 operator++ ();
401 }
402
403 void File::iterator::operator++ () {
404 m_gotdata = m_file->readLine (m_text);
405 }
406
407 str File::iterator::operator* () {
389 return m_text; 408 return m_text;
390 } 409 }
391 410
411 // The prime contestant for the weirdest operator== 2013 award?
392 bool File::iterator::operator== (File::iterator& other) { 412 bool File::iterator::operator== (File::iterator& other) {
393 return (other.m_file == null && m_file->atEnd ()); 413 return (other.m_file == null && !m_gotdata);
394 } 414 }
395 415
396 bool File::iterator::operator!= (File::iterator& other) { 416 bool File::iterator::operator!= (File::iterator& other) {
397 return !operator== (other); 417 return !operator== (other);
398 } 418 }

mercurial