1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    IDSupplier.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Sept 2002
15 /// @version $Id$
16 ///
17 // A class that generates enumerated and prefixed string-ids
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <sstream>
28 #include "StdDefs.h"
29 #include "IDSupplier.h"
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
IDSupplier(const std::string & prefix,long long int begin)35 IDSupplier::IDSupplier(const std::string& prefix, long long int begin)
36     : myCurrent(begin), myPrefix(prefix) {}
37 
38 
39 
IDSupplier(const std::string & prefix,const std::vector<std::string> & knownIDs)40 IDSupplier::IDSupplier(const std::string& prefix, const std::vector<std::string>& knownIDs)
41     : myCurrent(0), myPrefix(prefix) {
42     for (std::vector<std::string>::const_iterator id_it = knownIDs.begin(); id_it != knownIDs.end(); ++id_it) {
43         avoid(*id_it);
44     }
45 }
46 
47 
~IDSupplier()48 IDSupplier::~IDSupplier() {}
49 
50 
51 std::string
getNext()52 IDSupplier::getNext() {
53     std::ostringstream strm;
54     strm << myPrefix << myCurrent++;
55     return strm.str();
56 }
57 
58 
59 void
avoid(const std::string & id)60 IDSupplier::avoid(const std::string& id) {
61     // does it start with prefix?
62     if (id.find(myPrefix) == 0) {
63         long long int number;
64         std::istringstream buf(id.substr(myPrefix.size()));
65         buf >> number;
66         // does it continue with a number?
67         if (!buf.fail()) {
68             myCurrent = MAX2(myCurrent, number + 1);
69         }
70     }
71 }
72 
73 
74 /****************************************************************************/
75 
76