1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/misc/string_mapper.h"
33 CStringMapper
CStringMapper::s_GlobalMapper
;
35 // ****************************************************************************
36 CStringMapper::CStringMapper()
38 m_EmptyId
= new string();
41 // ****************************************************************************
42 CStringMapper
*CStringMapper::createLocalMapper()
44 return new CStringMapper
;
47 // ****************************************************************************
48 TStringId
CStringMapper::localMap(const std::string
&str
)
53 CAutoFastMutex
autoMutex(&m_Mutex
);
55 TStringTable::const_iterator it
= m_StringTable
.find((const std::string
*)&str
);
57 if (it
== m_StringTable
.end())
59 string
*pStr
= new string(str
);
60 m_StringTable
.insert(pStr
);
63 return (TStringId
)(*it
);
67 // ****************************************************************************
68 TStringId
CStringMapper::localMap(const char *str
)
73 CAutoFastMutex
autoMutex(&m_Mutex
);
75 TStringTable::const_iterator it
= m_StringTable
.find(str
);
77 if (it
== m_StringTable
.end())
79 string
*pStr
= new string(str
);
80 m_StringTable
.insert(pStr
);
83 return (TStringId
)(*it
);
87 // ***************************************************************************
88 void CStringMapper::localSerialString(NLMISC::IStream
&f
, TStringId
&id
)
103 // ****************************************************************************
104 void CStringMapper::localClear()
106 CAutoFastMutex
autoMutex(&m_Mutex
);
108 TStringTable::iterator it
= m_StringTable
.begin();
109 while (it
!= m_StringTable
.end())
111 const std::string
*ptrTmp
= (*it
);
115 m_StringTable
.clear();
118 // ****************************************************************************
119 // CStaticStringMapper
120 // ****************************************************************************
122 // ****************************************************************************
123 TSStringId
CStaticStringMapper::add(const std::string
&str
)
125 nlassert(!_MemoryCompressed
);
126 std::map
<std::string
, TSStringId
>::iterator it
= _TempStringTable
.find(str
);
127 if (it
== _TempStringTable
.end())
129 _TempStringTable
.insert(pair
<string
,TSStringId
>(str
,_IdCounter
));
130 _TempIdTable
.insert(pair
<TSStringId
,string
>(_IdCounter
,str
));
140 // ****************************************************************************
141 bool CStaticStringMapper::isAdded(const std::string
&str
) const
143 nlassert(!_MemoryCompressed
);
144 return _TempStringTable
.count(str
) != 0;
147 // ****************************************************************************
148 void CStaticStringMapper::memoryUncompress()
150 std::map
<std::string
, TSStringId
> tempStringTable
;
151 std::map
<TSStringId
, std::string
> tempIdTable
;
152 for(uint k
= 0; k
< _IdToStr
.size(); ++k
)
154 tempStringTable
[_IdToStr
[k
]] = (TSStringId
) k
;
155 tempIdTable
[(TSStringId
) k
] = _IdToStr
[k
];
157 delete [] _AllStrings
;
160 _TempStringTable
.swap(tempStringTable
);
161 _TempIdTable
.swap(tempIdTable
);
162 _MemoryCompressed
= false;
165 // ****************************************************************************
166 void CStaticStringMapper::memoryCompress()
168 _MemoryCompressed
= true;
169 std::map
<TSStringId
, std::string
>::iterator it
= _TempIdTable
.begin();
172 uint32 nNbStrings
= 0;
173 while (it
!= _TempIdTable
.end())
175 nTotalSize
+= (uint
)it
->second
.size() + 1;
180 _AllStrings
= new char[nTotalSize
];
181 _IdToStr
.resize(nNbStrings
);
184 it
= _TempIdTable
.begin();
185 while (it
!= _TempIdTable
.end())
187 strcpy(_AllStrings
+ nTotalSize
, it
->second
.c_str());
188 _IdToStr
[nNbStrings
] = _AllStrings
+ nTotalSize
;
189 nTotalSize
+= (uint
)it
->second
.size() + 1;
193 contReset(_TempStringTable
);
194 contReset(_TempIdTable
);
197 // ****************************************************************************
198 const char *CStaticStringMapper::get(TSStringId stringId
)
200 if (_MemoryCompressed
)
202 nlassert(stringId
< _IdToStr
.size());
203 return _IdToStr
[stringId
];
207 std::map
<TSStringId
, std::string
>::iterator it
= _TempIdTable
.find(stringId
);
208 if (it
!= _TempIdTable
.end())
209 return it
->second
.c_str();
215 // ****************************************************************************
216 void CStaticStringMapper::clear()
218 contReset(_TempStringTable
);
219 contReset(_TempIdTable
);
220 delete [] _AllStrings
;
225 _MemoryCompressed
= false;
229 // ****************************************************************************
230 void CStaticStringMapper::serial(IStream
&f
, TSStringId
&strId
)
245 // ****************************************************************************
246 void CStaticStringMapper::serial(IStream
&f
, std::vector
<TSStringId
> &strIdVect
)
248 std::vector
<std::string
> vsTmp
;
250 // Serialize class components.
254 strIdVect
.resize(vsTmp
.size());
255 for(uint i
= 0; i
< vsTmp
.size(); ++i
)
256 strIdVect
[i
] = add(vsTmp
[i
]);
260 vsTmp
.resize(strIdVect
.size());
261 for (uint i
= 0; i
< vsTmp
.size(); ++i
)
262 vsTmp
[i
] = get(strIdVect
[i
]);
270 } // namespace NLMISC