/src/alembic/lib/Alembic/Util/TokenMap.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //-***************************************************************************** |
2 | | // |
3 | | // Copyright (c) 2009-2012, |
4 | | // Sony Pictures Imageworks Inc. and |
5 | | // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd. |
6 | | // |
7 | | // All rights reserved. |
8 | | // |
9 | | // Redistribution and use in source and binary forms, with or without |
10 | | // modification, are permitted provided that the following conditions are |
11 | | // met: |
12 | | // * Redistributions of source code must retain the above copyright |
13 | | // notice, this list of conditions and the following disclaimer. |
14 | | // * Redistributions in binary form must reproduce the above |
15 | | // copyright notice, this list of conditions and the following disclaimer |
16 | | // in the documentation and/or other materials provided with the |
17 | | // distribution. |
18 | | // * Neither the name of Industrial Light & Magic nor the names of |
19 | | // its contributors may be used to endorse or promote products derived |
20 | | // from this software without specific prior written permission. |
21 | | // |
22 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
24 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
25 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
26 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
27 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
28 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
29 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
32 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | | // |
34 | | //-***************************************************************************** |
35 | | |
36 | | //-***************************************************************************** |
37 | | //! \file Alembic/Util/TokenMap.cpp |
38 | | //! \brief The body file containing the class implementation for |
39 | | //! the \ref Alembic::Util::TokenMap class |
40 | | //-***************************************************************************** |
41 | | |
42 | | #include <Alembic/Util/TokenMap.h> |
43 | | |
44 | | namespace Alembic { |
45 | | namespace Util { |
46 | | namespace ALEMBIC_VERSION_NS { |
47 | | |
48 | | //-***************************************************************************** |
49 | | void TokenMap::set( const std::string &config, |
50 | | char pairSep, |
51 | | char assignSep ) |
52 | 0 | { |
53 | 0 | std::size_t lastPair = 0; |
54 | 0 | while(1) |
55 | 0 | { |
56 | 0 | std::size_t curPair = config.find(pairSep, lastPair); |
57 | 0 | std::size_t curAssign = config.find(assignSep, lastPair); |
58 | |
|
59 | 0 | if (curAssign != std::string::npos) |
60 | 0 | { |
61 | 0 | std::size_t endPos = std::string::npos; |
62 | 0 | if (curPair != endPos) |
63 | 0 | { |
64 | 0 | endPos = curPair - curAssign - 1; |
65 | 0 | } |
66 | |
|
67 | 0 | m_map[config.substr(lastPair, curAssign - lastPair)] = |
68 | 0 | config.substr(curAssign + 1, endPos); |
69 | 0 | } |
70 | |
|
71 | 0 | if (curPair == std::string::npos) |
72 | 0 | { |
73 | 0 | return; |
74 | 0 | } |
75 | | |
76 | 0 | lastPair = curPair + 1; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | //-***************************************************************************** |
81 | | void TokenMap::setUnique( const std::string &config, |
82 | | char pairSep, |
83 | | char assignSep, |
84 | | bool quiet ) |
85 | 337k | { |
86 | 337k | std::size_t lastPair = 0; |
87 | 358k | while(1) |
88 | 358k | { |
89 | 358k | std::size_t curPair = config.find(pairSep, lastPair); |
90 | 358k | std::size_t curAssign = config.find(assignSep, lastPair); |
91 | | |
92 | 358k | if (curAssign > curPair) |
93 | 944 | { |
94 | 944 | if ( !quiet ) |
95 | 0 | { |
96 | 0 | ALEMBIC_THROW( "TokenMap::setUnique: malformed string found:" |
97 | 0 | << assignSep << " before: " << pairSep); |
98 | 0 | } |
99 | 944 | return; |
100 | 944 | } |
101 | | |
102 | 357k | if (curAssign != std::string::npos) |
103 | 21.3k | { |
104 | 21.3k | std::size_t endPos = std::string::npos; |
105 | 21.3k | if (curPair != endPos) |
106 | 20.1k | { |
107 | 20.1k | endPos = curPair - curAssign - 1; |
108 | 20.1k | } |
109 | | |
110 | 21.3k | std::string keyStr = config.substr(lastPair, curAssign - lastPair); |
111 | | |
112 | 21.3k | if ( m_map.count( keyStr ) > 0 ) |
113 | 9.97k | { |
114 | 9.97k | if ( !quiet ) |
115 | 0 | { |
116 | 0 | ALEMBIC_THROW( "TokenMap::setUnique: token: " |
117 | 0 | << keyStr << " is not unique." ); |
118 | 0 | } |
119 | 9.97k | } |
120 | 11.3k | else |
121 | 11.3k | { |
122 | 11.3k | m_map[keyStr] = config.substr(curAssign + 1, endPos); |
123 | 11.3k | } |
124 | 21.3k | } |
125 | | |
126 | 357k | if (curPair == std::string::npos) |
127 | 336k | { |
128 | 336k | return; |
129 | 336k | } |
130 | | |
131 | 20.1k | lastPair = curPair + 1; |
132 | 20.1k | } |
133 | 337k | } |
134 | | |
135 | | //-***************************************************************************** |
136 | | std::string TokenMap::get( char pairSep, |
137 | | char assignSep, |
138 | | bool check ) const |
139 | 74 | { |
140 | 74 | char buf[2] = { 0, 0 }; |
141 | | |
142 | 74 | buf[0] = pairSep; |
143 | 74 | std::string pairSepStr( ( const char * )buf ); |
144 | | |
145 | 74 | buf[0] = assignSep; |
146 | 74 | std::string assignSepStr( ( const char * )buf ); |
147 | | |
148 | 74 | std::stringstream output; |
149 | | |
150 | 74 | bool start = true; |
151 | | |
152 | 74 | for ( const_iterator iter = m_map.begin(); |
153 | 367 | iter != m_map.end(); ++iter ) |
154 | 293 | { |
155 | 293 | std::string token = (*iter).first; |
156 | 293 | std::string value = (*iter).second; |
157 | | |
158 | 293 | if ( check && |
159 | 293 | ( token.find( pairSep ) != std::string::npos || |
160 | 293 | token.find( assignSep ) != std::string::npos || |
161 | 293 | value.find( pairSep ) != std::string::npos || |
162 | 293 | value.find( assignSep ) != std::string::npos ) ) |
163 | 0 | { |
164 | 0 | ALEMBIC_THROW( "TokenMap::get: Token-Value pair " << |
165 | 0 | " contains separator characters: " << |
166 | 0 | pairSepStr << " or " << assignSepStr << |
167 | 0 | " for " << token << " or " << value); |
168 | 0 | } |
169 | | |
170 | 293 | if ( value == "" ) |
171 | 74 | { |
172 | 74 | continue; |
173 | 74 | } |
174 | | |
175 | 219 | if ( !start ) |
176 | 147 | { |
177 | 147 | output << pairSepStr; |
178 | 147 | } |
179 | | |
180 | 219 | output << token << assignSepStr << value; |
181 | | |
182 | 219 | start = false; |
183 | 219 | } |
184 | | |
185 | 74 | return output.str(); |
186 | 74 | } |
187 | | |
188 | | } // End namespace ALEMBIC_VERSION_NS |
189 | | } // End namespace Util |
190 | | } // End namespace Alembic |
191 | | |
192 | | |