00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "data/file.hpp"
00035 #include "data/exception.hpp"
00036 #include "data/pointer.hpp"
00037 #include "data/fstream.hpp"
00038
00039 #include <cstdlib>
00040 #include <cstdio>
00041 #include <cstring>
00042 #include <cerrno>
00043
00044 #ifdef WIN32
00045 #define WIN32_LEAN_AND_MEAN
00046 #include <windows.h>
00047 #include <direct.h>
00048 #include <io.h>
00049 #endif
00050
00051 namespace gsgl
00052 {
00053
00054 namespace io
00055 {
00056
00057 const gsgl::index_t MAX_PATH_SIZE = 1024;
00058
00059 file::file()
00060 : data_object()
00061 {
00062 }
00063
00064 file::file(const string & fname)
00065 : data_object(), name(fname)
00066 {
00067 #ifdef WIN32
00068 data::smart_pointer<wchar_t, true> buf(new wchar_t[MAX_PATH_SIZE]);
00069 wchar_t *base;
00070
00071 GetFullPathName(fname.w_string(), MAX_PATH_SIZE, buf, &base);
00072 if (!base)
00073 throw runtime_exception(L"no file in path %ls", fname.w_string());
00074
00075 base_name = string(base);
00076 full_path = string(buf);
00077
00078 *base = 0;
00079 dir_name = string(buf);
00080 string last = dir_name.right_substring(1);
00081 if (!last.is_empty() && !(last == L"\\" || last == L"/") )
00082 dir_name += directory::SEPARATOR;
00083
00084 dir = directory(dir_name);
00085 #else
00086 #error file::file() not implemented
00087 #endif
00088 }
00089
00090 file::file(const file & f)
00091 : data_object(), name(f.name), base_name(f.base_name), dir_name(f.dir_name), full_path(f.full_path), dir(f.dir)
00092 {
00093 }
00094
00095 file::~file()
00096 {
00097 }
00098
00099 file & file::operator= (const file & f)
00100 {
00101 name = f.name;
00102 base_name = f.base_name;
00103 dir_name = f.dir_name;
00104 full_path = f.full_path;
00105
00106 dir = f.dir;
00107
00108 return *this;
00109 }
00110
00111
00112 bool file::operator== (const file & f) const
00113 {
00114 return full_path == f.full_path;
00115 }
00116
00117
00118 bool file::operator!= (const file & f) const
00119 {
00120 return full_path != f.full_path;
00121 }
00122
00123
00124 ft_stream *file::open_text(gsgl::flags_t mode)
00125 {
00126 return new ft_stream(name, mode);
00127 }
00128
00129
00130
00131
00132
00133
00134 const string & file::get_name() const
00135 {
00136 return name;
00137 }
00138
00139 const string & file::get_base_name() const
00140 {
00141 return base_name;
00142 }
00143
00144 const string & file::get_dir_name() const
00145 {
00146 return dir_name;
00147 }
00148
00149 const string & file::get_full_path() const
00150 {
00151 return full_path;
00152 }
00153
00154 const directory & file::get_directory() const
00155 {
00156 return dir;
00157 }
00158
00159
00160
00161 string file::get_full_path(const string & fname)
00162 {
00163 file f(fname);
00164 return f.get_full_path();
00165 }
00166
00167
00168 bool file::exists(const string & fname)
00169 {
00170 return ::access(fname.c_string(), 00) == 0;
00171 }
00172
00173
00174 void file::remove(const string & fname)
00175 {
00176 if (::unlink(fname.c_string()) != 0)
00177 throw io_exception(L"Unable to delete file %ls: %hs.", fname.w_string(), ::strerror(errno));
00178 }
00179
00180
00181 static const gsgl::index_t BUF_LEN = 4096;
00182
00183 void file::copy(const string & src_path, const string & dest_path)
00184 {
00185 data::smart_pointer<char, true> buf(new char[BUF_LEN]);
00186
00187 FILE *fin = ::fopen(src_path.c_string(), "rb");
00188 if (!fin)
00189 throw io_exception(L"Unable to open %hs: %hs.", src_path.c_string(), ::strerror(errno));
00190
00191 FILE *fout = ::fopen(dest_path.c_string(), "wb");
00192 if (!fout)
00193 throw io_exception(L"Unable to open %hs: %hs.", dest_path.c_string(), ::strerror(errno));
00194
00195 size_t num_read;
00196 while ((num_read = ::fread(buf, sizeof(char), BUF_LEN, fin)))
00197 {
00198 size_t num_written = ::fwrite(buf, sizeof(char), num_read, fout);
00199 if (num_written != num_read)
00200 {
00201 fclose(fout);
00202 fclose(fin);
00203 throw io_exception(L"Error copying %hs to %hs: %hs.", src_path.c_string(), dest_path.c_string(), ::strerror(errno));
00204 }
00205 }
00206
00207 fclose(fout);
00208 fclose(fin);
00209 }
00210
00211 }
00212
00213 }