-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsmsutils.cpp
84 lines (77 loc) · 2.52 KB
/
qgsmsutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/***************************************************************************
qgsmsutils.cpp
---------------------
begin : August 2010
copyright : (C) 2010 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmsutils.h"
#include "qgslogger.h"
#include <stdlib.h>
#include <time.h>
#include <QDir>
#include <QFileInfo>
#include <QTextStream>
QString QgsMSUtils::createTempFilePath()
{
//save the content of the file into a temporary location
//generate a name considering the current time
//struct timeval currentTime;
//gettimeofday(¤tTime, NULL);
time_t seconds;
time( &seconds );
srand( seconds );
int randomNumber = rand();
QString tempFileName = QString::number( randomNumber );
QString tempFilePath;
//on windows, store the temporary file in current_path/tmp directory,
//on unix, store it in /tmp/qgis_wms_serv
#ifndef Q_OS_WIN
QDir tempFileDir( "/tmp/qgis_map_serv" );
if ( !tempFileDir.exists() ) //make sure the directory exists
{
QDir tmpDir( "/tmp" );
tmpDir.mkdir( "qgis_map_serv" );
}
tempFilePath = "/tmp/qgis_map_serv/" + tempFileName;
#else
QDir tempFileDir( QDir::currentPath() + "/tmp" );
if ( !tempFileDir.exists() )
{
QDir currentDir( QDir::currentPath() );
currentDir.mkdir( "tmp" );
}
tempFilePath = QDir::currentPath() + "/tmp" + "/" + tempFileName;
#endif // Q_OS_WIN
QFileInfo testFile( tempFilePath );
while ( testFile.exists() )
{
//change the name
tempFilePath += "1";
testFile.setFile( tempFilePath );
}
QgsDebugMsg( tempFilePath );
return tempFilePath;
}
int QgsMSUtils::createTextFile( QString filePath, const QString& text )
{
QFile file( filePath );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QTextStream fileStream( &file );
fileStream << text;
file.close();
return 0;
}
else
{
return 1;
}
}