22
22
#include < QFile>
23
23
#include < QDataStream>
24
24
#include < QTextStream>
25
+ #include < QTextCodec>
25
26
#include < QStringList>
26
27
#include < QRegExp>
27
28
#include < QUrl>
28
29
29
30
QgsDelimitedTextFile::QgsDelimitedTextFile ( QString url ) :
30
31
mFileName(QString()),
32
+ mEncoding(" UTF-8" ),
31
33
mFile(0 ),
32
34
mStream(0 ),
33
35
mDefinitionValid(false ),
@@ -74,6 +76,11 @@ bool QgsDelimitedTextFile::open()
74
76
return false ;
75
77
}
76
78
mStream = new QTextStream ( mFile );
79
+ if ( mEncoding .isEmpty () && mEncoding != " System" )
80
+ {
81
+ QTextCodec *codec = QTextCodec::codecForName (mEncoding .toAscii ());
82
+ mStream ->setCodec (codec);
83
+ }
77
84
}
78
85
return true ;
79
86
}
@@ -101,6 +108,12 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
101
108
// Extract the file name
102
109
setFileName ( url.toLocalFile ());
103
110
111
+ // Extract the encoding
112
+ if ( url.hasQueryItem (" encoding" ))
113
+ {
114
+ mEncoding =url.queryItemValue (" encoding" );
115
+ }
116
+
104
117
// The default type is csv, to be consistent with the
105
118
// previous implementation (except that quoting should be handled properly)
106
119
@@ -150,19 +163,16 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
150
163
}
151
164
152
165
QgsDebugMsg ( " Delimited text file is: " + mFileName );
166
+ QgsDebugMsg ( " Encoding is: " + mEncoding );
153
167
QgsDebugMsg ( " Delimited file type is: " + type );
154
- QgsDebugMsg ( " Delimiter is: " + delimiter );
155
- QgsDebugMsg ( " Quote character is: " + quote);
156
- QgsDebugMsg ( " Escape character is: " + escape);
168
+ QgsDebugMsg ( " Delimiter is: [ " + delimiter + " ] " );
169
+ QgsDebugMsg ( " Quote character is: [ " + quote + " ] " );
170
+ QgsDebugMsg ( " Escape character is: [ " + escape + " ] " );
157
171
QgsDebugMsg ( " Skip lines: " + QString::number (mSkipLines ) );
158
172
QgsDebugMsg ( " Skip lines: " + QString (mUseHeader ? " Yes" : " No" ) );
159
173
160
- if ( type == " csv" )
161
- {
162
- setTypeCSV (delimiter,quote,escape);
163
- }
164
174
// Support for previous version of plain characters
165
- else if ( type == " plain" )
175
+ if ( type == " csv " || type == " plain" )
166
176
{
167
177
setTypeCSV (delimiter,quote,escape);
168
178
}
@@ -184,6 +194,10 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
184
194
QUrl QgsDelimitedTextFile::url ()
185
195
{
186
196
QUrl url = QUrl::fromLocalFile ( mFileName );
197
+ if ( mEncoding != " UTF-8" )
198
+ {
199
+ url.addQueryItem (" encoding" ,mEncoding );
200
+ }
187
201
url.addQueryItem (" type" ,type ());
188
202
if ( mType == DelimTypeRegexp )
189
203
{
@@ -212,6 +226,12 @@ void QgsDelimitedTextFile::setFileName( QString filename )
212
226
mFileName = filename;
213
227
}
214
228
229
+ void QgsDelimitedTextFile::setEncoding ( QString encoding )
230
+ {
231
+ resetDefinition ();
232
+ mEncoding = encoding;
233
+ }
234
+
215
235
QString QgsDelimitedTextFile::type ()
216
236
{
217
237
if ( mType == DelimTypeWhitespace ) return QString (" whitespace" );
0 commit comments