~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to Util.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Moog
  • Date: 2011-05-06 19:01:38 UTC
  • Revision ID: james.westby@ubuntu.com-20110506190138-6l4nro6jmhr3837i
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of nzbget
 
3
 *
 
4
 *  Copyright (C) 2007-2009 Andrei Prygounkov <hugbug@users.sourceforge.net>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 *
 
20
 * $Revision: 327 $
 
21
 * $Date: 2009-05-31 00:18:31 +0200 (Sun, 31 May 2009) $
 
22
 *
 
23
 */
 
24
 
 
25
 
 
26
#ifndef UTIL_H
 
27
#define UTIL_H
 
28
 
 
29
#ifdef WIN32
 
30
#include <stdio.h>
 
31
#include <io.h>
 
32
#else
 
33
#include <dirent.h>
 
34
#endif
 
35
 
 
36
#ifdef WIN32
 
37
extern int optind, opterr;
 
38
extern char *optarg;
 
39
int getopt(int argc, char *argv[], char *optstring);
 
40
#endif
 
41
 
 
42
class DirBrowser
 
43
{
 
44
private:
 
45
#ifdef WIN32
 
46
        struct _finddata_t      m_FindData;
 
47
        intptr_t                        m_hFile;
 
48
        bool                            m_bFirst;
 
49
#else
 
50
        DIR*                            m_pDir;
 
51
        struct dirent*          m_pFindData;
 
52
#endif
 
53
 
 
54
public:
 
55
                                                DirBrowser(const char* szPath);
 
56
                                                ~DirBrowser();
 
57
        const char*                     Next();
 
58
};
 
59
 
 
60
class Util 
 
61
{
 
62
public:
 
63
 
 
64
        static char* BaseFileName(const char* filename);
 
65
        static void NormalizePathSeparators(char* szPath);
 
66
        static bool LoadFileIntoBuffer(const char* szFileName, char** pBuffer, int* pBufferLength);
 
67
        static bool SetFileSize(const char* szFilename, int iSize);
 
68
        static void MakeValidFilename(char* szFilename, char cReplaceChar, bool bAllowSlashes);
 
69
        static bool MoveFile(const char* szSrcFilename, const char* szDstFilename);
 
70
        static bool FileExists(const char* szFilename);
 
71
        static bool DirectoryExists(const char* szDirFilename);
 
72
        static bool CreateDirectory(const char* szDirFilename);
 
73
        static bool ForceDirectories(const char* szPath);
 
74
        static long long FileSize(const char* szFilename);
 
75
        static long long FreeDiskSize(const char* szPath);
 
76
        static bool DirEmpty(const char* szDirFilename);
 
77
        static bool RenameBak(const char* szFilename, const char* szBakPart, bool bRemoveOldExtension, char* szNewNameBuf, int iNewNameBufSize);
 
78
#ifndef WIN32
 
79
        static bool ExpandHomePath(const char* szFilename, char* szBuffer, int iBufSize);
 
80
        static void ExpandFileName(const char* szFilename, char* szBuffer, int iBufSize);
 
81
#endif
 
82
        static void FormatFileSize(char* szBuffer, int iBufLen, long long lFileSize);
 
83
 
 
84
        /*
 
85
         * Split command line int arguments.
 
86
         * Uses spaces and single quotation marks as separators.
 
87
         * Returns bool if sucessful or false if bad escaping was detected.
 
88
         * Parameter "argv" may be NULL if only a syntax check is needed.
 
89
         * Parsed parameters returned in Array "argv", which contains at least one element.
 
90
         * The last element in array is NULL.
 
91
         * Restrictions: the number of arguments is limited to 100 and each arguments must
 
92
         * be maximum 1024 chars long.
 
93
         * If these restrictions are exceeded, only first 100 arguments and only first 1024
 
94
         * for each argument are returned (the functions still returns "true").
 
95
         */
 
96
        static bool SplitCommandLine(const char* szCommandLine, char*** argv);
 
97
 
 
98
        static long long JoinInt64(unsigned long Hi, unsigned long Lo);
 
99
        static void SplitInt64(long long Int64, unsigned long* Hi, unsigned long* Lo);
 
100
 
 
101
        /**
 
102
         * Int64ToFloat converts Int64 to float.
 
103
         * Simple (float)Int64 does not work on all compilers,
 
104
         * for example on ARM for NSLU2 (unslung).
 
105
         */
 
106
        static float Int64ToFloat(long long Int64);
 
107
 
 
108
        static unsigned int DecodeBase64(char* szInputBuffer, int iInputBufferLength, char* szOutputBuffer);
 
109
 
 
110
        /*
 
111
         * Encodes string to be used as content of xml-tag.
 
112
         * Returns new string allocated with malloc, it need to be freed by caller.
 
113
         */
 
114
        static char* XmlEncode(const char* raw);
 
115
 
 
116
        /*
 
117
         * Decodes string from xml.
 
118
         * The string is decoded on the place overwriting the content of raw-data.
 
119
         */
 
120
        static void XmlDecode(char* raw);
 
121
 
 
122
        /*
 
123
         * Returns pointer to tag-content and length of content in iValueLength
 
124
         * The returned pointer points to the part of source-string, no additional strings are allocated.
 
125
         */
 
126
        static const char* XmlFindTag(const char* szXml, const char* szTag, int* pValueLength);
 
127
 
 
128
        /*
 
129
         * Parses tag-content into szValueBuf.
 
130
         */
 
131
        static bool XmlParseTagValue(const char* szXml, const char* szTag, char* szValueBuf, int iValueBufSize, const char** pTagEnd);
 
132
 
 
133
        /*
 
134
         * Creates JSON-string by replace the certain characters with escape-sequences.
 
135
         * Returns new string allocated with malloc, it need to be freed by caller.
 
136
         */
 
137
        static char* JsonEncode(const char* raw);
 
138
 
 
139
        /*
 
140
         * Decodes JSON-string.
 
141
         * The string is decoded on the place overwriting the content of raw-data.
 
142
         */
 
143
        static void JsonDecode(char* raw);
 
144
 
 
145
        /*
 
146
         * Returns pointer to field-content and length of content in iValueLength
 
147
         * The returned pointer points to the part of source-string, no additional strings are allocated.
 
148
         */
 
149
        static const char* JsonFindField(const char* szJsonText, const char* szFieldName, int* pValueLength);
 
150
 
 
151
        /*
 
152
         * Returns pointer to field-content and length of content in iValueLength
 
153
         * The returned pointer points to the part of source-string, no additional strings are allocated.
 
154
         */
 
155
        static const char* JsonNextValue(const char* szJsonText, int* pValueLength);
 
156
 
 
157
        /*
 
158
         * Returns program version and revision number as string formatted like "0.7.0-r295".
 
159
         * If revision number is not available only version is returned ("0.7.0").
 
160
         */
 
161
        static const char* VersionRevision() { return VersionRevisionBuf; };
 
162
        
 
163
        /*
 
164
         * Initialize buffer for program version and revision number.
 
165
         * This function must be called during program initialization before any
 
166
         * call to "VersionRevision()".
 
167
         */
 
168
        static void InitVersionRevision();
 
169
        
 
170
        static char VersionRevisionBuf[40];
 
171
};
 
172
 
 
173
#endif