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 <ctype.h>
21 : #include <stdio.h>
22 : #include <com/sun/star/beans/PropertyValues.hpp>
23 :
24 : #include <svl/ownlist.hxx>
25 :
26 : using namespace com::sun::star;
27 :
28 :
29 0 : static OUString parseString(const OUString & rCmd, sal_Int32 * pIndex)
30 : {
31 0 : OUString result;
32 :
33 0 : if(rCmd[*pIndex] == '\"') {
34 0 : (*pIndex) ++;
35 :
36 0 : sal_Int32 begin = *pIndex;
37 :
38 0 : while(*pIndex < rCmd.getLength() && rCmd[(*pIndex) ++] != '\"') ;
39 :
40 0 : result = rCmd.copy(begin, *pIndex - begin - 1);
41 : }
42 :
43 0 : return result;
44 : }
45 :
46 0 : static OUString parseWord(const OUString & rCmd, sal_Int32 * pIndex)
47 : {
48 0 : sal_Int32 begin = *pIndex;
49 :
50 0 : while(*pIndex < rCmd.getLength()
51 0 : && !isspace(sal::static_int_cast<int>(rCmd[*pIndex]))
52 0 : && rCmd[*pIndex] != '=')
53 0 : (*pIndex) ++;
54 :
55 0 : return rCmd.copy(begin, *pIndex - begin);
56 : }
57 :
58 0 : static void eatSpace(const OUString & rCmd, sal_Int32 * pIndex)
59 : {
60 0 : while(*pIndex < rCmd.getLength() && isspace(sal::static_int_cast<int>(rCmd[*pIndex])))
61 0 : (*pIndex) ++;
62 0 : }
63 :
64 : /**
65 : * Text is parsed and the single commands are added to the list.
66 : *
67 : * @returns bool true
68 : * The text was correctly parsed
69 : false
70 : The text was not parsed correctly
71 : */
72 0 : bool SvCommandList::AppendCommands
73 : (
74 : const OUString & rCmd, /* This text is translated to commands */
75 : sal_Int32 * pEaten /* Count of chars that have been read */
76 : )
77 : {
78 0 : sal_Int32 index = 0;
79 0 : while(index < rCmd.getLength())
80 : {
81 :
82 0 : eatSpace(rCmd, &index);
83 0 : OUString name = (rCmd[index] == '\"') ? parseString(rCmd, &index) : parseWord(rCmd, &index);
84 :
85 0 : eatSpace(rCmd, &index);
86 0 : OUString value;
87 0 : if(index < rCmd.getLength() && rCmd[index] == '=')
88 : {
89 0 : index ++;
90 :
91 0 : eatSpace(rCmd, &index);
92 0 : value = (rCmd[index] == '\"') ? parseString(rCmd, &index) : parseWord(rCmd, &index);
93 : }
94 :
95 0 : aCommandList.push_back( SvCommand(name, value));
96 0 : }
97 :
98 0 : *pEaten = index;
99 :
100 0 : return true;
101 : }
102 :
103 : /**
104 : * An object of the type SvCommand is created and the list is
105 : * attached.
106 : *
107 : * @returns SvCommand & The created object
108 : */
109 0 : SvCommand & SvCommandList::Append
110 : (
111 : const OUString & rCommand, /* The command */
112 : const OUString & rArg /* The command's argument */
113 : )
114 : {
115 0 : aCommandList.push_back( SvCommand( rCommand, rArg ) );
116 0 : return aCommandList.back();
117 : }
118 :
119 0 : bool SvCommandList::FillFromSequence( const com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue >& aCommandSequence )
120 : {
121 0 : const sal_Int32 nCount = aCommandSequence.getLength();
122 0 : OUString aCommand, aArg;
123 0 : OUString aApiArg;
124 0 : for( sal_Int32 nIndex=0; nIndex<nCount; nIndex++ )
125 : {
126 0 : aCommand = aCommandSequence[nIndex].Name;
127 0 : if( !( aCommandSequence[nIndex].Value >>= aApiArg ) )
128 0 : return false;
129 0 : aArg = aApiArg;
130 0 : Append( aCommand, aArg );
131 : }
132 :
133 0 : return true;
134 : }
135 :
136 0 : void SvCommandList::FillSequence( com::sun::star::uno::Sequence < com::sun::star::beans::PropertyValue >& aCommandSequence )
137 : {
138 0 : const sal_Int32 nCount = aCommandList.size();
139 0 : aCommandSequence.realloc( nCount );
140 0 : for( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ )
141 : {
142 0 : aCommandSequence[nIndex].Name = aCommandList[ nIndex ].GetCommand();
143 0 : aCommandSequence[nIndex].Handle = -1;
144 0 : aCommandSequence[nIndex].Value = uno::makeAny( aCommandList[ nIndex ].GetArgument() );
145 0 : aCommandSequence[nIndex].State = beans::PropertyState_DIRECT_VALUE;
146 : }
147 0 : }
148 :
149 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|