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 } |