From 0a3d61c32e843d00b9fa87e575c1c7bd6af6ded1 Mon Sep 17 00:00:00 2001 From: stv Date: Wed, 6 Sep 2006 07:41:30 +0000 Subject: [PATCH] Function unix2dos --- src/Qtx/Qtx.cxx | 61 ++++++++++++++++++++++++++++++++++++++++++++++++- src/Qtx/Qtx.h | 1 + 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index ac512c146..8201c34d2 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -414,7 +414,7 @@ QString Qtx::addSlash( const QString& path ) /*! Name: dos2unix [static public] - Desc: Convert text file. Replace symbols "LF/CR" by symbol "LF". + Desc: Convert text file. Replace symbols "CR/LF" by symbol "LF". */ bool Qtx::dos2unix( const QString& absName ) { @@ -482,6 +482,65 @@ bool Qtx::dos2unix( const QString& absName ) return QDir().rename( QString( temp ), absName ); } +/*! + Name: unix2dos [static public] + Desc: Convert text file. Replace symbol "LF" by symbols "CR/LF". +*/ +bool Qtx::unix2dos( const QString& absName ) +{ + FILE* src = ::fopen( absName, "rb" ); + if ( !src ) + return false; + + /* we'll use temporary file */ + char temp[512] = { '\0' }; + QString dir = Qtx::dir( absName ); + FILE* tgt = ::fopen( strcpy( temp, ::tempnam( dir, "__x" ) ), "wb" ); + if ( !tgt ) + return false; + + /* temp -> result of conversion */ + const char CR = 0x0d; + const char LF = 0x0a; + bool prefCR = false; + + while( true ) + { + int outcnt = 0; + char inbuf[512], outbuf[1024]; + + /* convert buffer */ + int nbread = ::fread( inbuf, 1, sizeof( inbuf ), src ); + for ( int incnt = 0; incnt < nbread; incnt++ ) + { + if ( inbuf[incnt] == LF && !prefCR ) + outbuf[outcnt++] = CR; + outbuf[outcnt++] = inbuf[incnt]; + prefCR = inbuf[incnt] == CR; + } + + /* write converted buffer to temp file */ + int nbwri = ::fwrite( outbuf, 1, outcnt, tgt ); + if ( nbwri != outcnt ) + { + ::fclose( src ); + ::fclose( tgt ); + QFile::remove( QString( temp ) ); + return false; + } + if ( nbread != sizeof( inbuf ) ) + break; /* converted ok */ + } + ::fclose( src ); + ::fclose( tgt ); + + /* rename temp -> src */ + if ( !QFile::remove( absName ) ) + return false; + + return QDir().rename( QString( temp ), absName ); +} + /*! Name: rgbSet [static public] Desc: Pack the specified color into one integer RGB set. diff --git a/src/Qtx/Qtx.h b/src/Qtx/Qtx.h index 45933d112..864441bed 100755 --- a/src/Qtx/Qtx.h +++ b/src/Qtx/Qtx.h @@ -119,6 +119,7 @@ public: static bool mkDir( const QString& ); static bool rmDir( const QString& ); static bool dos2unix( const QString& ); + static bool unix2dos( const QString& ); static QString addSlash( const QString& ); static int rgbSet( const QColor& ); -- 2.39.2