ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / include / IcePatch2 / Util.h
blobe883b8098f229c5b9bd0910200d490e4d9bc8796
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #ifndef ICE_PATCH2_UTIL_H
11 #define ICE_PATCH2_UTIL_H
13 #include <Ice/Ice.h>
14 #include <IcePatch2/FileInfo.h>
15 #include <stdio.h>
17 namespace IcePatch2
20 ICE_PATCH2_API extern const char* checksumFile;
21 ICE_PATCH2_API extern const char* logFile;
23 ICE_PATCH2_API std::string lastError();
25 ICE_PATCH2_API std::string bytesToString(const Ice::ByteSeq&);
26 ICE_PATCH2_API Ice::ByteSeq stringToBytes(const std::string&);
28 ICE_PATCH2_API std::string simplify(const std::string&);
30 ICE_PATCH2_API bool isRoot(const std::string&);
32 ICE_PATCH2_API std::string getSuffix(const std::string&);
33 ICE_PATCH2_API std::string getWithoutSuffix(const std::string&);
34 ICE_PATCH2_API bool ignoreSuffix(const std::string&);
36 ICE_PATCH2_API std::string getBasename(const std::string&);
37 ICE_PATCH2_API std::string getDirname(const std::string&);
39 ICE_PATCH2_API void rename(const std::string&, const std::string&);
41 ICE_PATCH2_API void remove(const std::string&);
42 ICE_PATCH2_API void removeRecursive(const std::string&);
44 ICE_PATCH2_API Ice::StringSeq readDirectory(const std::string&);
46 ICE_PATCH2_API void createDirectory(const std::string&);
47 ICE_PATCH2_API void createDirectoryRecursive(const std::string&);
49 ICE_PATCH2_API void compressBytesToFile(const std::string&, const Ice::ByteSeq&, Ice::Int);
50 ICE_PATCH2_API void decompressFile(const std::string&);
51 ICE_PATCH2_API void setFileFlags(const std::string&, const FileInfo&);
53 struct FileInfoEqual: public std::binary_function<const FileInfo&, const FileInfo&, bool>
55 bool
56 operator()(const FileInfo& lhs, const FileInfo& rhs)
58 if(lhs.path != rhs.path)
60 return false;
64 // For the size portion of the comparison, we only distinquish
65 // between file (size >= 0) and directory (size == -1). We do
66 // not take the actual size into account, as it might be set
67 // to 0 if no compressed file is available.
69 Ice::Int lsz = lhs.size > 0 ? 0 : lhs.size;
70 Ice::Int rsz = rhs.size > 0 ? 0 : rhs.size;
71 if(lsz != rsz)
73 return false;
76 if(lhs.executable != rhs.executable)
78 return false;
81 return lhs.checksum == rhs.checksum;
85 struct FileInfoWithoutFlagsLess: public std::binary_function<const FileInfo&, const FileInfo&, bool>
87 bool
88 operator()(const FileInfo& lhs, const FileInfo& rhs)
90 return compareWithoutFlags(lhs, rhs) < 0;
93 int
94 compareWithoutFlags(const FileInfo& lhs, const FileInfo& rhs)
96 if(lhs.path < rhs.path)
98 return -1;
100 else if(rhs.path < lhs.path)
102 return 1;
106 // For the size portion of the comparison, we only distinquish
107 // between file (size >= 0) and directory (size == -1). We do
108 // not take the actual size into account, as it might be set
109 // to 0 if no compressed file is available.
111 Ice::Int lsz = lhs.size > 0 ? 0 : lhs.size;
112 Ice::Int rsz = rhs.size > 0 ? 0 : rhs.size;
113 if(lsz < rsz)
115 return -1;
117 else if(rsz < lsz)
119 return 1;
122 if(lhs.checksum < rhs.checksum)
124 return -1;
126 else if(rhs.checksum < lhs.checksum)
128 return 1;
131 return 0;
135 struct FileInfoLess : public FileInfoWithoutFlagsLess
137 bool
138 operator()(const FileInfo& lhs, const FileInfo& rhs)
140 int rc = compareWithoutFlags(lhs, rhs);
141 if(rc < 0)
143 return true;
145 else if(rc > 0)
147 return false;
150 return lhs.executable < rhs.executable;
154 class ICE_PATCH2_API GetFileInfoSeqCB
156 public:
158 virtual ~GetFileInfoSeqCB() { }
160 virtual bool remove(const std::string&) = 0;
161 virtual bool checksum(const std::string&) = 0;
162 virtual bool compress(const std::string&) = 0;
165 ICE_PATCH2_API bool getFileInfoSeq(const std::string&, int, GetFileInfoSeqCB*, FileInfoSeq&);
166 ICE_PATCH2_API bool getFileInfoSeqSubDir(const std::string&, const std::string&, int, GetFileInfoSeqCB*, FileInfoSeq&);
168 ICE_PATCH2_API void saveFileInfoSeq(const std::string&, const FileInfoSeq&);
169 ICE_PATCH2_API void loadFileInfoSeq(const std::string&, FileInfoSeq&);
171 ICE_PATCH2_API bool readFileInfo(FILE*, FileInfo&);
172 ICE_PATCH2_API bool writeFileInfo(FILE*, const FileInfo&);
174 struct FileTree1
176 FileInfoSeq files;
177 Ice::ByteSeq checksum;
180 typedef std::vector<FileTree1> FileTree1Seq;
182 struct FileTree0
184 FileTree1Seq nodes;
185 Ice::ByteSeq checksum;
188 ICE_PATCH2_API void getFileTree0(const FileInfoSeq&, FileTree0&);
192 #endif