source: trunk/src/sys/file.cpp @ 2361

Last change on this file since 2361 was 2361, checked in by sam, 8 years ago

sys: prototype for the file reading functions.

  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16
17namespace lol
18{
19
20class FileData
21{
22    friend class File;
23
24    void Open(String const &file, FileAccess mode)
25    {
26#if HAVE_STDIO_H
27        /* FIXME: no modes, no error checking, no nothing */
28        m_fd = fopen(file.C(), "r");
29#endif
30    }
31
32    String ReadString()
33    {
34        String ret;
35#if HAVE_STDIO_H
36        while (!feof(m_fd))
37        {
38            char buf[BUFSIZ];
39            size_t count = fread(buf, 1, BUFSIZ, m_fd);
40            if (count <= 0)
41                break;
42
43            int oldsize = ret.Count();
44            ret.Resize(oldsize + count);
45            memcpy(&ret[oldsize], buf, count);
46        }
47#endif
48        return ret;
49    }
50
51    void Close()
52    {
53#if HAVE_STDIO_H
54        fclose(m_fd);
55#endif
56    }
57
58#if HAVE_STDIO_H
59    FILE *m_fd;
60#endif
61    Atomic<int> m_refcount;
62};
63
64File::File()
65  : m_data(new FileData)
66{
67    ++m_data->m_refcount;
68}
69
70File::File(File const &that)
71  : m_data(that.m_data)
72{
73    ++m_data->m_refcount;
74}
75
76File &File::operator =(File const &that)
77{
78    if (this == &that)
79        return *this;
80
81    int refcount = --m_data->m_refcount;
82    if (refcount == 0)
83    {
84        m_data->Close();
85        delete m_data;
86    }
87
88    m_data = that.m_data;
89    ++m_data->m_refcount;
90
91    return *this;
92}
93
94File::~File()
95{
96    int refcount = --m_data->m_refcount;
97    if (refcount == 0)
98    {
99        m_data->Close();
100        delete m_data;
101    }
102}
103
104void File::Open(String const &file, FileAccess mode)
105{
106    return m_data->Open(file, mode);
107}
108
109String File::ReadString()
110{
111    return m_data->ReadString();
112}
113
114void File::Close()
115{
116    m_data->Close();
117}
118
119} /* namespace lol */
120
Note: See TracBrowser for help on using the repository browser.