Zipios++
basicentry.cpp
Go to the documentation of this file.
1 
2 #include "zipios++/zipios-config.h"
3 
4 #include <cassert>
5 
6 #include "zipios++/meta-iostreams.h"
7 #include <string>
8 
9 #include "zipios_common.h"
10 #include "zipios++/basicentry.h"
11 #include "zipios++/zipios_defs.h"
12 
13 #include "outputstringstream.h"
14 
15 namespace zipios {
16 
17 using std::ifstream ;
18 using std::ios ;
19 
20 //
21 // Public definitions
22 //
23 
24 BasicEntry::BasicEntry( const string &filename, const string &comment,
25  const FilePath &basepath )
26  : _filename ( filename ),
27  _comment ( comment ),
28  _basepath ( basepath )
29 {
30  string full_path = _basepath + _filename ;
31  ifstream is( full_path.c_str(), ios::in | ios::binary ) ;
32  if ( ! is ) {
33  _valid = false ;
34  } else {
35  is.seekg( 0, ios::end ) ;
36  _size = is.tellg() ;
37  is.close() ;
38  _valid = true ;
39  }
40 }
41 
42 string BasicEntry::getComment() const {
43  return _comment ;
44 }
45 
47  return getSize() ;
48 }
49 
50 uint32 BasicEntry::getCrc() const {
51  return 0 ;
52 }
53 
54 vector< unsigned char > BasicEntry::getExtra() const {
55  return vector< unsigned char > () ;
56 }
57 
59  return STORED ;
60 }
61 
62 string BasicEntry::getName() const {
63  return _filename ;
64 }
65 
66 string BasicEntry::getFileName() const {
67  if ( isDirectory() )
68  return string() ;
69  string::size_type pos ;
70  pos = _filename.find_last_of( separator ) ;
71  if ( pos != string::npos ) { // separator found!
72  // isDirectory() check means pos should not be last, so pos+1 is ok
73  return _filename.substr(pos + 1) ;
74  } else {
75  return _filename ;
76  }
77 }
78 
79 uint32 BasicEntry::getSize() const {
80  return _size ;
81 }
82 
83 int BasicEntry::getTime() const {
84  return 0 ; // FIXME later
85 }
86 
87 bool BasicEntry::isValid() const {
88  return _valid ;
89 }
90 
91 // virtual int hashCode() const {}
93  assert( _filename.size() != 0 ) ;
94  return _filename[ _filename.size() - 1 ] == separator ;
95 }
96 
97 
98 void BasicEntry::setComment( const string &comment ) {
99  _comment = comment ;
100 }
101 
103 }
104 
105 void BasicEntry::setCrc( uint32 ) {
106 }
107 
108 void BasicEntry::setExtra( const vector< unsigned char > & ) {
109 }
110 
112 }
113 
114 void BasicEntry::setName( const string &name ) {
115  _filename = name ;
116 }
117 
118 void BasicEntry::setSize( uint32 size ) {
119  _size = size ;
120 }
121 
122 void BasicEntry::setTime( int ) {
123 }
124 
125 
126 string BasicEntry::toString() const {
127  OutputStringStream sout ;
128  sout << _filename << " (" << _size << " bytes)" ;
129  return sout.str() ;
130 }
131 
133  return new BasicEntry( *this ) ;
134 }
135 
136 BasicEntry::~BasicEntry() {
137 }
138 
139 
140 } // namespace
141 
146 /*
147  Zipios++ - a small C++ library that provides easy access to .zip files.
148  Copyright (C) 2000 Thomas Søndergaard
149 
150  This library is free software; you can redistribute it and/or
151  modify it under the terms of the GNU Lesser General Public
152  License as published by the Free Software Foundation; either
153  version 2 of the License, or (at your option) any later version.
154 
155  This library is distributed in the hope that it will be useful,
156  but WITHOUT ANY WARRANTY; without even the implied warranty of
157  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
158  Lesser General Public License for more details.
159 
160  You should have received a copy of the GNU Lesser General Public
161  License along with this library; if not, write to the Free Software
162  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
163 */
virtual StorageMethod getMethod() const
Returns the method used to store the entry in the FileCollection.
Definition: basicentry.cpp:58
virtual string getFileName() const
Returns the filename of the entry.
Definition: basicentry.cpp:66
virtual string toString() const
Returns a human-readable string representation of the entry.
Definition: basicentry.cpp:126
virtual uint32 getCompressedSize() const
Returns the compressed size of the entry.
Definition: basicentry.cpp:46
virtual void setSize(uint32 size)
Sets the size field for the entry.
Definition: basicentry.cpp:118
virtual FileEntry * clone() const
Create a heap allocated clone of the object this method is called for.
Definition: basicentry.cpp:132
virtual void setTime(int time)
Sets the time field for the entry.
Definition: basicentry.cpp:122
virtual vector< unsigned char > getExtra() const
Returns a vector of bytes of extra data that may be stored with the entry.
Definition: basicentry.cpp:54
Header file that defines some simple data types.
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
Definition: basicentry.cpp:111
virtual void setCompressedSize(uint32 size)
Set the compressed size field of the entry.
Definition: basicentry.cpp:102
virtual bool isValid() const
Any method or operator that initializes a FileEntry may set a flag, that specifies whether the read e...
Definition: basicentry.cpp:87
OutputStringStream is typedefed to ostringstream if sstream is part of the standard library (unless Z...
Header file that defines OutputStringStream.
virtual int getTime() const
Returns the date and time of FIXME: what?
Definition: basicentry.cpp:83
virtual bool isDirectory() const
Returns true if the entry is a directory.
Definition: basicentry.cpp:92
Header file that defines BasicEntry.
virtual void setCrc(uint32 crc)
Sets the crc field.
Definition: basicentry.cpp:105
virtual string getName() const
Returns the full filename of the entry, including a path if the entry is stored in a subfolder...
Definition: basicentry.cpp:62
virtual string getComment() const
Returns the comment of the entry, if it has one.
Definition: basicentry.cpp:42
virtual void setComment(const string &comment)
Sets the comment field for the FileEntry.
Definition: basicentry.cpp:98
string str()
Specialization of ostrstream::str() that takes care of null-terminating the string and unfreezing the...
BasicEntry(const string &filename, const string &comment, const FilePath &basepath=FilePath())
Constructor.
Definition: basicentry.cpp:24
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.h:52
virtual void setName(const string &name)
Sets the name field for the entry.
Definition: basicentry.cpp:114
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition: fileentry.h:25
virtual uint32 getCrc() const
Returns the Crc for the entry, if it has one.
Definition: basicentry.cpp:50
virtual void setExtra(const vector< unsigned char > &extra)
Sets the extra field.
Definition: basicentry.cpp:108
FilePath represents a path to a file or directory name.
Definition: filepath.h:18
virtual uint32 getSize() const
Returns the (uncompressed) size of the entry data.
Definition: basicentry.cpp:79
Header file containing miscellaneous small functions.