FileUtils.cpp

Go to the documentation of this file.
00001 
00009 #include "FileUtils.h"
00010 #include "Certificate.h"
00011 #include "ToolkitUtils.h"
00012 #include "components.h"
00013 #include "PKIFException.h"
00014 #include "PKIFErrors.h"
00015 #include "Buffer.h"
00016 
00017 #include "boost/filesystem/path.hpp"
00018 #include "boost/filesystem/operations.hpp"
00019 #include "boost/filesystem/fstream.hpp"
00020 #include <iostream>
00021 #include <sstream>
00022 
00023 #include "boost/numeric/conversion/cast.hpp"
00024 #include "boost/numeric/conversion/bounds.hpp"
00025 #include "boost/limits.hpp"
00026 
00027 using boost::numeric_cast;
00028 using boost::bad_numeric_cast;
00029 
00030 using namespace std;
00031 namespace fs = boost::filesystem;
00032 
00044 CPKIFBufferPtr CAC_API ReadFileIntoBuffer(
00046     const std::string & filename) throw(CPKIFException)
00047 {
00048     fs::path filepath;
00049     boost::intmax_t filelen = 0;
00050     try {
00051         filepath = fs::path(filename,fs::native);
00052         //file_size() will throw if it's not a valid file
00053         filelen = fs::file_size(filepath);
00054     } catch(...) {
00055         throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_INVALID_INPUT,
00056             "Invalid filename passed to DecodeCertFromFile()");
00057     }
00058 
00059     int tmpST = 0;
00060     try 
00061     {
00062         tmpST = numeric_cast<int>(filelen);
00063     }
00064     catch(bad_numeric_cast &) 
00065     {
00066         throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "File size too big.");
00067     }
00068 
00069     fs::ifstream infile;
00070     try {
00071         // on windows ios_base::binary definitely matters
00072         infile.open(filepath,ios_base::binary);
00073     } catch(std::exception & e) {
00074         ostringstream os;
00075         os << "Error opening " << filename << " for reading: " << e.what();
00076         throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_INVALID_INPUT,
00077             os.str().c_str());
00078     }
00079     unsigned char * filebuf = new unsigned char[tmpST];
00080     infile.read((char *)filebuf,tmpST);
00081     infile.close();
00082     // the returned buffer gets ownership and will delete filebuf
00083     CPKIFBufferPtr returnbuf(new CPKIFBuffer(true,filebuf,tmpST));
00084     return returnbuf;
00085 }
00086 
00087 
00101 CPKIFCertificatePtr DecodeCertFromFile(
00103     const std::string & filename) throw(CPKIFException)
00104 {
00105     CPKIFBufferPtr fileBuf = ReadFileIntoBuffer(filename);
00106     CPKIFCertificatePtr cert(new CPKIFCertificate());
00107     cert->Decode(fileBuf->GetBuffer(),fileBuf->GetLength());
00108     return cert;
00109 }
00120 void WriteBufferToFile(
00122     const std::string & filename,
00124     CPKIFBufferPtr &buf)
00125 {
00126     fs::path filepath;
00127     size_t filelen = 0;
00128     try {
00129         filepath = fs::path(filename,fs::native);
00130     } catch(...) {
00131         throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_INVALID_INPUT,
00132             "Invalid filename passed to WriteBufferToFile()");
00133     }
00134     fs::ofstream outfile;
00135     try {
00136         // on windows ios_base::binary definitely matters
00137         outfile.open(filepath,ios_base::binary | ios::out );
00138     } catch(std::exception & e) {
00139         ostringstream os;
00140         os << "Error opening " << filename << " for writing: " << e.what();
00141         throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_INVALID_INPUT,
00142             os.str().c_str());
00143     }
00144     try {
00145         outfile.write((char *)buf->GetBuffer(),buf->GetLength());
00146         outfile.close();
00147     } catch(std::exception & e) {
00148         ostringstream os;
00149         os << "Error writing to " << filename << " : " << e.what();
00150         throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_OPERATION_NOT_SUCCESSFUL,
00151             os.str().c_str());
00152     }
00153 }

Generated on Mon Nov 15 11:15:50 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6