LCOV - code coverage report
Current view: top level - rsc/source/tools - rscchar.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 45 75 60.0 %
Date: 2014-04-11 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       27861 : char * RscChar::MakeUTF8( char * pStr, sal_uInt16 nTextEncoding )
      31             : {
      32       27861 :     sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
      33       27861 :     if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
      34           0 :         RscExit( 10 );
      35             : 
      36       27861 :     char *          pOrgStr = new char[ nMaxUniCodeBuf ];
      37       27861 :     sal_uInt32      nOrgLen = 0;
      38             : 
      39       27861 :     char cOld = '1';
      40      672094 :     while( cOld != 0 )
      41             :     {
      42             :         char c;
      43             : 
      44      616372 :         if( *pStr == '\\' )
      45             :         {
      46         750 :             ++pStr;
      47         750 :             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         485 :                     c = '\n';
      60         485 :                     break;
      61             :                 case 'r':
      62           0 :                     c = '\r';
      63           0 :                     break;
      64             :                 case 't':
      65          50 :                     c = '\t';
      66          50 :                     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         209 :                     c = '\"';
      81         209 :                     break;
      82             :                 default:
      83             :                 {
      84           2 :                     if( '0' <= *pStr && '7' >= *pStr )
      85             :                     {
      86           2 :                         sal_uInt16  nChar = 0;
      87           2 :                         int  i = 0;
      88          10 :                         while( '0' <= *pStr && '7' >= *pStr && i != 3 )
      89             :                         {
      90           6 :                             nChar = nChar * 8 + (sal_uInt8)*pStr - (sal_uInt8)'0';
      91           6 :                             ++pStr;
      92           6 :                             i++;
      93             :                         }
      94           2 :                         if( nChar > 255 )
      95             :                         {
      96             :                             // value is too big, or more than 3 digits
      97           0 :                             delete [] pOrgStr;
      98           0 :                             return( NULL );
      99             :                         }
     100           2 :                         c = (char)nChar;
     101           2 :                         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      615622 :             c = *pStr;
     129      616372 :         pOrgStr[ nOrgLen++ ] = c;
     130      616372 :         cOld = *pStr;
     131      616372 :         pStr++;
     132             :     }
     133             : 
     134       27861 :     sal_Unicode *   pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
     135       27861 :     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       27861 :                                                 &nSrcCvtBytes );
     148             : 
     149       27861 :     rtl_destroyTextToUnicodeConverter( hConv );
     150       27861 :     delete[] pOrgStr, pOrgStr = 0;
     151             : 
     152       27861 :     hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
     153             :     // factor of 6 is the maximum size of an UNICODE character as utf8
     154       27861 :     char * pUtf8 = (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       27861 :                             &nSrcCvtBytes );
     163             : 
     164       27861 :     rtl_destroyTextToUnicodeConverter( hConv );
     165       27861 :     delete[] pUniCode, pUniCode = 0;
     166             : 
     167       27861 :     return pUtf8;
     168             : };
     169             : 
     170             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10