This commit is contained in:
Nekotekina 2015-04-20 01:26:28 +03:00
parent 339021ac15
commit 3b26031692
11 changed files with 471 additions and 448 deletions

View file

@ -48,47 +48,47 @@ namespace fmt
// the stream `os`. Then write `arg` to to the stream. If there's no
// `fmt::placeholder` after `pos` everything in `fmt` after pos is written
// to `os`. Then `arg` is written to `os` after appending a space character
template<typename T>
empty_t write(const std::string &fmt, std::ostream &os, std::string::size_type &pos, T &&arg)
{
std::string::size_type ins = fmt.find(placeholder, pos);
//template<typename T>
//empty_t write(const std::string &fmt, std::ostream &os, std::string::size_type &pos, T &&arg)
//{
// std::string::size_type ins = fmt.find(placeholder, pos);
if (ins == std::string::npos)
{
os.write(fmt.data() + pos, fmt.size() - pos);
os << ' ' << arg;
// if (ins == std::string::npos)
// {
// os.write(fmt.data() + pos, fmt.size() - pos);
// os << ' ' << arg;
pos = fmt.size();
}
else
{
os.write(fmt.data() + pos, ins - pos);
os << arg;
// pos = fmt.size();
// }
// else
// {
// os.write(fmt.data() + pos, ins - pos);
// os << arg;
pos = ins + placeholder.size();
}
return{};
}
// pos = ins + placeholder.size();
// }
// return{};
//}
// typesafe version of a sprintf-like function. Returns the printed to
// string. To mark positions where the arguments are supposed to be
// inserted use `fmt::placeholder`. If there's not enough placeholders
// the rest of the arguments are appended at the end, seperated by spaces
template<typename ... Args>
std::string SFormat(const std::string &fmt, Args&& ... parameters)
{
std::ostringstream os;
std::string::size_type pos = 0;
std::initializer_list<empty_t> { write(fmt, os, pos, parameters)... };
//template<typename ... Args>
//std::string SFormat(const std::string &fmt, Args&& ... parameters)
//{
// std::ostringstream os;
// std::string::size_type pos = 0;
// std::initializer_list<empty_t> { write(fmt, os, pos, parameters)... };
if (!fmt.empty())
{
os.write(fmt.data() + pos, fmt.size() - pos);
}
// if (!fmt.empty())
// {
// os.write(fmt.data() + pos, fmt.size() - pos);
// }
std::string result = os.str();
return result;
}
// std::string result = os.str();
// return result;
//}
//small wrapper used to deal with bitfields
template<typename T>

View file

@ -92,17 +92,11 @@ bool truncate_file(const std::string& file, uint64_t length)
bool get_file_info(const std::string& path, FileInfo& info)
{
info.name = path;
info.exists = false;
info.isDirectory = false;
info.isWritable = false;
info.size = 0;
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs))
{
info = {};
return false;
}
@ -117,6 +111,7 @@ bool get_file_info(const std::string& path, FileInfo& info)
struct stat64 file_info;
if (stat64(path.c_str(), &file_info) < 0)
{
info = {};
return false;
}
@ -128,7 +123,6 @@ bool get_file_info(const std::string& path, FileInfo& info)
info.mtime = file_info.st_mtime;
info.ctime = file_info.st_ctime;
#endif
return true;
}
@ -460,19 +454,13 @@ bool rfile_t::trunc(u64 size) const
bool rfile_t::stat(FileInfo& info) const
{
info.name.clear(); // possibly, TODO
info.exists = false;
info.isDirectory = false;
info.isWritable = false;
info.size = 0;
#ifdef _WIN32
FILE_BASIC_INFO basic_info;
//FILE_NAME_INFO name_info;
if (!GetFileInformationByHandleEx(fd, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO)))
{
info = {};
return false;
}
@ -487,10 +475,7 @@ bool rfile_t::stat(FileInfo& info) const
struct stat64 file_info;
if (fstat64(fd, &file_info) < 0)
{
info.exists = false;
info.isDirectory = false;
info.isWritable = false;
info.size = 0;
info = {};
return false;
}
@ -502,7 +487,6 @@ bool rfile_t::stat(FileInfo& info) const
info.mtime = file_info.st_mtime;
info.ctime = file_info.st_ctime;
#endif
return true;
}

View file

@ -2,7 +2,6 @@
struct FileInfo
{
std::string name;
bool exists;
bool isDirectory;
bool isWritable;
@ -50,29 +49,33 @@ struct rfile_t final
private:
handle_type fd;
#ifndef _WIN32
handle_type pad;
#endif
public:
rfile_t();
~rfile_t();
explicit rfile_t(const std::string& filename, u32 mode = o_read);
rfile_t(const rfile_t&) = delete;
rfile_t(rfile_t&&) = delete;
rfile_t(rfile_t&&) = delete; // possibly TODO
rfile_t& operator =(const rfile_t&) = delete;
rfile_t& operator =(rfile_t&&) = delete;
rfile_t& operator =(rfile_t&&) = delete; // possibly TODO
operator bool() const;
operator bool() const; // check is_opened()
void import(handle_type fd); // replace file handle
void import(handle_type fd);
bool open(const std::string& filename, u32 mode = o_read);
bool is_opened() const;
bool trunc(u64 size) const;
bool stat(FileInfo& info) const;
bool is_opened() const; // check whether the file is opened
bool trunc(u64 size) const; // change file size (possibly appending zero bytes)
bool stat(FileInfo& info) const; // get file info
bool close();
u64 read(void* buffer, u64 count) const;
u64 write(const void* buffer, u64 count) const;
//u64 write(const std::string& str) const;
u64 seek(u64 offset, u32 mode = from_begin) const;
u64 size() const;
};
@ -105,109 +108,3 @@ struct rFileName
void *handle;
};
// TODO: eliminate this:
template<typename T> __forceinline u8 Read8(T& f)
{
u8 ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u16 Read16(T& f)
{
be_t<u16> ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u32 Read32(T& f)
{
be_t<u32> ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u64 Read64(T& f)
{
be_t<u64> ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u16 Read16LE(T& f)
{
u16 ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u32 Read32LE(T& f)
{
u32 ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline u64 Read64LE(T& f)
{
u64 ret;
f.Read(&ret, sizeof(ret));
return ret;
}
template<typename T> __forceinline void Write8(T& f, const u8 data)
{
f.Write(&data, sizeof(data));
}
__forceinline void Write8(const rfile_t& f, const u8 data)
{
f.write(&data, sizeof(data));
}
template<typename T> __forceinline void Write16LE(T& f, const u16 data)
{
f.Write(&data, sizeof(data));
}
__forceinline void Write16LE(const rfile_t& f, const u16 data)
{
f.write(&data, sizeof(data));
}
template<typename T> __forceinline void Write32LE(T& f, const u32 data)
{
f.Write(&data, sizeof(data));
}
__forceinline void Write32LE(const rfile_t& f, const u32 data)
{
f.write(&data, sizeof(data));
}
template<typename T> __forceinline void Write64LE(T& f, const u64 data)
{
f.Write(&data, sizeof(data));
}
__forceinline void Write64LE(const rfile_t& f, const u64 data)
{
f.write(&data, sizeof(data));
}
template<typename T> __forceinline void Write16(T& f, const u16 data)
{
Write16LE(f, re16(data));
}
template<typename T> __forceinline void Write32(T& f, const u32 data)
{
Write32LE(f, re32(data));
}
template<typename T> __forceinline void Write64(T& f, const u64 data)
{
Write64LE(f, re64(data));
}