From: <cn...@us...> - 2008-12-30 23:50:20
|
Revision: 118 http://hgengine.svn.sourceforge.net/hgengine/?rev=118&view=rev Author: cnlohr Date: 2008-12-30 23:50:17 +0000 (Tue, 30 Dec 2008) Log Message: ----------- add utility function to dump a file to a buffer Modified Paths: -------------- Mercury2/src/MercuryUtil.cpp Mercury2/src/MercuryUtil.h Modified: Mercury2/src/MercuryUtil.cpp =================================================================== --- Mercury2/src/MercuryUtil.cpp 2008-12-30 23:46:54 UTC (rev 117) +++ Mercury2/src/MercuryUtil.cpp 2008-12-30 23:50:17 UTC (rev 118) @@ -1,4 +1,5 @@ #include <MercuryUtil.h> +#include <MercuryFile.h> #include <stdint.h> #ifndef WIN32 @@ -43,6 +44,36 @@ } +long FileToString( const MString & sFileName, char * & data ) +{ + data = 0; + + MercuryFile * f = FILEMAN.Open( sFileName ); + if( !f ) return -1; + + int length = f->Length(); + + data = (char*)malloc( length + 1 ); + + if( !data ) + { + data = 0; + return -1; + } + + int r = f->Read( data, length ); + + if( r != length ) + { + free( data ); + data = 0; + return -1; + } + + return length; +} + + /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryUtil.h =================================================================== --- Mercury2/src/MercuryUtil.h 2008-12-30 23:46:54 UTC (rev 117) +++ Mercury2/src/MercuryUtil.h 2008-12-30 23:50:17 UTC (rev 118) @@ -67,6 +67,10 @@ template<typename T> unsigned long InstanceCounter<T>::m_count = 0; +///Open up filename: sFileName and dump it into a new buffer; you must delete the return value when done. +///The return value is -1 if there was an issue, otherwise it is valid. +long FileToString( const MString & sFileName, char * & data ); + #endif /* Copyright (c) 2008, Joshua Allen This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |