sprint 1-alpha
sprint/io/tar.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::tar
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_TAR_H
00023 #define _SPRINT_TAR_H
00024 
00029 #include <string>
00030 #include <map>
00031 #include <sprint/io/file.h>
00032 
00033 namespace sprint {
00034   namespace io {
00035 
00049 class tar {
00050   private:
00051         typedef unsigned long long offset_t;
00052         
00054         struct item {
00055         offset_t offs;  
00056         unsigned long size; 
00057         };
00058         
00059 
00060         typedef std::map<std::string, item> item_t;
00061         
00062   private:
00064         sprint::io::file fd;
00065         
00066         bool m_open_for_read;   
00067 
00070         unsigned long m_file_pos;   
00073         unsigned long m_file_size; 
00074 
00075         /* write mode */        
00076         std::string m_file_name;    
00077         bool m_write_back;          
00078         bool m_valid;               
00079         
00080         bool m_eof;     
00081         offset_t m_next;  
00082         item_t m_item;  
00083   
00084   private:
00085         bool open_in_read(const char *filename);
00086         bool open_in_write(const char *filename, bool append);
00087 
00088         bool read_next(std::string & filename, item & i);
00090         bool seek(const std::string & file, item & i);
00091 
00092 public:
00095         tar(const char *archive, flag_t flag, bool append = false);
00096         ~tar();
00097          
00107         unsigned long write_header(const std::string & filename, unsigned long file_size, bool write_back_mode = true);
00108 
00115         unsigned long write_body(const char *buffer, unsigned long size);
00116 
00120         template<class T>
00121         inline unsigned long write_tbody(const T * buffer, unsigned num)
00122         {
00123         return write_body(reinterpret_cast<const char *>(buffer), num * sizeof(T));
00124         }
00128         template<class T>
00129         inline unsigned long write_tbody(const T & buffer)
00130         {
00131         return write_body(reinterpret_cast<const char *>(&buffer), sizeof(T));
00132         }
00133         
00141         unsigned long write_end(bool sync=true);
00142 
00144         unsigned  long put(const char *file, const char *string);
00145 
00147         unsigned long put(const char *file, const char *buffer, unsigned long size, bool fdatasync=false);
00152 
00153 
00154         bool seek(const std::string & file);
00156         unsigned long filesize() const { return m_file_size; }
00159         unsigned long fileread(unsigned char *buffer, unsigned long size);
00160 
00161   // TODO: seek in reading 
00163 };
00164 
00165   }
00166 }
00167 
00168 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines