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 "oox/token/tokenmap.hxx"
21 :
22 : #include <string.h>
23 : #include <rtl/strbuf.hxx>
24 : #include <rtl/string.hxx>
25 : #include "oox/token/tokens.hxx"
26 :
27 : namespace oox {
28 :
29 :
30 : using ::com::sun::star::uno::Sequence;
31 :
32 :
33 : namespace {
34 : // include auto-generated Perfect_Hash
35 : #if defined __clang__
36 : #if __has_warning("-Wdeprecated-register")
37 : #pragma GCC diagnostic push
38 : #pragma GCC diagnostic ignored "-Wdeprecated-register"
39 : #endif
40 : #endif
41 : #include "tokenhash.inc"
42 : #if defined __clang__
43 : #if __has_warning("-Wdeprecated-register")
44 : #pragma GCC diagnostic pop
45 : #endif
46 : #endif
47 : } // namespace
48 :
49 :
50 :
51 0 : TokenMap::TokenMap() :
52 0 : maTokenNames( static_cast< size_t >( XML_TOKEN_COUNT ) )
53 : {
54 : static const sal_Char* sppcTokenNames[] =
55 : {
56 : // include auto-generated C array with token names as C strings
57 : #include "tokennames.inc"
58 : ""
59 : };
60 :
61 0 : const sal_Char* const* ppcTokenName = sppcTokenNames;
62 0 : for( TokenNameVector::iterator aIt = maTokenNames.begin(), aEnd = maTokenNames.end(); aIt != aEnd; ++aIt, ++ppcTokenName )
63 : {
64 0 : OString aUtf8Token( *ppcTokenName );
65 0 : aIt->maUniName = OStringToOUString( aUtf8Token, RTL_TEXTENCODING_UTF8 );
66 0 : aIt->maUtf8Name = Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUtf8Token.getStr() ), aUtf8Token.getLength() );
67 0 : }
68 :
69 : #if OSL_DEBUG_LEVEL > 0
70 : // check that the perfect_hash is in sync with the token name list
71 : bool bOk = true;
72 : for( sal_Int32 nToken = 0; bOk && (nToken < XML_TOKEN_COUNT); ++nToken )
73 : {
74 : // check that the getIdentifier <-> getToken roundtrip works
75 : OString aUtf8Name = OUStringToOString( maTokenNames[ nToken ].maUniName, RTL_TEXTENCODING_UTF8 );
76 : struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
77 : bOk = pToken && (pToken->nToken == nToken);
78 : OSL_ENSURE( bOk, OStringBuffer( "TokenMap::TokenMap - token list broken, #" ).
79 : append( nToken ).append( ", '" ).append( aUtf8Name ).append( '\'' ).getStr() );
80 : }
81 : #endif
82 :
83 0 : for (unsigned char c = 'a'; c <= 'z'; c++)
84 : {
85 : struct xmltoken* pToken = Perfect_Hash::in_word_set(
86 0 : reinterpret_cast< const char* >( &c ), 1 );
87 0 : mnAlphaTokens[ c - 'a' ] = pToken ? pToken->nToken : XML_TOKEN_INVALID;
88 : }
89 0 : }
90 :
91 0 : TokenMap::~TokenMap()
92 : {
93 0 : }
94 :
95 0 : OUString TokenMap::getUnicodeTokenName( sal_Int32 nToken ) const
96 : {
97 0 : if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
98 0 : return maTokenNames[ static_cast< size_t >( nToken ) ].maUniName;
99 0 : return OUString();
100 : }
101 :
102 0 : sal_Int32 TokenMap::getTokenFromUnicode( const OUString& rUnicodeName ) const
103 : {
104 0 : OString aUtf8Name = OUStringToOString( rUnicodeName, RTL_TEXTENCODING_UTF8 );
105 0 : struct xmltoken* pToken = Perfect_Hash::in_word_set( aUtf8Name.getStr(), aUtf8Name.getLength() );
106 0 : return pToken ? pToken->nToken : XML_TOKEN_INVALID;
107 : }
108 :
109 0 : Sequence< sal_Int8 > TokenMap::getUtf8TokenName( sal_Int32 nToken ) const
110 : {
111 0 : if( (0 <= nToken) && (static_cast< size_t >( nToken ) < maTokenNames.size()) )
112 0 : return maTokenNames[ static_cast< size_t >( nToken ) ].maUtf8Name;
113 0 : return Sequence< sal_Int8 >();
114 : }
115 :
116 0 : sal_Int32 TokenMap::getTokenPerfectHash( const char *pStr, sal_Int32 nLength ) const
117 : {
118 0 : struct xmltoken* pToken = Perfect_Hash::in_word_set( pStr, nLength );
119 0 : return pToken ? pToken->nToken : XML_TOKEN_INVALID;
120 : }
121 :
122 :
123 :
124 : } // namespace oox
125 :
126 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|