src/types.cpp

changeset 287
3fcccd8c3357
parent 286
7a562bf3d829
child 288
2980d7fd948e
equal deleted inserted replaced
286:7a562bf3d829 287:3fcccd8c3357
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include <QObject> 19 #include <QObject>
20 #include <QStringList> 20 #include <QStringList>
21 #include <QTextStream>
22 #include <qfile.h>
21 #include <assert.h> 23 #include <assert.h>
22 #include "common.h" 24 #include "common.h"
23 #include "types.h" 25 #include "types.h"
24 #include "misc.h" 26 #include "misc.h"
27
28 const File nullfile (null);
25 29
26 str DoFormat (vector<StringFormatArg> args) { 30 str DoFormat (vector<StringFormatArg> args) {
27 assert (args.size () >= 1); 31 assert (args.size () >= 1);
28 str text = args[0].value (); 32 str text = args[0].value ();
29 33
268 } 272 }
269 273
270 StringFormatArg::StringFormatArg (const floatconfig& v) { 274 StringFormatArg::StringFormatArg (const floatconfig& v) {
271 m_val.number (v.value); 275 m_val.number (v.value);
272 } 276 }
277
278 // =============================================================================
279 File::File (const std::nullptr_t&) {
280 // Make a null file
281 m_file = null;
282 m_textstream = null;
283 }
284
285 File::File (str path, OpenType rtype) {
286 m_file = null;
287 open (path, rtype);
288 }
289
290 File::File (FILE* fp, OpenType rtype) {
291 m_file = null;
292 open (fp, rtype);
293 }
294
295 File::~File () {
296 if (m_file) {
297 m_file->close ();
298 delete m_file;
299
300 if (m_textstream)
301 delete m_textstream;
302 }
303 }
304
305 bool File::open (FILE* fp, OpenType rtype) {
306 return open ("", rtype, fp);
307 }
308
309 bool File::open (str path, OpenType rtype, FILE* fp) {
310 close ();
311
312 if (!m_file)
313 m_file = new QFile;
314
315 m_file->setFileName (path);
316
317 bool result;
318
319 QIODevice::OpenMode mode =
320 (rtype == Read) ? QIODevice::ReadOnly :
321 (rtype == Write) ? QIODevice::WriteOnly :
322 QIODevice::Append;
323
324 if (fp)
325 result = m_file->open (fp, mode);
326 else
327 result = m_file->open (mode);
328
329 if (result) {
330 m_textstream = new QTextStream (m_file);
331 return true;
332 }
333
334 delete m_file;
335 m_file = null;
336 return false;
337 }
338
339 File::iterator File::begin() {
340 return iterator (this);
341 }
342
343 File::iterator& File::end () {
344 return m_endIterator;
345 }
346
347 void File::write (str msg) {
348 m_file->write (msg.toUtf8 (), msg.length ());
349 }
350
351 str File::readLine () {
352 return m_textstream->readLine ();
353 }
354
355 bool File::atEnd () const {
356 return m_textstream->atEnd ();
357 }
358
359 bool File::isNull () const {
360 return m_file == null;
361 }
362
363 bool File::operator!() const {
364 return isNull ();
365 }
366
367 void File::close () {
368 if (!m_file)
369 return;
370
371 delete m_file;
372 m_file = null;
373
374 if (m_textstream) {
375 delete m_textstream;
376 m_textstream = null;
377 }
378 }
379
380 bool File::flush () {
381 return m_file->flush ();
382 }
383
384 void File::iterator::operator++() {
385 m_text = m_file->readLine ();
386 }
387
388 str File::iterator::operator*() {
389 return m_text;
390 }
391
392 bool File::iterator::operator== (File::iterator& other) {
393 return (other.m_file == null && m_file->atEnd ());
394 }
395
396 bool File::iterator::operator!= (File::iterator& other) {
397 return !operator== (other);
398 }

mercurial