home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / IText.cpp < prev    next >
C/C++ Source or Header  |  1997-03-21  |  19KB  |  622 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odmri
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.22 com/src/pubutils/src/IText.cpp, odmri, od96os2, odos29712d 12/3/96 16:33:31 [ 3/21/97 17:21:16 ]
  28. /*
  29.    File:    IText.cpp
  30.  
  31.    Contains:   Routines for manipulating ITexts.
  32.  
  33.    Owned by:   Vincent Lo
  34.  
  35.    Copyright:  ⌐ 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  36.  
  37.    To Do:
  38.  
  39.       These could all be rewritten to use the ODByteArray functions,
  40.       thus reducing code size.
  41.  
  42.    In Progress:
  43.  
  44. */
  45.  
  46. #define _OD_DONT_IMPORT_CPP_
  47.  
  48. #ifndef _ODEXCEPT_
  49. #include <ODExcept.h>
  50. #endif
  51.  
  52. #ifndef _UTILERRS_
  53. #include <UtilErrs.h>
  54. #endif
  55.  
  56. #ifndef _ODDEBUG_
  57. #include "ODDebug.h"
  58. #endif
  59.  
  60. #ifndef _ODTYPES_
  61. #include "ODTypes.h"
  62. #endif
  63.  
  64. #ifndef _ITEXT_
  65. #include "IText.h"
  66. #endif
  67.  
  68. #ifndef _BARRAY_
  69. #include "BArray.h"
  70. #endif
  71.  
  72. #ifndef _ODMEMORY_
  73. #include "ODMemory.h"
  74. #endif
  75.  
  76. #ifndef  SOM_Module_include5cStdTypes_OpenDoc_StdTypes_defined
  77. #include <StdTypes.xh>
  78. #endif
  79.  
  80. #include "string.h"
  81.  
  82.  
  83. #if defined(__IBMCPP__) || defined(_IBMR2)
  84. // These are for creating an IText with default code page info
  85. #include <nl_types.h>
  86. #include <langinfo.h>
  87. #endif
  88. #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_) || defined(_PLATFORM_AIX_)
  89. #include <string.h>
  90. #define kIBMCodePagePrefix "IBM-"
  91. #endif
  92.  
  93. #ifdef _PLATFORM_AIX_
  94. #include <nl_types.h>
  95. #include <langinfo.h>
  96. ODScriptCode ODGetCodePageIdentifier();
  97. #endif
  98.  
  99. //==============================================================================
  100. // IText
  101. //==============================================================================
  102.  
  103.  
  104. #if ODDebug
  105.    #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_) || defined(_PLATFORM_AIX_)
  106.    #define CHECK_FORMAT(I)               ASSERTMSG_DEBUG((I) && (I)->format==kODIBMCodePageText,  \
  107.                                                   kODErrAssertionFailed, "Unknown IText format")
  108.  
  109.    #else // _PLATFORM_MACINTOSH_
  110.    #define CHECK_FORMAT(I)               ASSERTMSG_DEBUG((I) && (I)->format==kODTraditionalMacText,  \
  111.                                                   kODErrAssertionFailed, "Unknown IText format")
  112.    #endif
  113.  
  114. #else
  115.  
  116.    #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_) || defined(_PLATFORM_AIX_)
  117.    #define CHECK_FORMAT(I)               if( (I)->format==kODIBMCodePageText ) ;         \
  118.                                             else THROW(kODErrInvalidITextFormat)
  119.  
  120.    #else // _PLATFORM_MACINTOSH_
  121.    #define CHECK_FORMAT(I)               if( (I)->format==kODTraditionalMacText ) ;         \
  122.                                             else THROW(kODErrInvalidITextFormat)
  123.    #endif
  124. #endif
  125.  
  126.  
  127. //------------------------------------------------------------------------------
  128. // NewIText  [static]   Creates an uninitialized empty IText.
  129. //------------------------------------------------------------------------------
  130.  
  131. static ODIText* NewIText( )
  132. {
  133.    ODIText* result = new ODIText;
  134.  
  135. #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_) || defined(_PLATFORM_AIX_)
  136.    result->format = kODIBMCodePageText;
  137. #else  // _PLATFORM_MACINTOSH_
  138.    result->format = kODTraditionalMacText;
  139. #endif
  140.    result->text._buffer = kODNULL;
  141.    return result;
  142. }
  143.  
  144.  
  145. //------------------------------------------------------------------------------
  146. // CreateITextCString
  147. //------------------------------------------------------------------------------
  148.  
  149. WIN32_DLLEXPORT ODIText* CreateITextCString(ODScriptCode scriptCode, ODLangCode langCode, char* text)
  150. {
  151.    ODIText* result = NewIText();
  152.  
  153.    try {
  154.       SetITextString(result, text);
  155.       SetITextScriptCode(result, scriptCode);
  156.       SetITextLangCode(result, langCode);
  157.    } catch (ODException _exception) {
  158.       DisposeIText(result);
  159.       throw;
  160.    }
  161.  
  162.    return result;
  163. }
  164.  
  165.  
  166. //------------------------------------------------------------------------------
  167. // CreateITextPString
  168. //------------------------------------------------------------------------------
  169.  
  170. WIN32_DLLEXPORT ODIText* CreateITextPString(ODScriptCode scriptCode, ODLangCode langCode, StringPtr text)
  171. {
  172.    ODIText* result = NewIText();
  173.  
  174.    try {
  175.       SetITextString(result, text);
  176.       SetITextScriptCode(result, scriptCode);
  177.       SetITextLangCode(result, langCode);
  178.    } catch (ODException _exception) {
  179.       DisposeIText(result);
  180.       throw;
  181.    }
  182.  
  183.    return result;
  184. }
  185.  
  186.  
  187. //------------------------------------------------------------------------------
  188. // CreateITextClear
  189. //------------------------------------------------------------------------------
  190.  
  191. WIN32_DLLEXPORT ODIText* CreateITextClear(ODScriptCode scriptCode, ODLangCode langCode, ODSize textLen)
  192. {
  193.    ODIText* result = NewIText();
  194.  
  195.    try {
  196.       SetITextBufferSize(result, textLen+sizeof(ODTradITextDataHeader), kODFalse);
  197.       SetITextScriptCode(result, scriptCode);
  198.       SetITextLangCode(result, langCode);
  199.    } catch (ODException _exception) {
  200.       DisposeIText(result);
  201.       throw;
  202.    }
  203.  
  204.    return result;
  205. }
  206.  
  207.  
  208. //------------------------------------------------------------------------------
  209. // CreateIText
  210. //------------------------------------------------------------------------------
  211.  
  212. WIN32_DLLEXPORT ODIText*                 CreateITextWLen(ODScriptCode scriptCode, ODLangCode langCode,
  213.                   ODUByte* text, ODSize textLength )
  214. {
  215.    ODIText* result = NewIText();
  216.  
  217.    try {
  218.       SetITextBufferSize(result,
  219.                                          textLength + sizeof(ODTradITextDataHeader), kODFalse);
  220.       ODBlockMove(text,
  221.                &(((ODTradITextData*)(result->text._buffer))->theText),
  222.                textLength);
  223.       SetITextScriptCode(result, scriptCode);
  224.       SetITextLangCode(result, langCode);
  225.    } catch (ODException _exception) {
  226.       DisposeIText(result);
  227.       throw;
  228.    }
  229.  
  230.    return result;
  231. }
  232.  
  233.  
  234. //------------------------------------------------------------------------------
  235. // CreateIText from a char* with no script code or lang code. Since on Windows and
  236. // and OS/2 there is the no seperation of the notions of script and language  it is
  237. // possible to create an IText simply from a char*. The code page is detected from the
  238. // and this information is kept in the script code field. - OF.
  239. //------------------------------------------------------------------------------
  240.  
  241. WIN32_DLLEXPORT ODIText*        CreateITextFromCharPtr(char* text)
  242. {
  243.  
  244. #if defined(_PLATFORM_OS2_)
  245.     ODScriptCode scriptCode;
  246.     ULONG  aulCpList[8]  = {0},                 // Code page list
  247.       ulBufSize      = 8 * sizeof(ULONG),       // Size of output list
  248.       ulListSize      = 0,                      // Size of list returned
  249.       indx            = 0;                      // Loop index
  250.     APIRET rc     = NO_ERROR;                   // Return code
  251.  
  252.     rc = DosQueryCp(ulBufSize,      /* Length of output code page list  */
  253.                     aulCpList,            /* List of code pages         */
  254.                     &ulListSize);         /* Length of list returned    */
  255.  
  256.     if (rc == NO_ERROR) {
  257.       scriptCode = aulCpList[0]; // primary code page is index 0
  258.     } else {
  259.       scriptCode = 0;  // It's not bad enough to stop.
  260.     }
  261.  
  262. #else
  263.    #ifdef _PLATFORM_WIN32_
  264.       ODScriptCode scriptCode=GetOEMCP();
  265.    #endif
  266.    #ifdef _PLATFORM_AIX_
  267.       ODScriptCode scriptCode=ODGetCodePageIdentifier();
  268.    #endif
  269. #endif
  270.  
  271.    ODSize textLength=strlen(text);
  272.  
  273.    ODIText* result = NewIText();
  274.  
  275.    try {
  276.       SetITextBufferSize(result,
  277.                                          textLength + sizeof(ODTradITextDataHeader), kODFalse);
  278.       ODBlockMove(text,
  279.                &(((ODTradITextData*)(result->text._buffer))->theText),
  280.                textLength);
  281.       SetITextScriptCode(result, scriptCode);
  282.  
  283.    //There is no langCode assoicated with kODIBMCodePageText. So, set the langCode to 0
  284.       SetITextLangCode(result, 0);
  285.    } catch (ODException _exception) {
  286.       DisposeIText(result);
  287.       throw;
  288.    }
  289.  
  290.    return result;
  291.  
  292. }
  293. //------------------------------------------------------------------------------
  294. // CopyIText
  295. //------------------------------------------------------------------------------
  296.  
  297. WIN32_DLLEXPORT ODIText* CopyIText(ODIText* original)
  298. {
  299.    CHECK_FORMAT(original);
  300.  
  301.    ODIText* result = NewIText();
  302.  
  303.    try {
  304.       unsigned long dataSize = original->text._length;
  305.       result->format = original->format;
  306.       result->text._buffer = (octet *)ODNewPtr(dataSize);
  307.       result->text._maximum = dataSize;
  308.       result->text._length  = dataSize;
  309.       ODBlockMove(original->text._buffer, result->text._buffer, dataSize);
  310.    } catch (ODException _exception) {
  311.       DisposeIText(result);
  312.       throw;
  313.    }
  314.  
  315.    return result;
  316. }
  317.  
  318. //------------------------------------------------------------------------------
  319. // CopyITextStruct
  320. //------------------------------------------------------------------------------
  321.  
  322. WIN32_DLLEXPORT ODIText CopyITextStruct(ODIText* original)
  323. {
  324.    ODIText  result;
  325.  
  326.    CHECK_FORMAT(original);
  327.  
  328.    result.format = original->format;
  329.    result.text = CopyByteArrayStruct(&(original->text));
  330.  
  331.    return result;
  332. }
  333.  
  334.  
  335. //------------------------------------------------------------------------------
  336. // SetITextBufferSize
  337. //------------------------------------------------------------------------------
  338.  
  339. WIN32_DLLEXPORT ODIText*
  340. SetITextBufferSize( ODIText* iText, ODSize bufferSize, ODBoolean preserveContents )
  341. {
  342.    // Will create new IText if input is NULL.
  343.  
  344.    if( iText && iText->text._maximum==bufferSize && iText->text._buffer ) {
  345.       iText->text._length = bufferSize;
  346.       return iText;                                     // No-op
  347.    }
  348.  
  349.    ODTradITextData *buffer = (ODTradITextData*) ODNewPtr(bufferSize);
  350.    if( !iText ) {
  351.       try {
  352.          iText = NewIText();
  353.       } catch (ODException _exception) {
  354.          ODDisposePtr(buffer);
  355.          throw;
  356.       }
  357.    } else if( iText->text._buffer ) {
  358.       if( preserveContents )
  359.          ODBlockMove(iText->text._buffer, buffer, bufferSize);
  360.       ODDisposePtr(iText->text._buffer);
  361.    }
  362.    iText->text._buffer = (octet*) buffer;
  363.    iText->text._maximum = bufferSize;
  364.    iText->text._length = bufferSize;
  365.    return iText;
  366. }
  367.  
  368.  
  369. //------------------------------------------------------------------------------
  370. // GetITextPtr
  371. //------------------------------------------------------------------------------
  372.  
  373. WIN32_DLLEXPORT char* GetITextPtr( ODIText *iText )
  374. {
  375.    CHECK_FORMAT(iText);
  376.    return ((ODTradITextData*)iText->text._buffer)->theText;
  377. }
  378.  
  379.  
  380. //------------------------------------------------------------------------------
  381. // SetITextScriptCode
  382. //------------------------------------------------------------------------------
  383.  
  384. WIN32_DLLEXPORT void SetITextScriptCode(ODIText* iText, ODScriptCode scriptCode)
  385. {
  386.    CHECK_FORMAT(iText);
  387.    ODTradITextData *data = (ODTradITextData*) iText->text._buffer;
  388.    data->theScriptCode = scriptCode;
  389. }
  390.  
  391.  
  392. //------------------------------------------------------------------------------
  393. // GetITextScriptCode
  394. //------------------------------------------------------------------------------
  395.  
  396. WIN32_DLLEXPORT ODScriptCode GetITextScriptCode(ODIText* iText)
  397. {
  398.    CHECK_FORMAT(iText);
  399.    ODTradITextData *data = (ODTradITextData*) iText->text._buffer;
  400.    return data->theScriptCode;
  401. }
  402.  
  403.  
  404. //------------------------------------------------------------------------------
  405. // SetITextLangCode
  406. //------------------------------------------------------------------------------
  407.  
  408. WIN32_DLLEXPORT void SetITextLangCode(ODIText* iText, ODLangCode langCode)
  409. {
  410.    CHECK_FORMAT(iText);
  411.    ODTradITextData *data = (ODTradITextData*) iText->text._buffer;
  412.    data->theLangCode = langCode;
  413. }
  414.  
  415.  
  416. //------------------------------------------------------------------------------
  417. // GetITextLangCode
  418. //------------------------------------------------------------------------------
  419.  
  420. WIN32_DLLEXPORT ODLangCode GetITextLangCode(ODIText* iText)
  421. {
  422.    CHECK_FORMAT(iText);
  423.    ODTradITextData *data = (ODTradITextData*) iText->text._buffer;
  424.    return data->theLangCode;
  425. }
  426.  
  427.  
  428. //------------------------------------------------------------------------------
  429. // SetITextStringLength
  430. //------------------------------------------------------------------------------
  431.  
  432. WIN32_DLLEXPORT ODIText* SetITextStringLength( ODIText* iText, ODSize length, ODBoolean preserveText )
  433. {
  434.    ODTradITextDataHeader data = {0,0};
  435.    if( iText ) {
  436.       CHECK_FORMAT(iText);
  437.       if( iText->text._buffer )
  438.          data = * (ODTradITextDataHeader*) iText->text._buffer;
  439.    }
  440.    iText = SetITextBufferSize(iText,length+sizeof(ODTradITextDataHeader),preserveText);
  441.    if( !preserveText )
  442.       * (ODTradITextDataHeader*) iText->text._buffer = data;
  443.    return iText;
  444. }
  445.  
  446.  
  447. //------------------------------------------------------------------------------
  448. // GetITextStringLength
  449. //------------------------------------------------------------------------------
  450.  
  451. WIN32_DLLEXPORT ODULong GetITextStringLength(ODIText* iText)
  452. {
  453.    CHECK_FORMAT(iText);
  454.    if( iText->text._buffer )
  455.       return iText->text._length - sizeof(ODTradITextDataHeader);
  456.    else
  457.       return 0;
  458. }
  459.  
  460.  
  461. //------------------------------------------------------------------------------
  462. // SetITextCString
  463. //------------------------------------------------------------------------------
  464.  
  465. WIN32_DLLEXPORT void SetITextCString(ODIText* iText, char* text)
  466. {
  467.    CHECK_FORMAT(iText);
  468.    ODSize textLen = text ?strlen(text) :0;
  469.    SetITextStringLength(iText,textLen,kODFalse);
  470.    ODBlockMove(text, ((ODTradITextData*)iText->text._buffer)->theText, textLen);
  471. }
  472.  
  473.  
  474. //------------------------------------------------------------------------------
  475. // SetITextPString
  476. //------------------------------------------------------------------------------
  477.  
  478. WIN32_DLLEXPORT void SetITextPString(ODIText* iText, StringPtr text)
  479. {
  480.    CHECK_FORMAT(iText);
  481.    ODSize textLen = text ?text[0] :0;
  482.    SetITextStringLength(iText,textLen,kODFalse);
  483.    ODBlockMove(&text[1], ((ODTradITextData*)iText->text._buffer)->theText, textLen);
  484. }
  485.  
  486.  
  487. //------------------------------------------------------------------------------
  488. // SetITextText
  489. //------------------------------------------------------------------------------
  490.  
  491. WIN32_DLLEXPORT void SetITextText(ODIText* iText, ODUByte* text, ODSize textLength)
  492. {
  493.    CHECK_FORMAT(iText);
  494.    SetITextStringLength(iText, textLength, kODFalse);
  495.    ODBlockMove(text, ((ODTradITextData*)iText->text._buffer)->theText,
  496.             textLength);
  497. }
  498.  
  499.  
  500. //------------------------------------------------------------------------------
  501. // GetITextCString
  502. //------------------------------------------------------------------------------
  503.  
  504. WIN32_DLLEXPORT char* GetITextCString(ODIText* iText, char *cstring)
  505. {
  506.    CHECK_FORMAT(iText);
  507.    ODSize len;
  508.    if( iText->text._buffer )
  509.       len = iText->text._length - sizeof(ODTradITextDataHeader);
  510.    else
  511.       len = 0;
  512.  
  513.    if( !cstring )
  514.       cstring = new char[len+1];
  515.    cstring[len] = '\0';
  516.    if( len )
  517.       ODBlockMove(((ODTradITextData*)iText->text._buffer)->theText,
  518.                cstring, len);
  519.    return cstring;
  520. }
  521.  
  522.  
  523. //------------------------------------------------------------------------------
  524. // GetITextPString
  525. //------------------------------------------------------------------------------
  526.  
  527. WIN32_DLLEXPORT StringPtr GetITextPString(ODIText* iText, Str255 pstring)
  528. {
  529.    CHECK_FORMAT(iText);
  530.    ODSize len;
  531.    if( iText->text._buffer )
  532.       len = iText->text._length - sizeof(ODTradITextDataHeader);
  533.    else
  534.       len = 0;
  535.  
  536.    if( !pstring )
  537.       pstring = new unsigned char[len+1];
  538.    if( len )
  539.       ODBlockMove(((ODTradITextData*)iText->text._buffer)->theText,
  540.                &pstring[1], len);
  541.    pstring[0] = len;
  542.    return pstring;
  543. }
  544.  
  545.  
  546. //------------------------------------------------------------------------------
  547. // DisposeIText
  548. //------------------------------------------------------------------------------
  549.  
  550. WIN32_DLLEXPORT void DisposeIText(ODIText* iText)
  551. {
  552.    if( iText ) {
  553.       CHECK_FORMAT(iText);
  554.       DisposeITextStruct(*iText);
  555.       delete iText;
  556.    }
  557. }
  558.  
  559.  
  560. #ifdef _PLATFORM_AIX_
  561. //========================================================
  562. // This function retrieves the codeset from the system and
  563. // then returns an identifier representing the codeset.
  564. //========================================================
  565. ODScriptCode ODGetCodePageIdentifier()
  566.  {
  567.   char* codeset=nl_langinfo(CODESET);  // get codepage from system
  568.  
  569.   // now determine which codepage we are using and
  570.   // return the code that identifies it.
  571.  
  572.    if(!codeset)
  573.      {
  574.      WARNMSG(WARN_INDEX(0),"Error: codeset is NULL!,File = %s\n",__FILE__);
  575.      return kUnknown;
  576.      }
  577.  
  578.   if(!strcmp("IBM-1046",codeset))       
  579.      return kIBM_1046;
  580.   else if(!strcmp("ISO8859-6",codeset))
  581.       return kISO8859_6;
  582.   else if(!strcmp("ISO8859-1",codeset))
  583.       return kISO8859_1;
  584.   else if(!strcmp("ISO8859-5",codeset))
  585.       return kISO8859_5;
  586.   else if(!strcmp("IBM-850",codeset))   
  587.       return kIBM_850;
  588.   else if(!strcmp("IBM-eucTW",codeset)) 
  589.       return kIBM_eucTW;
  590.   else if(!strcmp("ISO8859-2",codeset))
  591.       return kISO8859_2;
  592.   else if(!strcmp("IBM-932",codeset)) 
  593.       return kIBM_932;
  594.   else if(!strcmp("IBM-eucJP",codeset)) 
  595.       return kIBM_eucJP;
  596.   else if(!strcmp("IBM-eucKR",codeset)) 
  597.       return kIBM_eucKR;
  598.   else if(!strcmp("ISO8859-7",codeset)) 
  599.       return kISO8859_7;
  600.   else if(!strcmp("IBM-856",codeset)) 
  601.       return kIBM_856;
  602.   else if(!strcmp("ISO8859-8",codeset)) 
  603.       return kISO8859_8;
  604.   else if(!strcmp("IBM-eucCN",codeset)) 
  605.       return kIBM_eucCN;
  606.   else if(!strcmp("ISO8859-9",codeset)) 
  607.       return kISO8859_9;
  608.   else if(!strcmp("Unicode",codeset)) 
  609.       return kUnicode;
  610.   else if(!strcmp("UTF-8",codeset)) 
  611.       return kUTF_8;
  612.   else
  613.     {
  614.       WARNMSG(WARN_INDEX(0),"Error: Unknown codepage!,File = %s\n",__FILE__);
  615.       return kUnknown;
  616.     }
  617.  }
  618. #endif // _PLATFORM_AIX_
  619.  
  620.  
  621.  
  622.