OpenCores
URL https://opencores.org/ocsvn/bw_tiff_compression/bw_tiff_compression/trunk

Subversion Repositories bw_tiff_compression

[/] [bw_tiff_compression/] [trunk/] [client_application/] [src/] [Libs/] [CPathLib.cpp] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 amulder
/*
2
 * @file     PathLib.cpp
3
 * @date     May 14, 2012
4
 * @author   Aart Mulder
5
 */
6
 
7
/* System includes */
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <fstream>
12
#include <iostream>
13
#include <dirent.h>
14
#include <sys/stat.h>
15
#include <sys/types.h>
16
 
17
#ifdef linux
18
#include <unistd.h>
19
#else
20
//#include <direct.h>
21
#include <stdarg.h>
22
#include <windef.h>
23
#include <WinBase.h>
24
#endif
25
 
26
#ifdef QT
27
#include <QRegExp>
28
#include <QFile>
29
#include <QDir>
30
#endif
31
 
32
/* Own includes */
33
#include "CPathLib.h"
34
#include "stddefs.h"
35
 
36
CPathLib::CPathLib()
37
{
38
 
39
}
40
 
41
char* CPathLib::CompletePath(char* pcPath)
42
{
43
        char aCurrentDir[FILENAME_MAX], *pResult;
44
#ifdef linux
45
    char szTmp[32];
46
#endif
47
    int bytes;
48
 
49
        pResult = (char*)malloc(sizeof(char) * FILENAME_MAX);
50
 
51
        if( (pcPath[0] == '.') && (pcPath[1] == '/') )
52
        {
53
#ifdef linux
54
        sprintf(szTmp, "/proc/%d/exe", getpid());
55
        aCurrentDir[0] = '\0';
56
        bytes = MIN(readlink(szTmp, aCurrentDir, FILENAME_MAX), FILENAME_MAX - 1);
57
        if(bytes >= 0)
58
            aCurrentDir[bytes] = '\0';
59
#else
60
        bytes = GetModuleFileNameA(NULL, aCurrentDir, FILENAME_MAX);
61
        if(bytes == 0)
62
            return NULL;
63
#endif
64
 
65
                strcpy(pResult, aCurrentDir);
66
                strcat(pResult, pcPath+1);
67
        }
68
        else
69
        {
70
                strcpy(pResult, pcPath);
71
        }
72
 
73
        return pResult;
74
}
75
 
76
std::string CPathLib::CompletePath(std::string sPath)
77
{
78
        char aCurrentDir[FILENAME_MAX];
79
        std::string sResult;
80
#ifdef linux
81
    char szTmp[32];
82
#endif
83
    int bytes;
84
 
85
        if( (sPath.at(0) == '.') && (sPath.at(1) == '/') )
86
        {
87
#ifdef linux
88
        sprintf(szTmp, "/proc/%d/exe", getpid());
89
        aCurrentDir[0] = '\0';
90
        bytes = MIN(readlink(szTmp, aCurrentDir, FILENAME_MAX), FILENAME_MAX - 1);
91
        if(bytes >= 0)
92
            aCurrentDir[bytes] = '\0';
93
#else
94
        bytes = GetModuleFileNameA(NULL, aCurrentDir, FILENAME_MAX);
95
        if(bytes == 0)
96
            return sResult;
97
#endif
98
                sResult.assign(aCurrentDir);
99
                sResult.append(sPath.substr(1, sPath.length()-1));
100
        }
101
        else
102
        {
103
                sResult = sPath;
104
        }
105
 
106
        return sResult;
107
}
108
 
109
#ifdef QT
110
QString CPathLib::CompletePath(QString sPath)
111
{
112
        char aCurrentDir[FILENAME_MAX];
113
        QString sResult;
114
#ifdef linux
115
    char szTmp[32];
116
#endif
117
    int bytes;
118
 
119
        if( (sPath.at(0) == '.') && (sPath.at(1) == '/') )
120
        {
121
#ifdef linux
122
        sprintf(szTmp, "/proc/%d/exe", getpid());
123
        aCurrentDir[0] = '\0';
124
        bytes = MIN(readlink(szTmp, aCurrentDir, FILENAME_MAX), FILENAME_MAX - 1);
125
        if(bytes >= 0)
126
            aCurrentDir[bytes] = '\0';
127
#else
128
        bytes = GetModuleFileNameA(NULL, aCurrentDir, FILENAME_MAX);
129
        if(bytes == 0)
130
            return sResult;
131
#endif
132
        sResult.fromAscii(aCurrentDir);
133
                sResult.append(sPath.right(sPath.length()-1));
134
        }
135
        else
136
        {
137
                sResult = sPath;
138
        }
139
 
140
        return sResult;
141
}
142
#endif
143
 
144
#ifdef QT
145
void CPathLib::DeleteFiles(QList<QString> *pFiles, QList<bool> *pResults)
146
#else
147
void CPathLib::DeleteFiles(std::list<std::string> *pFiles, std::list<bool> *pResults)
148
#endif
149
{
150
#ifdef QT
151
        QList<QString>::iterator itFiles;
152
        QString sFilename;
153
#else
154
        std::list<std::string>::iterator itFiles;
155
        std::string sFilename;
156
#endif
157
 
158
        if(pFiles == NULL)
159
                return;
160
 
161
        if(pResults == NULL)
162
                return;
163
 
164
        pResults->clear();
165
 
166
        for(itFiles = pFiles->begin(); itFiles != pFiles->end(); itFiles++)
167
        {
168
                sFilename = CompletePath(*itFiles);
169
 
170
#ifdef QT
171
                if(remove(sFilename.toStdString().c_str()) != 0)
172
#else
173
                if(remove(sFilename.c_str()) != 0)
174
#endif
175
                {
176
                        pResults->push_back(false);
177
                }
178
                else
179
                {
180
                        pResults->push_back(true);
181
                }
182
        }
183
}
184
 
185
#ifdef QT
186
QList<QString> CPathLib::ReadDir(QString sDir)
187
{
188
        QList<QString> aFiles;
189
 
190
        ReadDir(sDir, &aFiles);
191
 
192
        return aFiles;
193
}
194
#endif
195
 
196
#ifdef QT
197
bool CPathLib::ReadDir(QString sDir, QList<QString> *paFiles, QString sFilter)
198
{
199
        return ReadDir(sDir, paFiles, NULL, &sFilter);
200
}
201
#endif
202
 
203
#ifdef QT
204
bool CPathLib::ReadDir(QString sDir, QList<QString> *paFiles, QList<quint32> *paFileSizes, QString *sFilter)
205
{
206
        QString filepath;
207
        DIR *dp;
208
        struct dirent *dirp;
209
        struct stat filestat;
210
 
211
        if(paFiles == NULL)
212
                return false;
213
 
214
        paFiles->clear();
215
 
216
        if(paFileSizes != NULL)
217
                paFileSizes->clear();
218
 
219
    sDir = CPathLib::CompletePath(sDir);
220
 
221
        dp = opendir(sDir.toStdString().c_str());
222
        if(dp == NULL)
223
                return false;
224
 
225
        while( (dirp = readdir(dp)) )
226
        {
227
                filepath = sDir + QString("/") + QString("%1").arg(dirp->d_name);
228
 
229
                if(stat(filepath.toStdString().c_str(), &filestat)) continue;
230
                if( S_ISDIR(filestat.st_mode)) continue;
231
 
232
                /*
233
                 * Do the filter operation.
234
                 */
235
                if(sFilter != NULL)
236
                {
237
                        if(sFilter->length() > 0)
238
                        {
239
                                QString sFileName(dirp->d_name);
240
                                QRegExp oRegExp(sFilter->toStdString().c_str());
241
                                if( oRegExp.indexIn(sFileName) == 0)
242
                                {
243
                                        paFiles->push_back(QString(dirp->d_name));
244
                                }
245
                        }
246
                        else
247
                        {
248
                                paFiles->push_back(QString(dirp->d_name));
249
                        }
250
                }
251
                else
252
                {
253
                        paFiles->push_back(QString(dirp->d_name));
254
                }
255
 
256
                if(paFileSizes != NULL)
257
                        paFileSizes->push_back(filestat.st_size);
258
        }
259
 
260
        return true;
261
}
262
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.