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

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

sys: don’t use fopen on the PS3.

  • Property svn:keywords set to Id
File size: 2.1 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 __CELLOS_LV2__
27#elif HAVE_STDIO_H
28        /* FIXME: no modes, no error checking, no nothing */
29        m_fd = fopen(file.C(), "r");
30#endif
31    }
32
33    String ReadString()
34    {
35        String ret;
36#if __CELLOS_LV2__
37#elif HAVE_STDIO_H
38        while (!feof(m_fd))
39        {
40            char buf[BUFSIZ];
41            size_t count = fread(buf, 1, BUFSIZ, m_fd);
42            if (count <= 0)
43                break;
44
45            int oldsize = ret.Count();
46            ret.Resize(oldsize + count);
47            memcpy(&ret[oldsize], buf, count);
48        }
49#endif
50        return ret;
51    }
52
53    void Close()
54    {
55#if __CELLOS_LV2__
56#elif HAVE_STDIO_H
57        fclose(m_fd);
58#endif
59    }
60
61#if __CELLOS_LV2__
62#elif HAVE_STDIO_H
63    FILE *m_fd;
64#endif
65    Atomic<int> m_refcount;
66};
67
68File::File()
69  : m_data(new FileData)
70{
71    ++m_data->m_refcount;
72}
73
74File::File(File const &that)
75  : m_data(that.m_data)
76{
77    ++m_data->m_refcount;
78}
79
80File &File::operator =(File const &that)
81{
82    if (this == &that)
83        return *this;
84
85    int refcount = --m_data->m_refcount;
86    if (refcount == 0)
87    {
88        m_data->Close();
89        delete m_data;
90    }
91
92    m_data = that.m_data;
93    ++m_data->m_refcount;
94
95    return *this;
96}
97
98File::~File()
99{
100    int refcount = --m_data->m_refcount;
101    if (refcount == 0)
102    {
103        m_data->Close();
104        delete m_data;
105    }
106}
107
108void File::Open(String const &file, FileAccess mode)
109{
110    return m_data->Open(file, mode);
111}
112
113String File::ReadString()
114{
115    return m_data->ReadString();
116}
117
118void File::Close()
119{
120    m_data->Close();
121}
122
123} /* namespace lol */
124
Note: See TracBrowser for help on using the repository browser.