~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvilib/kvi_fileutils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//   File : kvi_fileutils.cpp (/usr/build/NEW_kvirc/kvirc/kvicore/kvi_fileutils.cpp)
3
 
//   Last major modification : Fri Dec 25 1998 18:26:48 by Szymon Stefanek
4
 
//
5
 
//   This file is part of the KVirc irc client distribution
 
1
// =============================================================================
 
2
//
 
3
//      --- kvi_fileutils.cpp ---
 
4
//
 
5
//   This file is part of the KVIrc IRC client distribution
6
6
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
7
7
//
8
8
//   This program is FREE software. You can redistribute it and/or
17
17
//
18
18
//   You should have received a copy of the GNU General Public License
19
19
//   along with this program. If not, write to the Free Software Foundation,
20
 
//   Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
//   Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
21
//
22
 
 
23
 
//#define _KVI_DEBUG_CHECK_RANGE_
 
22
// =============================================================================
 
23
 
 
24
#define _KVI_DEBUG_CHECK_RANGE_
 
25
#define _KVI_DEBUG_CLASS_NAME_ "KviFileUtils"
 
26
 
 
27
#define _KVI_FILEUTILS_CPP_
 
28
 
24
29
#include "kvi_debug.h"
25
 
 
26
 
#include "kvi_settings.h"
27
 
 
28
 
#define _KVI_FILEUTLIS_CPP_
 
30
#include "kvi_filedialog.h"
29
31
#include "kvi_fileutils.h"
30
 
 
31
 
#include "kvi_locale.h"
32
 
 
33
 
#include <qdir.h>
34
 
#include <qfile.h>
35
 
#include <qfileinfo.h>
36
 
 
37
 
//================ kvi_askFor*Name ============//
38
 
 
39
 
#include <qfiledialog.h>
 
32
#include "kvi_string.h"
 
33
 
 
34
// ================ kvi_askFor*Name ============
40
35
 
41
36
QString kvi_askForDirectoryName(const char *basePath)
42
37
{
43
 
        return QFileDialog::getExistingDirectory(basePath);
44
 
}
45
 
 
46
 
QString kvi_askForOpenFileName(const char *basePath,const char *filter)
47
 
{
48
 
        return QFileDialog::getOpenFileName(basePath,filter);
49
 
}
50
 
 
51
 
QStringList kvi_askForOpenFileNames(const char *basePath,const char *filter)
52
 
{
53
 
        return QFileDialog::getOpenFileNames(filter,basePath);
 
38
        return KviFileDialog::getExistingDirectory(basePath);
 
39
}
 
40
 
 
41
QString kvi_askForOpenFileName(const char *basePath, const char *filter)
 
42
{
 
43
        return KviFileDialog::getOpenFileName(basePath, filter);
 
44
}
 
45
 
 
46
QStringList kvi_askForOpenFileNames(const char *basePath, const char *filter)
 
47
{
 
48
        return KviFileDialog::getOpenFileNames(filter, basePath);
54
49
}
55
50
 
56
51
QString kvi_askForSaveFileName(const char *baseFileName)
57
52
{
58
 
        return QFileDialog::getSaveFileName(baseFileName);
 
53
        return KviFileDialog::getSaveFileName(baseFileName);
59
54
}
60
55
 
61
 
//================ kvi_isAbsolutePath ===============//
62
 
 
 
56
// ================ kvi_isAbsolutePath ===============
63
57
bool kvi_isAbsolutePath(const char *path)
64
58
{
65
59
        __range_valid(path);
66
 
        return ((*path)=='/');
 
60
        return ((*path) == '/');
67
61
}
68
62
 
69
 
//============ kvi_directoryExists ================//
70
 
 
 
63
// ============ kvi_directoryExists ================
71
64
bool kvi_directoryExists(const char *path)
72
65
{
73
66
        __range_valid(path);
75
68
        return (f.exists() && f.isDir());
76
69
}
77
70
 
78
 
//============ kvi_fileExists ================//
79
 
 
 
71
// ============ kvi_fileExists ================
80
72
bool kvi_fileExists(const char *path)
81
73
{
82
74
        __range_valid(path);
84
76
        return (f.exists() && f.isFile());
85
77
}
86
78
 
87
 
//============= kvi_fileIsReadable =============//
88
 
 
 
79
// ============= kvi_fileIsReadable =============
89
80
bool kvi_fileIsReadable(const char *path)
90
81
{
91
82
        __range_valid(path);
93
84
        return (f.exists() && f.isFile() && f.isReadable());
94
85
}
95
86
 
96
 
//================ kvi_makeDir ================//
 
87
// ============= kvi_directoryIsWritable =============
 
88
bool kvi_directoryIsWritable(const char *path)
 
89
{
 
90
        __range_valid(path);
 
91
        QFileInfo f(path);
 
92
        return (f.exists() && f.isDir() && f.isWritable());
 
93
}
97
94
 
 
95
// ================ kvi_makeDir ================
98
96
bool kvi_makeDir(const char *path)
99
97
{
100
98
        __range_valid(path);
102
100
        KviStr dir(path);
103
101
        KviStr createdDir("");
104
102
        dir.stripLeft('/');
105
 
        while(dir.hasData()){
106
 
                createdDir+="/";
107
 
                createdDir+=dir.getToken('/');
108
 
                if(!kvi_directoryExists(createdDir.m_ptr)){
109
 
                        if(!d.mkdir(createdDir.m_ptr)){
110
 
                                debug("Can't create directory %s",createdDir.m_ptr);
 
103
        while( dir.hasData() ) {
 
104
                createdDir.ensureLastCharIs('/');
 
105
                createdDir += dir.getToken('/');
 
106
                if( !kvi_directoryExists(createdDir.ptr()) ) {
 
107
                        if( !d.mkdir(createdDir.ptr()) ) {
 
108
                                debug("Cannot create directory %s", createdDir.ptr());
111
109
                                return false;
112
110
                        }
113
111
                }
115
113
        return true;
116
114
}
117
115
 
118
 
//=================== kvi_readLine =====================//
119
 
 
120
 
bool kvi_readLine(QFile *f,KviStr &str)
 
116
// =================== kvi_readLine =====================
 
117
bool kvi_readLine(QFile *f, KviStr &str)
121
118
{
122
119
        char tmp_buf[256];
123
120
        int cur_len = 0;
124
 
//      char *cur_ptr = tmp_buf;
125
121
        str = "";
126
122
        int ch = f->getch();
127
 
        
128
 
        while((ch != -1)&&(ch != '\n')&&(ch != 0)){
 
123
 
 
124
        while( (ch != -1) && (ch != '\n') && (ch != 0) ) {
129
125
                tmp_buf[cur_len] = ch;
130
126
                cur_len++;
131
 
                if(cur_len > 255){
132
 
                        if(tmp_buf[255] == '\r')cur_len--; //Ignore CR...
133
 
                        str.append(tmp_buf,cur_len);
 
127
                if( cur_len > 255 ) {
 
128
                        if( tmp_buf[255] == '\r' ) cur_len--; // Ignore CR...
 
129
                        str.append(tmp_buf, cur_len);
134
130
                        cur_len = 0;
135
131
                }
136
132
                ch = f->getch();
137
133
        }
138
 
        if(ch == 0){
 
134
        if( ch == 0 ) {
139
135
                KviStr fName = f->name();
140
 
                debug("Warning : %s is not an ascii file",fName.ptr());
141
 
        }
142
 
        if(cur_len > 0){
143
 
                if(tmp_buf[cur_len-1] == '\r')cur_len--; //Ignore last CR
144
 
                str.append(tmp_buf,cur_len);
145
 
        }
146
 
//      debug("ch is now %c , cur_len is now %d , string is %s",ch,cur_len,str.ptr());
147
 
        return (ch == '\n'); //more data to read else a broken file or EOF
 
136
                debug("Warning : %s is not an ASCII file", fName.ptr());
 
137
        }
 
138
        if( cur_len > 0 ) {
 
139
                if( tmp_buf[cur_len-1] == '\r' ) cur_len--; // Ignore last CR
 
140
                str.append(tmp_buf, cur_len);
 
141
        }
 
142
        return (ch == '\n'); // More data to read else a broken file or EOF
148
143
}
149
144
 
150
 
//==================== kvi_readStrippedLine =====================//
151
 
 
152
 
bool kvi_readStrippedLine(QFile *f,KviStr &str)
 
145
// ==================== kvi_readStrippedLine =====================
 
146
bool kvi_readStrippedLine(QFile *f, KviStr &str)
153
147
{
154
 
        bool bRet = kvi_readLine(f,str);
 
148
        bool bRet = kvi_readLine(f, str);
155
149
        str.stripWhiteSpace();
156
150
        return bRet;
157
151
}
158
152
 
159
 
//==================== kvi_readFirstNonEmptyStrippedLine =========================//
160
 
 
161
 
bool kvi_readFirstNonEmptyStrippedLine(QFile *f,KviStr &str)
 
153
// ==================== kvi_readFirstNonEmptyStrippedLine =========================
 
154
bool kvi_readFirstNonEmptyStrippedLine(QFile *f, KviStr &str)
162
155
{
163
156
        bool bContinue;
164
 
        do{
165
 
                bContinue = kvi_readStrippedLine(f,str);
166
 
        } while(bContinue && str.isEmpty());
 
157
        do {
 
158
                bContinue = kvi_readStrippedLine(f, str);
 
159
        } while( bContinue && str.isEmpty() );
167
160
        return bContinue;
168
161
}
169
162
 
170
 
//=================== kvi_writeLine =======================//
171
 
 
172
 
bool kvi_writeLine(QFile *f,const char *line)
 
163
// =================== kvi_writeLine =======================
 
164
bool kvi_writeLine(QFile *f, const char *line)
173
165
{
174
 
        bool bRet = (f->writeBlock(line,strlen(line)) != -1);
 
166
        bool bRet = (f->writeBlock(line, strlen(line)) != -1);
175
167
        return bRet ? (f->putch('\n') != -1) : false;
176
168
}
177
169
 
178
 
//=================== kvi_loadFile =======================//
179
 
 
180
 
bool kvi_loadFile(const char *path,KviStr &buffer)
 
170
// =================== kvi_loadFile =======================
 
171
bool kvi_loadFile(const char *path, KviStr &buffer)
181
172
{
182
173
        QFile f(path);
183
 
        if(!f.open(IO_ReadOnly))return false;
 
174
        if( !f.open(IO_ReadOnly) ) return false;
184
175
        buffer = "";
185
176
        KviStr tmp;
186
 
        while(kvi_readLine(&f,tmp)){
 
177
        while( kvi_readLine(&f, tmp) ) {
187
178
                buffer.append(tmp);
188
 
                buffer.append('\n'); // kvi_readLine returned true...last char was a newline
 
179
                buffer.append('\n'); // kvi_readLine returned true... last char was a newline
189
180
        }
190
 
        // kvi_readLine returned false , no ending newline encountered
 
181
        // kvi_readLine returned false, no ending newline encountered,
191
182
        // but tmp may still contain some data
192
 
        if(tmp.hasData())buffer.append(tmp);
 
183
        if( tmp.hasData() ) buffer.append(tmp);
193
184
        return true;
194
185
}
195
186
 
196
 
bool kvi_writeFile(const char *path,KviStr &buffer)
 
187
bool kvi_writeFile(const char *path, KviStr &buffer)
197
188
{
198
189
        QFile f(path);
199
 
        if(!f.open(IO_WriteOnly|IO_Truncate))return false;
200
 
        if(f.writeBlock(buffer.ptr(),buffer.len()) != buffer.len())return false;
201
 
        f.close();
202
 
        return true;
 
190
        if( !f.open(IO_WriteOnly | IO_Truncate) ) return false;
 
191
        bool success = (f.writeBlock(buffer.ptr(), buffer.len()) == buffer.len());
 
192
        f.close(); // Always close the file
 
193
        return success;
203
194
}
204
195
 
205
 
bool kvi_copyFile(const char *src,const char *dst)
 
196
bool kvi_copyFile(const char *src, const char *dst)
206
197
{
207
198
        QFile f1(src);
208
 
        if(!f1.open(IO_ReadOnly))return false;
 
199
        if( !f1.open(IO_ReadOnly) ) return false;
209
200
        QFile f2(dst);
210
 
        if(!f2.open(IO_WriteOnly|IO_Truncate)){
 
201
        if( !f2.open(IO_WriteOnly | IO_Truncate) ) {
211
202
                f1.close();
212
203
                return false;
213
204
        }
214
205
        char buffer[1024];
215
 
        while(!f1.atEnd()){
216
 
                int len = f1.readBlock(buffer,1024);
217
 
                if(len == -1){
 
206
        while( !f1.atEnd() ) {
 
207
                int len = f1.readBlock(buffer, 1024);
 
208
                if( len == -1 ) {
218
209
                        f1.close();
219
210
                        f2.close();
220
 
                        return false; //"serious error"
 
211
                        return false; // Serious error
221
212
                }
222
 
                f2.writeBlock(buffer,len);
 
213
                f2.writeBlock(buffer, len);
223
214
        }
224
215
        f1.close();
225
216
        f2.close();
226
217
        return true;
227
218
}
228
219
 
229
 
bool kvi_renameFile(const char *src,const char *dst)
 
220
bool kvi_renameFile(const char *src, const char *dst)
230
221
{
231
222
        QDir d;
232
 
        return d.rename(_CHAR_2_QSTRING(src),_CHAR_2_QSTRING(dst));
 
223
        return d.rename(_CHAR_2_QSTRING(src), _CHAR_2_QSTRING(dst));
233
224
}
 
225