diff --git a/main.cpp b/main.cpp index 691cecdb..6d58101d 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include "piscreen.h" #include "piethernet.h" #include "piintrospection.h" +#include "pifile.h" //struct MS { // //MS() {i = 0; f = 0.;} @@ -30,6 +31,12 @@ public: }; int main (int argc, char * argv[]) { + PIFile::setDefaultCharset("utf8"); + piCout << PIFile::defaultCharset(); + PIFile::setDefaultCharset("cp1251"); + piCout << PIFile::defaultCharset(); + return 0; + VC<30, double> x(1.5), y(3.3); double r = 0.0; PITimeMeasurer tm; diff --git a/src/core/piinit.cpp b/src/core/piinit.cpp index 2c13ec72..d946f2cb 100644 --- a/src/core/piinit.cpp +++ b/src/core/piinit.cpp @@ -85,6 +85,7 @@ void android_thread_exit_handler(int sig) { PIInit::PIInit() { PISystemInfo * sinfo = PISystemInfo::instance(); sinfo->execDateTime = PIDateTime::current(); + file_charset = 0; #ifndef ANDROID PISignals::setSlot(__sighandler__); PISignals::grabSignals(PISignals::UserDefined1); @@ -253,6 +254,8 @@ PIInit::PIInit() { PIInit::~PIInit() { + if (file_charset) delete file_charset; + file_charset = 0; #ifdef WINDOWS WSACleanup(); //if (setTimerResolution) setTimerResolutionAddr(prev_res, TRUE, &prev_res); diff --git a/src/core/piinit.h b/src/core/piinit.h index cc0719fc..3e41d9e8 100644 --- a/src/core/piinit.h +++ b/src/core/piinit.h @@ -28,6 +28,9 @@ #include "piincludes.h" +class PIFile; + + class __PIInit_Initializer__ { public: __PIInit_Initializer__(); @@ -38,6 +41,7 @@ public: class PIInit { + friend class PIFile; public: PIInit(); ~PIInit(); @@ -48,6 +52,7 @@ private: HMODULE ntlib; ULONG prev_res; #endif + char * file_charset; }; static __PIInit_Initializer__ __piinit_initializer__; diff --git a/src/io/pifile.cpp b/src/io/pifile.cpp index 51e5b2ca..6a54f7df 100755 --- a/src/io/pifile.cpp +++ b/src/io/pifile.cpp @@ -40,12 +40,12 @@ #endif #define S_IFHDN 0x40 #if defined(QNX) || defined(ANDROID) -# define _fopen_call_ fopen -# define _fseek_call_ fseek -# define _ftell_call_ ftell -# define _stat_struct_ struct stat -# define _stat_call_ stat -# define _stat_link_ lstat +# define _fopen_call_ fopen +# define _fseek_call_ fseek +# define _ftell_call_ ftell +# define _stat_struct_ struct stat +# define _stat_call_ stat +# define _stat_link_ lstat #else # if defined(MAC_OS) # define _fopen_call_ fopen @@ -56,9 +56,9 @@ # define _fseek_call_ fseeko64 # define _ftell_call_ ftello64 # endif -# define _stat_struct_ struct stat64 -# define _stat_call_ stat64 -# define _stat_link_ lstat64 +# define _stat_struct_ struct stat64 +# define _stat_call_ stat64 +# define _stat_link_ lstat64 #endif @@ -182,8 +182,8 @@ bool PIFile::closeDevice() { PIString PIFile::readLine() { PIString str; if (!opened_) return str; - int cc, cp = 0; - while (!isEnd() && cp < 4095) { + int cc; + while (!isEnd()) { cc = fgetc(fd); if (char(cc) == '\n' || cc == EOF) break; str.push_back(char(cc)); @@ -395,6 +395,22 @@ void PIFile::remove() { +const char * PIFile::defaultCharset() { + return PIInit::instance()->file_charset; +} + + +void PIFile::setDefaultCharset(const char * c) { + if (PIInit::instance()->file_charset) delete PIInit::instance()->file_charset; + PIInit::instance()->file_charset = 0; + if (!c) return; + PIInit::instance()->file_charset = new char[1024]; + memset(PIInit::instance()->file_charset, 0, 1024); + strcpy(PIInit::instance()->file_charset, c); + piCout << PIInit::instance()->file_charset; +} + + PIFile::FileInfo PIFile::fileInfo(const PIString & path) { FileInfo ret; diff --git a/src/io/pifile.h b/src/io/pifile.h index 5bc319a5..a52617c1 100755 --- a/src/io/pifile.h +++ b/src/io/pifile.h @@ -247,7 +247,12 @@ public: EVENT_HANDLER1(void, resize, llong, new_size) {resize(new_size, 0);} EVENT_HANDLER2(void, resize, llong, new_size, uchar, fill); - + //! + static const char * defaultCharset(); + + //! + static void setDefaultCharset(const char * c); + //! Returns opened temporary file with open mode "mode" static PIFile openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite) {return PIFile(PIString(tmpnam(0)), mode);}