//--------------------------------------------------------------------------- #pragma hdrstop #include #include "WordGlobal.h" #pragma link "Word_2K_SRVR" //--------------------------------------------------------------------------- #pragma argsused //Fonctions de conversion /* ----------------------------------------------------------------------------- Dans la fonction suivante les règles de nommage sont telles: A indique un Ascci étendu char* W indique un wchar_t* cch est la taille en caractères cb est une taille en byte len est la longeur sans le zero terminal ------------------------------------------------------------------------------*/ //Fonctions d'allocation et suppression char * StrAllocThrowA(size_t cchSize) { return new char[cchSize]; } void StrFreeA(char * s) { delete[] s; } wchar_t * StrAllocThrowW(size_t cchSize) { return new wchar_t[cchSize]; } void StrFreeW(wchar_t * s) { delete[] s; } char * awcstombs(wchar_t const *sczW) { size_t const cchLenA = wcstombs(NULL, sczW, 1024); char * szA = StrAllocThrowA(cchLenA+1); wcstombs(szA, sczW, 1024); return szA; } wchar_t * ambstowcs(char const *sczA) { size_t const cchLenA = strlen(sczA); size_t const cchLenW = mbstowcs(NULL, sczA, cchLenA+1); wchar_t * szW = StrAllocThrowW(cchLenW+1); mbstowcs(szW, sczA, cchLenA+1); return szW; } inline void StrFreW( wchar_t * s) { return StrFreeW(s); } inline void StrFreA( char * s) { return StrFreeA(s); } static CWordReader* pWordReader; static bool IsAllocated; static char* ReturnBuffer; static bool bTempEOF; int WordOpen( char* FileName) { erc ErrorCode; bool bIsEndOfText; wchar_t* NormFile; std::wstring WordFile; int IsStatus; IsAllocated = false; NormFile = ambstowcs( FileName); // alloue un bloc de wchar_t dans NormFile qu'il va falloir libérer WordFile = NormFile; ErrorCode = INIT_NO_ERROR; pWordReader = new CWordReader; ErrorCode = pWordReader->StartWordApi(); if (ErrorCode == INIT_NO_ERROR) { ErrorCode = pWordReader->OpenDocument( WordFile); if (ErrorCode == INIT_NO_ERROR) { ErrorCode = pWordReader->InitializePointBloc(bIsEndOfText); if (ErrorCode == INIT_NO_ERROR) IsStatus = INIT_NO_ERROR; else IsStatus= FAILURE; } else IsStatus=FAILURE; } StrFreW ( NormFile); return (IsStatus); } int wstrlen(char* String) { int i=0; while( String[i++]>0); return (i-1); } char* WordBlocRead(bool* pIsEndOfText) { erc ErrorCode; wchar_t* NormText; std::wstring WordText; char* TempText; CBloc* pBloc; bool bTempEOF; int i; if( IsAllocated) { delete (ReturnBuffer); IsAllocated =false; } ReturnBuffer = new char[3*1024]; IsAllocated=true; for( i=0; i < 3*1024; i++) ReturnBuffer[i]=0; bTempEOF = false; ErrorCode = pWordReader->GoNextBloc(bTempEOF); if (bTempEOF) *pIsEndOfText = bTempEOF; if (ErrorCode == INIT_NO_ERROR) { pBloc = new CBloc(); ErrorCode = pWordReader->ReadBloc(pBloc); if (ErrorCode == INIT_NO_ERROR) { WordText = pBloc->wsBlocText; //wstring) NormText = const_cast( WordText.c_str() ); // wchar_t TempText = awcstombs ( NormText); //alloue un bloc pour NormText qu'il va falloir libérer // wchar_t* en Char* strcat( ReturnBuffer, TempText ); } else { strcat( ReturnBuffer, "echec de lecture du bloc\n"); } delete pBloc; pBloc = NULL; } StrFreA ( TempText); return (ReturnBuffer); } int WordClose( ) { erc ErrorCode; ErrorCode = INIT_NO_ERROR; ErrorCode = pWordReader->CloseDocument(); ErrorCode = pWordReader->StopWordApi(); //Destruction du WordReader if (IsAllocated) delete (ReturnBuffer); delete ( pWordReader); return ((int)ErrorCode); } //---------------------------------------------------------------------------