| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: textfile.cpp |
| 3 | // Purpose: implementation of wxTextFile class |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 03.04.98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // headers |
| 14 | // ============================================================================ |
| 15 | |
| 16 | #ifdef __GNUG__ |
| 17 | #pragma implementation "textfile.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif //__BORLANDC__ |
| 25 | |
| 26 | #if !wxUSE_FILE |
| 27 | #undef wxUSE_TEXTFILE |
| 28 | #define wxUSE_TEXTFILE 0 |
| 29 | #endif // wxUSE_FILE |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/string.h" |
| 33 | #include "wx/intl.h" |
| 34 | #include "wx/file.h" |
| 35 | #include "wx/log.h" |
| 36 | #endif |
| 37 | |
| 38 | #include "wx/textfile.h" |
| 39 | |
| 40 | // ============================================================================ |
| 41 | // wxTextFile class implementation |
| 42 | // ============================================================================ |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | // static methods (always compiled in) |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | // default type is the native one |
| 49 | const wxTextFileType wxTextFile::typeDefault = |
| 50 | #if defined(__WINDOWS__) |
| 51 | wxTextFileType_Dos; |
| 52 | #elif defined(__UNIX__) |
| 53 | wxTextFileType_Unix; |
| 54 | #elif defined(__WXMAC__) |
| 55 | wxTextFileType_Mac; |
| 56 | #elif defined(__WXPM__) |
| 57 | wxTextFileType_Os2; |
| 58 | #else |
| 59 | wxTextFileType_None; |
| 60 | #error "wxTextFile: unsupported platform." |
| 61 | #endif |
| 62 | |
| 63 | const wxChar *wxTextFile::GetEOL(wxTextFileType type) |
| 64 | { |
| 65 | switch ( type ) { |
| 66 | default: |
| 67 | wxFAIL_MSG(wxT("bad file type in wxTextFile::GetEOL.")); |
| 68 | // fall through nevertheless - we must return something... |
| 69 | |
| 70 | case wxTextFileType_None: return wxT(_T("")); |
| 71 | case wxTextFileType_Unix: return wxT(_T("\n")); |
| 72 | case wxTextFileType_Dos: return wxT(_T("\r\n")); |
| 73 | case wxTextFileType_Mac: return wxT(_T("\r")); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | wxString wxTextFile::Translate(const wxString& text, wxTextFileType type) |
| 78 | { |
| 79 | // don't do anything if there is nothing to do |
| 80 | if ( type == wxTextFileType_None ) |
| 81 | return text; |
| 82 | |
| 83 | // GRG: don't do anything either if it is empty |
| 84 | if ( text.IsEmpty() ) |
| 85 | return text; |
| 86 | |
| 87 | wxString eol = GetEOL(type), result; |
| 88 | |
| 89 | // optimization: we know that the length of the new string will be about |
| 90 | // the same as the length of the old one, so prealloc memory to aviod |
| 91 | // unnecessary relocations |
| 92 | result.Alloc(text.Len()); |
| 93 | |
| 94 | wxChar chLast = 0; |
| 95 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) |
| 96 | { |
| 97 | wxChar ch = *pc; |
| 98 | switch ( ch ) { |
| 99 | case _T('\n'): |
| 100 | // Dos/Unix line termination |
| 101 | result += eol; |
| 102 | chLast = 0; |
| 103 | break; |
| 104 | |
| 105 | case _T('\r'): |
| 106 | if ( chLast == _T('\r') ) { |
| 107 | // Mac empty line |
| 108 | result += eol; |
| 109 | } |
| 110 | else { |
| 111 | // just remember it: we don't know whether it is just "\r" |
| 112 | // or "\r\n" yet |
| 113 | chLast = _T('\r'); |
| 114 | } |
| 115 | break; |
| 116 | |
| 117 | default: |
| 118 | if ( chLast == _T('\r') ) { |
| 119 | // Mac line termination |
| 120 | result += eol; |
| 121 | } |
| 122 | |
| 123 | // add to the current line |
| 124 | result += ch; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if ( chLast ) { |
| 129 | // trailing '\r' |
| 130 | result += eol; |
| 131 | } |
| 132 | |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | #if wxUSE_TEXTFILE |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // ctors & dtor |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
| 142 | wxTextFile::wxTextFile(const wxString& strFile) : m_strFile(strFile) |
| 143 | { |
| 144 | m_nCurLine = 0; |
| 145 | m_isOpened = FALSE; |
| 146 | } |
| 147 | |
| 148 | wxTextFile::~wxTextFile() |
| 149 | { |
| 150 | // m_file dtor called automatically |
| 151 | } |
| 152 | |
| 153 | // ---------------------------------------------------------------------------- |
| 154 | // file operations |
| 155 | // ---------------------------------------------------------------------------- |
| 156 | |
| 157 | bool wxTextFile::Exists() const |
| 158 | { |
| 159 | return wxFile::Exists(m_strFile); |
| 160 | } |
| 161 | |
| 162 | bool wxTextFile::Create(const wxString& strFile) |
| 163 | { |
| 164 | m_strFile = strFile; |
| 165 | |
| 166 | return Create(); |
| 167 | } |
| 168 | |
| 169 | bool wxTextFile::Create() |
| 170 | { |
| 171 | // file name must be either given in ctor or in Create(const wxString&) |
| 172 | wxASSERT( !m_strFile.IsEmpty() ); |
| 173 | |
| 174 | // if the file already exists do nothing |
| 175 | if ( Exists() ) return FALSE; |
| 176 | |
| 177 | if ( m_file.Open(m_strFile, wxFile::write) ) |
| 178 | { |
| 179 | m_file.Close(); |
| 180 | return TRUE; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | return FALSE; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool wxTextFile::Open(const wxString& strFile) |
| 189 | { |
| 190 | m_strFile = strFile; |
| 191 | |
| 192 | return Open(); |
| 193 | } |
| 194 | |
| 195 | bool wxTextFile::Open() |
| 196 | { |
| 197 | // file name must be either given in ctor or in Open(const wxString&) |
| 198 | wxASSERT( !m_strFile.IsEmpty() ); |
| 199 | |
| 200 | // open file in read-only mode |
| 201 | if ( !m_file.Open(m_strFile) ) |
| 202 | return FALSE; |
| 203 | |
| 204 | // read file into memory |
| 205 | m_isOpened = Read(); |
| 206 | |
| 207 | m_file.Close(); |
| 208 | |
| 209 | return m_isOpened; |
| 210 | } |
| 211 | |
| 212 | // analyse some lines of the file trying to guess it's type. |
| 213 | // if it fails, it assumes the native type for our platform. |
| 214 | wxTextFileType wxTextFile::GuessType() const |
| 215 | { |
| 216 | // file should be opened and we must be in it's beginning |
| 217 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); |
| 218 | |
| 219 | // scan the file lines |
| 220 | size_t nUnix = 0, // number of '\n's alone |
| 221 | nDos = 0, // number of '\r\n' |
| 222 | nMac = 0; // number of '\r's |
| 223 | |
| 224 | // we take MAX_LINES_SCAN in the beginning, middle and the end of file |
| 225 | #define MAX_LINES_SCAN (10) |
| 226 | size_t nCount = m_aLines.Count() / 3, |
| 227 | nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3; |
| 228 | |
| 229 | #define AnalyseLine(n) \ |
| 230 | switch ( m_aTypes[n] ) { \ |
| 231 | case wxTextFileType_Unix: nUnix++; break; \ |
| 232 | case wxTextFileType_Dos: nDos++; break; \ |
| 233 | case wxTextFileType_Mac: nMac++; break; \ |
| 234 | default: wxFAIL_MSG(_("unknown line terminator")); \ |
| 235 | } |
| 236 | |
| 237 | size_t n; |
| 238 | for ( n = 0; n < nScan; n++ ) // the beginning |
| 239 | AnalyseLine(n); |
| 240 | for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ ) |
| 241 | AnalyseLine(n); |
| 242 | for ( n = nCount - nScan; n < nCount; n++ ) |
| 243 | AnalyseLine(n); |
| 244 | |
| 245 | #undef AnalyseLine |
| 246 | |
| 247 | // interpret the results (FIXME far from being even 50% fool proof) |
| 248 | if ( nDos + nUnix + nMac == 0 ) { |
| 249 | // no newlines at all |
| 250 | wxLogWarning(_("'%s' is probably a binary file."), m_strFile.c_str()); |
| 251 | } |
| 252 | else { |
| 253 | #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \ |
| 254 | : n##t1 > n##t2 \ |
| 255 | ? wxTextFileType_##t1 \ |
| 256 | : wxTextFileType_##t2 |
| 257 | |
| 258 | // Watcom C++ doesn't seem to be able to handle the macro |
| 259 | #if !defined(__WATCOMC__) |
| 260 | if ( nDos > nUnix ) |
| 261 | return GREATER_OF(Dos, Mac); |
| 262 | else if ( nDos < nUnix ) |
| 263 | return GREATER_OF(Unix, Mac); |
| 264 | else { |
| 265 | // nDos == nUnix |
| 266 | return nMac > nDos ? wxTextFileType_Mac : typeDefault; |
| 267 | } |
| 268 | #endif // __WATCOMC__ |
| 269 | |
| 270 | #undef GREATER_OF |
| 271 | } |
| 272 | |
| 273 | return typeDefault; |
| 274 | } |
| 275 | |
| 276 | bool wxTextFile::Read() |
| 277 | { |
| 278 | // file should be opened and we must be in it's beginning |
| 279 | wxASSERT( m_file.IsOpened() && m_file.Tell() == 0 ); |
| 280 | |
| 281 | wxString str; |
| 282 | char ch, chLast = '\0'; |
| 283 | char buf[1024]; |
| 284 | int n, nRead; |
| 285 | while ( !m_file.Eof() ) { |
| 286 | nRead = m_file.Read(buf, WXSIZEOF(buf)); |
| 287 | if ( nRead == wxInvalidOffset ) { |
| 288 | // read error (error message already given in wxFile::Read) |
| 289 | return FALSE; |
| 290 | } |
| 291 | |
| 292 | for ( n = 0; n < nRead; n++ ) { |
| 293 | ch = buf[n]; |
| 294 | switch ( ch ) { |
| 295 | case '\n': |
| 296 | // Dos/Unix line termination |
| 297 | m_aLines.Add(str); |
| 298 | m_aTypes.Add(chLast == '\r' ? wxTextFileType_Dos |
| 299 | : wxTextFileType_Unix); |
| 300 | str.Empty(); |
| 301 | chLast = '\n'; |
| 302 | break; |
| 303 | |
| 304 | case '\r': |
| 305 | if ( chLast == '\r' ) { |
| 306 | // Mac empty line |
| 307 | m_aLines.Add(wxEmptyString); |
| 308 | m_aTypes.Add(wxTextFileType_Mac); |
| 309 | } |
| 310 | else |
| 311 | chLast = '\r'; |
| 312 | break; |
| 313 | |
| 314 | default: |
| 315 | if ( chLast == '\r' ) { |
| 316 | // Mac line termination |
| 317 | m_aLines.Add(str); |
| 318 | m_aTypes.Add(wxTextFileType_Mac); |
| 319 | chLast = ch; |
| 320 | str = ch; |
| 321 | } |
| 322 | else { |
| 323 | // add to the current line |
| 324 | str += ch; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // anything in the last line? |
| 331 | if ( !str.IsEmpty() ) { |
| 332 | m_aTypes.Add(wxTextFileType_None); // no line terminator |
| 333 | m_aLines.Add(str); |
| 334 | } |
| 335 | |
| 336 | return TRUE; |
| 337 | } |
| 338 | |
| 339 | bool wxTextFile::Close() |
| 340 | { |
| 341 | m_aTypes.Clear(); |
| 342 | m_aLines.Clear(); |
| 343 | m_nCurLine = 0; |
| 344 | m_isOpened = FALSE; |
| 345 | |
| 346 | return TRUE; |
| 347 | } |
| 348 | |
| 349 | bool wxTextFile::Write(wxTextFileType typeNew) |
| 350 | { |
| 351 | wxTempFile fileTmp(m_strFile); |
| 352 | |
| 353 | if ( !fileTmp.IsOpened() ) { |
| 354 | wxLogError(_("can't write file '%s' to disk."), m_strFile.c_str()); |
| 355 | return FALSE; |
| 356 | } |
| 357 | |
| 358 | size_t nCount = m_aLines.Count(); |
| 359 | for ( size_t n = 0; n < nCount; n++ ) { |
| 360 | fileTmp.Write(m_aLines[n] + |
| 361 | GetEOL(typeNew == wxTextFileType_None ? m_aTypes[n] |
| 362 | : typeNew)); |
| 363 | } |
| 364 | |
| 365 | // replace the old file with this one |
| 366 | return fileTmp.Commit(); |
| 367 | } |
| 368 | |
| 369 | #endif // wxUSE_TEXTFILE |
| 370 | |