libs/EDocBFD/BFDArchive.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 Brendon Costa
00004 
00005    This code is free software; you can redistribute it and/or modify 
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This code is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this code; see the file COPYING.  If not, write to
00017    the Free Software Foundation, 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 
00020 *******************************************************************************/
00021 #include "config.h"
00022 
00023 #include "EDocBFD/BFDArchive.h"
00024 
00025 #include "EDocBFD/bfd_utils.h"
00026 #include "EDocBFD/exceptions.h"
00027 #include "EDoc/utils.h"
00028 #include "EDoc/persistence_data.h"
00029 
00030 #ifdef HAVE_FCNTL_H
00031    #include <fcntl.h>
00032 #endif
00033 
00034 #ifdef HAVE_UNISTD_H
00035    #include <unistd.h>
00036 #endif
00037 
00038 #ifdef HAVE_ERRNO_H
00039    #include <errno.h>
00040 #endif
00041 
00042 namespace EDocBFD
00043 {
00044 
00045    //===========================================================================
00046    ExtractedBFDFile::ExtractedBFDFile(std::string temp_file_name_in) :
00047       sync(false),
00048       temp_file_name(temp_file_name_in)
00049    {
00050       EDOC_Fine("Create new BFD Read tempfile: " << temp_file_name);
00051       EDoc::Persistence::AddTempFile(temp_file_name);
00052    }
00053    //===========================================================================
00054    ExtractedBFDFile::ExtractedBFDFile(std::string bfd_file_name_in, 
00055          std::string temp_file_name_in,
00056          std::string embedded_file_name_in) :
00057       
00058       sync(true),
00059       bfd_file_name(bfd_file_name_in),
00060       temp_file_name(temp_file_name_in),
00061       embedded_file_name(embedded_file_name_in)
00062    {
00063       EDOC_Fine("Create new BFD Write tempfile: " << temp_file_name);
00064       EDoc::Persistence::AddTempFile(temp_file_name);
00065    }
00066    //===========================================================================
00067    ExtractedBFDFile::~ExtractedBFDFile()
00068    {
00069       if (sync)
00070       {
00071          EDOC_Fine("Syncing write tempfile: " << temp_file_name);
00072          EmbedFileInBinary(temp_file_name, 
00073             embedded_file_name,
00074             bfd_file_name, 
00075             EDOC_SECTION_NAME);
00076       }
00077       
00078       EDOC_Fine("Erasing tempfile: " << temp_file_name);
00079       unlink(temp_file_name.c_str());
00080       EDoc::Persistence::RemoveTempFile(temp_file_name);
00081    }
00082 
00083    //===========================================================================
00084    //===========================================================================
00085    BFDArchive::BFDArchive()
00086    {
00087       InitialiseBFD();
00088    }
00089    //===========================================================================
00090    bool BFDArchive::HandlesType(const EDoc::ManagedFile& filename)
00091    {
00092       try
00093       {
00094          struct bfd* abfd = OpenBFDFile(filename.GetWorkFileName());
00095          bfd_close(abfd);
00096          return abfd;
00097       }
00098       catch (BFDException& e)
00099       {
00100          return false;
00101       }
00102    }
00103    //===========================================================================
00104    std::list<EDoc::ManagedFile> BFDArchive::ReadExtractAll(
00105       const EDoc::ManagedFile& filename)
00106    {
00107       std::list<EDoc::ManagedFile> files;
00108       struct bfd* abfd = OpenBFDFile(filename.GetWorkFileName());
00109       EDOC_Debug("Opened BFD file: " << filename.GetFileNameRep());
00110       try
00111       {
00112          ProcessReadBFD(files, abfd, filename);
00113          bfd_close(abfd);
00114       }
00115       catch(...)
00116       {
00117          bfd_close(abfd);
00118          throw;
00119       }
00120       
00121       return files;
00122    }
00123    //===========================================================================
00124    EDoc::ManagedFile BFDArchive::WriteOpen(const EDoc::ManagedFile& arch, 
00125       std::string item_filename)
00126    {
00127       std::string outfile = EDoc::CreateTempFilename();
00128       int fd = open(outfile.c_str(), O_WRONLY, O_TRUNC);
00129       if (fd == -1)
00130       {
00131          EDOC_THROW_EXCEPTION(BFDException, "", "");
00132       }
00133       close(fd);
00134    
00135       std::list<std::string> id = arch.GetIdentifier();
00136       id.push_back(item_filename);
00137       
00138       return EDoc::ManagedFile(id, outfile, new ExtractedBFDFile(
00139          arch.GetWorkFileName(), outfile, item_filename));
00140    }
00141    //===========================================================================
00142 }
00143 

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1