00001 #include "System.hpp"
00002 #include "Exception.hpp"
00003 #include <cassert>
00004
00005 namespace aitools {
00006 namespace invertedindex {
00007
00008 uint64_t
00009 System::directory_size(const bfs::path& path) throw (std::invalid_argument)
00010 {
00011 if (!bfs::is_directory(path))
00012 {
00013 Exception::throw_invalid_argument("No regular directory", path);
00014 }
00015 uint64_t size(0);
00016 const bfs::directory_iterator end;
00017 for (bfs::directory_iterator it(path); it != end; ++it)
00018 {
00019 size += bfs::file_size(it->path());
00020 }
00021 return size;
00022 }
00023
00024 void
00025 System::fclose(FILE* file)
00026 {
00027 if (file == NULL) return;
00028 std::fclose(file);
00029 }
00030
00031 FILE*
00032 System::fopen(const bfs::path& path, const std::string& mode)
00033 throw (std::invalid_argument)
00034 {
00035 FILE* file(std::fopen(path.string().c_str(), mode.c_str()));
00036 if (file == NULL)
00037 {
00038 Exception::throw_invalid_argument("std::fopen() failed", path);
00039 }
00040 return file;
00041 }
00042
00043 void
00044 System::fread(void* data, size_t size, size_t count, FILE* file)
00045 throw (std::runtime_error)
00046 {
00047 assert(data != NULL);
00048 assert(file != NULL);
00049 if (std::fread(data, size, count, file) != count)
00050 {
00051 Exception::throw_runtime_error("std::fread() failed");
00052 }
00053 }
00054
00055 void
00056 System::fwrite(const void* data, size_t size, size_t count, FILE* file)
00057 throw (std::runtime_error)
00058 {
00059 assert(data != NULL);
00060 assert(file != NULL);
00061 if (std::fwrite(data, size, count, file) != count)
00062 {
00063 Exception::throw_runtime_error("std::fwrite() failed");
00064 }
00065 }
00066
00067 void
00068 System::fseek(FILE* file, long offset, int origin)
00069 throw (std::runtime_error)
00070 {
00071 assert(file != NULL);
00072 if (std::fseek(file, offset, origin) != 0)
00073 {
00074 Exception::throw_runtime_error("std::fseek() failed");
00075 }
00076 }
00077
00078 long
00079 System::ftell(FILE* file) throw (std::runtime_error)
00080 {
00081 assert(file != NULL);
00082 long offset(std::ftell(file));
00083 if (offset == -1)
00084 {
00085 Exception::throw_runtime_error("std::ftell() failed");
00086 }
00087 return offset;
00088 }
00089
00090 FILE*
00091 System::tmpfile() throw (std::runtime_error)
00092 {
00093 FILE* file(std::tmpfile());
00094 if (file == NULL)
00095 {
00096 Exception::throw_runtime_error("std::tmpfile() failed");
00097 }
00098 return file;
00099 }
00100
00101 }
00102 }