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 <astenum.hxx>
21 
22 #include <registry/version.h>
23 #include <registry/writer.hxx>
24 
AstEnum(const OString & name,AstScope * pScope)25 AstEnum::AstEnum(const OString& name, AstScope* pScope)
26     : AstType(NT_enum, name, pScope)
27     , AstScope(NT_enum)
28     , m_enumValueCount(0)
29 {
30 }
31 
~AstEnum()32 AstEnum::~AstEnum()
33 {
34 }
35 
checkValue(AstExpression * pExpr)36 AstConstant* AstEnum::checkValue(AstExpression* pExpr)
37 {
38     DeclList::const_iterator iter = getIteratorBegin();
39     DeclList::const_iterator end = getIteratorEnd();
40 
41     iter = std::find_if(iter, end, [&pExpr](AstDeclaration* pDecl) {
42         return static_cast<AstConstant*>(pDecl)->getConstValue()->compareLong(pExpr); });
43 
44     if (iter != end)
45         return static_cast<AstConstant*>(*iter);
46 
47     if ( pExpr->getExprValue()->u.lval > m_enumValueCount )
48         m_enumValueCount = pExpr->getExprValue()->u.lval + 1;
49 
50     return nullptr;
51 }
52 
dump(RegistryKey & rKey)53 bool AstEnum::dump(RegistryKey& rKey)
54 {
55     RegistryKey localKey;
56     if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey) != RegError::NO_ERROR)
57     {
58         fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
59                 idlc()->getOptions()->getProgramName().getStr(),
60                 getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
61         return false;
62     }
63 
64     sal_uInt16 nConst = getNodeCount(NT_enum_val);
65     if ( nConst > 0 )
66     {
67         typereg::Writer aBlob(
68             m_bPublished ? TYPEREG_VERSION_1 : TYPEREG_VERSION_0,
69             getDocumentation(), "", RT_TYPE_ENUM, m_bPublished,
70             OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), 0,
71             nConst, 0, 0);
72 
73         DeclList::const_iterator iter = getIteratorBegin();
74         DeclList::const_iterator end = getIteratorEnd();
75         sal_uInt16 index = 0;
76         while ( iter != end )
77         {
78             AstDeclaration* pDecl = *iter;
79             if ( pDecl->getNodeType() == NT_enum_val )
80                 static_cast<AstConstant*>(pDecl)->dumpBlob(aBlob, index++, false);
81 
82             ++iter;
83         }
84 
85         sal_uInt32 aBlobSize;
86         void const * pBlob = aBlob.getBlob(&aBlobSize);
87 
88         if (localKey.setValue("", RegValueType::BINARY,
89                                 const_cast<RegValue>(pBlob), aBlobSize) != RegError::NO_ERROR)
90         {
91             fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
92                     idlc()->getOptions()->getProgramName().getStr(),
93                     getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
94             return false;
95         }
96     }
97 
98     return true;
99 }
100 
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
102