Coverage Report

Created: 2026-05-30 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/alembic/lib/Alembic/Util/TokenMap.cpp
Line
Count
Source
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
441k
{
86
441k
    std::size_t lastPair = 0;
87
480k
    while(1)
88
480k
    {
89
480k
        std::size_t curPair = config.find(pairSep, lastPair);
90
480k
        std::size_t curAssign = config.find(assignSep, lastPair);
91
92
480k
        if (curAssign > curPair)
93
1.59k
        {
94
1.59k
            if ( !quiet )
95
0
            {
96
0
                ALEMBIC_THROW( "TokenMap::setUnique: malformed string found:"
97
0
                    << assignSep << " before: " << pairSep);
98
0
            }
99
1.59k
            return;
100
1.59k
        }
101
102
478k
        if (curAssign != std::string::npos)
103
40.9k
        {
104
40.9k
            std::size_t endPos = std::string::npos;
105
40.9k
            if (curPair != endPos)
106
38.8k
            {
107
38.8k
                endPos = curPair - curAssign - 1;
108
38.8k
            }
109
110
40.9k
            std::string keyStr = config.substr(lastPair, curAssign - lastPair);
111
112
40.9k
            if ( m_map.count( keyStr ) > 0 )
113
16.5k
            {
114
16.5k
                if ( !quiet )
115
0
                {
116
0
                    ALEMBIC_THROW( "TokenMap::setUnique: token: "
117
0
                                   << keyStr << " is not unique." );
118
0
                }
119
16.5k
            }
120
24.4k
            else
121
24.4k
            {
122
24.4k
                m_map[keyStr] = config.substr(curAssign + 1, endPos);
123
24.4k
            }
124
40.9k
        }
125
126
478k
        if (curPair == std::string::npos)
127
439k
        {
128
439k
            return;
129
439k
        }
130
131
38.8k
        lastPair = curPair + 1;
132
38.8k
    }
133
441k
}
134
135
//-*****************************************************************************
136
std::string TokenMap::get( char pairSep,
137
                           char assignSep,
138
                           bool check ) const
139
56
{
140
56
    char buf[2] = { 0, 0 };
141
142
56
    buf[0] = pairSep;
143
56
    std::string pairSepStr( ( const char * )buf );
144
145
56
    buf[0] = assignSep;
146
56
    std::string assignSepStr( ( const char * )buf );
147
148
56
    std::stringstream output;
149
150
56
    bool start = true;
151
152
56
    for ( const_iterator iter = m_map.begin();
153
419
          iter != m_map.end(); ++iter )
154
363
    {
155
363
        std::string token = (*iter).first;
156
363
        std::string value = (*iter).second;
157
158
363
        if ( check &&
159
363
             ( token.find( pairSep ) != std::string::npos ||
160
363
               token.find( assignSep ) != std::string::npos ||
161
363
               value.find( pairSep ) != std::string::npos ||
162
363
               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
363
        if ( value == "" )
171
154
        {
172
154
            continue;
173
154
        }
174
175
209
        if ( !start )
176
158
        {
177
158
            output << pairSepStr;
178
158
        }
179
180
209
        output << token << assignSepStr << value;
181
182
209
        start = false;
183
209
    }
184
185
56
    return output.str();
186
56
}
187
188
} // End namespace ALEMBIC_VERSION_NS
189
} // End namespace Util
190
} // End namespace Alembic
191
192