LCOV - code coverage report
Current view: top level - rsc/source/tools - rscchar.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 35 75 46.7 %
Date: 2015-06-13 12:38:46 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stdio.h>
      21             : #include <string.h>
      22             : #include <ctype.h>
      23             : 
      24             : #include <rsctools.hxx>
      25             : 
      26             : #include <rtl/textcvt.h>
      27             : #include <rtl/textenc.h>
      28             : #include <rtl/alloc.h>
      29             : 
      30       25046 : char * RscChar::MakeUTF8( char * pStr, sal_uInt16 nTextEncoding )
      31             : {
      32       25046 :     sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
      33       25046 :     if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
      34           0 :         RscExit( 10 );
      35             : 
      36       25046 :     char *          pOrgStr = new char[ nMaxUniCodeBuf ];
      37       25046 :     sal_uInt32      nOrgLen = 0;
      38             : 
      39       25046 :     char cOld = '1';
      40      589732 :     while( cOld != 0 )
      41             :     {
      42             :         char c;
      43             : 
      44      539640 :         if( *pStr == '\\' )
      45             :         {
      46         613 :             ++pStr;
      47         613 :             switch( *pStr )
      48             :             {
      49             :                 case 'a':
      50           0 :                     c = '\a';
      51           0 :                     break;
      52             :                 case 'b':
      53           1 :                     c = '\b';
      54           1 :                     break;
      55             :                 case 'f':
      56           0 :                     c = '\f';
      57           0 :                     break;
      58             :                 case 'n':
      59         412 :                     c = '\n';
      60         412 :                     break;
      61             :                 case 'r':
      62           0 :                     c = '\r';
      63           0 :                     break;
      64             :                 case 't':
      65          25 :                     c = '\t';
      66          25 :                     break;
      67             :                 case 'v':
      68           0 :                     c = '\v';
      69           0 :                     break;
      70             :                 case '\\':
      71           3 :                     c = '\\';
      72           3 :                     break;
      73             :                 case '?':
      74           0 :                     c = '\?';
      75           0 :                     break;
      76             :                 case '\'':
      77           0 :                     c = '\'';
      78           0 :                     break;
      79             :                 case '\"':
      80         172 :                     c = '\"';
      81         172 :                     break;
      82             :                 default:
      83             :                 {
      84           0 :                     if( '0' <= *pStr && '7' >= *pStr )
      85             :                     {
      86           0 :                         sal_uInt16  nChar = 0;
      87           0 :                         int  i = 0;
      88           0 :                         while( '0' <= *pStr && '7' >= *pStr && i != 3 )
      89             :                         {
      90           0 :                             nChar = nChar * 8 + (sal_uInt8)*pStr - (sal_uInt8)'0';
      91           0 :                             ++pStr;
      92           0 :                             i++;
      93             :                         }
      94           0 :                         if( nChar > 255 )
      95             :                         {
      96             :                             // value is too big, or more than 3 digits
      97           0 :                             delete [] pOrgStr;
      98           0 :                             return NULL;
      99             :                         }
     100           0 :                         c = (char)nChar;
     101           0 :                         pStr--;
     102             :                     }
     103           0 :                     else if( 'x' == *pStr )
     104             :                     {
     105           0 :                         sal_uInt16  nChar = 0;
     106           0 :                         int  i = 0;
     107           0 :                         ++pStr;
     108           0 :                         while( isxdigit( *pStr ) && i != 2 )
     109             :                         {
     110           0 :                             if( isdigit( *pStr ) )
     111           0 :                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'0';
     112           0 :                             else if( isupper( *pStr ) )
     113           0 :                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'A' +10;
     114             :                             else
     115           0 :                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'a' +10;
     116           0 :                             ++pStr;
     117           0 :                             i++;
     118             :                         }
     119           0 :                         c = (char)nChar;
     120           0 :                         pStr--;
     121             :                     }
     122             :                     else
     123           0 :                         c = *pStr;
     124             :                 };
     125             :             }
     126             :         }
     127             :         else
     128      539027 :             c = *pStr;
     129      539640 :         pOrgStr[ nOrgLen++ ] = c;
     130      539640 :         cOld = *pStr;
     131      539640 :         pStr++;
     132             :     }
     133             : 
     134       25046 :     sal_Unicode *   pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
     135       25046 :     rtl_TextToUnicodeConverter hConv = rtl_createTextToUnicodeConverter( nTextEncoding );
     136             : 
     137             :     sal_uInt32 nInfo;
     138             :     sal_Size   nSrcCvtBytes;
     139             :     sal_Size nUniSize = rtl_convertTextToUnicode( hConv, 0,
     140             :                                                 pOrgStr, nOrgLen,
     141             :                                                 pUniCode, nMaxUniCodeBuf,
     142             :                                                 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
     143             :                                                 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
     144             :                                                 | RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
     145             :                                                 | RTL_TEXTTOUNICODE_FLAGS_FLUSH,
     146             :                                                 &nInfo,
     147       25046 :                                                 &nSrcCvtBytes );
     148             : 
     149       25046 :     rtl_destroyTextToUnicodeConverter( hConv );
     150       25046 :     delete[] pOrgStr, pOrgStr = 0;
     151             : 
     152       25046 :     hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
     153             :     // factor of 6 is the maximum size of an UNICODE character as utf8
     154       25046 :     char * pUtf8 = static_cast<char *>(rtl_allocateMemory( nUniSize * 6 ));
     155             :     rtl_convertUnicodeToText( hConv, 0,
     156             :                             pUniCode, nUniSize,
     157             :                             pUtf8, nUniSize * 6,
     158             :                             RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
     159             :                             | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
     160             :                             | RTL_UNICODETOTEXT_FLAGS_FLUSH,
     161             :                             &nInfo,
     162       25046 :                             &nSrcCvtBytes );
     163             : 
     164       25046 :     rtl_destroyTextToUnicodeConverter( hConv );
     165       25046 :     delete[] pUniCode, pUniCode = 0;
     166             : 
     167       25046 :     return pUtf8;
     168             : };
     169             : 
     170             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11