00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef SLURPEDFILE_H
00011 #define SLURPEDFILE_H SLURPEDFILE_H
00012 #include <vector>
00013 #include <fstream>
00014 #include <stdexcept>
00015 #include <string>
00016 #include "typedefs.h"
00017
00018 class file_not_found : public std::runtime_error {
00019 public: file_not_found(string s) : std::runtime_error(s) {}
00020 };
00021
00022 bool file_exists(const string& filename){
00023 std::ifstream f(filename.c_str());
00024 return f.is_open();
00025 }
00026
00027 slurpedfile slurp(const string& filename){
00028 slurpedfile fbuffer;
00029 std::ifstream f(filename.c_str(),std::ios::binary);
00030 if(!f.is_open()){
00031 throw file_not_found("error opening file " + filename);
00032 }
00033
00034
00035 int begin = f.tellg();
00036 f.seekg (0, std::ios::end);
00037 int end = f.tellg();
00038 f.seekg (0, std::ios::beg);
00039 fbuffer.resize(end - begin);
00040 f.read(reinterpret_cast<char*>(&fbuffer[0]),fbuffer.size());
00041 f.close();
00042 return fbuffer;
00043 }
00044
00045
00046
00047 bool slurptofile(const string& filename, const std::string& content){
00048 std::ofstream f(filename.c_str(),std::ios::binary);
00049 if(!f.is_open()) return false;
00050 f << content;
00051 f.close();
00052 return true;
00053 }
00054
00055 #endif