2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 * Implements new and legacy symbol table routines.
42 * \author David van der Spoel <david.vanderspoel@icm.uu.se>
43 * \author Paul Bauer <paul.bauer.q@gmail.com>
44 * \ingroup module_topology
55 #include "gromacs/utility/basedefinitions.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/iserializer.h"
60 #include "gromacs/utility/smalloc.h"
61 #include "gromacs/utility/stringutil.h"
62 #include "gromacs/utility/txtdump.h"
64 StringTableEntry StringTableBuilder::addString(const std::string& theString)
66 int size = map_.size();
67 std::string stripped = gmx::stripString(theString);
69 const auto foundEntry = map_.insert(StringTablePair(stripped, size));
70 return StringTableEntry(foundEntry.first->first, foundEntry.first->second);
73 int StringTableBuilder::findEntryByName(const std::string& name) const
75 auto foundEntry = map_.find(name);
76 if (foundEntry != map_.end())
78 return foundEntry->second;
82 GMX_THROW(gmx::InternalError(
83 gmx::formatString("Could not find string \"%s\" in SymbolTable", name.c_str())));
87 StringTable StringTableBuilder::build()
89 std::vector<std::string> table(map_.size());
90 for (const auto& entry : map_)
92 table[entry.second] = entry.first;
95 return StringTable(table);
98 void StringTable::printStringTableStorageToFile(FILE* fp, int indent, const char* title) const
100 indent = pr_title_n(fp, indent, title, table_.size());
102 for (const auto& entry : table_)
104 pr_indent(fp, indent);
105 fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, entry.c_str());
109 StringTable::StringTable(gmx::ISerializer* serializer)
111 GMX_RELEASE_ASSERT(serializer->reading(), "Can not use writing serializer to read string table");
113 serializer->doInt(&nr);
115 for (auto& entry : table_)
117 serializer->doString(&entry);
121 void StringTable::serializeStringTable(gmx::ISerializer* serializer)
123 GMX_RELEASE_ASSERT(!serializer->reading(),
124 "Can not use reading serializer to write string table");
125 int nr = table_.size();
126 serializer->doInt(&nr);
127 for (auto& entry : table_)
129 serializer->doString(&entry);
133 StringTableEntry StringTable::at(gmx::index index) const
135 if (index >= gmx::ssize(table_))
137 GMX_THROW(gmx::InternalError("Can't read beyond last entry"));
139 return StringTableEntry(table_[index], index);
142 StringTableEntry StringTable::operator[](gmx::index index) const
144 GMX_ASSERT(index < gmx::ssize(table_), "Can't read beyond last entry");
145 return StringTableEntry(table_[index], index);
148 void StringTableEntry::serialize(gmx::ISerializer* serializer) const
150 GMX_RELEASE_ASSERT(!serializer->reading(),
151 "Can not use reading serializer to write string index");
152 int entry = tableIndex_;
153 serializer->doInt(&entry);
156 StringTableEntry readStringTableEntry(gmx::ISerializer* serializer, const StringTable& table)
158 GMX_RELEASE_ASSERT(serializer->reading(), "Can not use writing serializer to read string index");
160 serializer->doInt(&entry);
161 return table.at(entry);
164 void StringTable::copyToLegacySymtab(struct t_symtab* symtab) const
166 for (const auto& entry : table_)
168 put_symtab(symtab, entry.c_str());
172 // Old code for legacy data structure starts below.
173 //! Maximum size of character string in table.
174 constexpr int c_trimSize = 1024;
175 //! Maximum number of entries in each element of the linked list.
176 constexpr int c_maxBufSize = 5;
179 * Remove leading and trailing whitespace from string and enforce maximum length.
181 * \param[in] s String to trim.
182 * \param[inout] out String to return.
183 * \param[in] maxlen Maximum string length to use.
184 * \returns New pruned string.
186 static char* trim_string(const char* s, char* out, int maxlen)
190 if (strlen(s) > static_cast<size_t>(maxlen - 1))
192 gmx_fatal(FARGS, "String '%s' (%zu) is longer than buffer (%d).\n", s, strlen(s), maxlen - 1);
195 for (; (*s) == ' '; s++) {}
196 for (len = strlen(s); (len > 0); len--)
198 if (s[len - 1] != ' ')
203 if (len >= c_trimSize)
205 len = c_trimSize - 1;
207 for (i = 0; i < len; i++)
215 int lookup_symtab(t_symtab* symtab, char** name)
221 symbuf = symtab->symbuf;
222 while (symbuf != nullptr)
224 const int index = name - symbuf->buf;
225 if ((index >= 0) && (index < symbuf->bufsize))
231 base += symbuf->bufsize;
232 symbuf = symbuf->next;
235 gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
238 char** get_symtab_handle(t_symtab* symtab, int name)
242 symbuf = symtab->symbuf;
243 while (symbuf != nullptr)
245 if (name < symbuf->bufsize)
247 return &(symbuf->buf[name]);
251 name -= symbuf->bufsize;
252 symbuf = symbuf->next;
255 gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
258 //! Returns a new initialized entry into the symtab linked list.
259 static t_symbuf* new_symbuf()
264 symbuf->bufsize = c_maxBufSize;
265 snew(symbuf->buf, symbuf->bufsize);
266 symbuf->next = nullptr;
272 * Low level function to enter new string into legacy symtab.
274 * \param[inout] symtab Symbol table to add entry to.
275 * \param[in] name New string to add to symtab.
276 * \returns Pointer to new entry in the legacy symbol table, or to existing entry if it already existed.
278 static char** enter_buf(t_symtab* symtab, char* name)
284 if (symtab->symbuf == nullptr)
286 symtab->symbuf = new_symbuf();
289 symbuf = symtab->symbuf;
292 for (i = 0; (i < symbuf->bufsize); i++)
294 if (symbuf->buf[i] == nullptr)
297 symbuf->buf[i] = gmx_strdup(name);
298 return &(symbuf->buf[i]);
300 else if (strcmp(symbuf->buf[i], name) == 0)
302 return &(symbuf->buf[i]);
305 if (symbuf->next != nullptr)
307 symbuf = symbuf->next;
316 symbuf->next = new_symbuf();
317 symbuf = symbuf->next;
320 symbuf->buf[0] = gmx_strdup(name);
321 return &(symbuf->buf[0]);
324 char** put_symtab(t_symtab* symtab, const char* name)
328 return enter_buf(symtab, trim_string(name, buf, 1023));
331 void open_symtab(t_symtab* symtab)
334 symtab->symbuf = nullptr;
337 void close_symtab(t_symtab gmx_unused* symtab) {}
339 // TODO this will go away when we use a
340 // std::list<std::vector<std::string>>> for t_symtab.
341 t_symtab* duplicateSymtab(const t_symtab* symtab)
343 t_symtab* copySymtab;
345 open_symtab(copySymtab);
346 t_symbuf* symbuf = symtab->symbuf;
347 if (symbuf != nullptr)
349 snew(copySymtab->symbuf, 1);
351 t_symbuf* copySymbuf = copySymtab->symbuf;
352 while (symbuf != nullptr)
354 snew(copySymbuf->buf, symbuf->bufsize);
355 copySymbuf->bufsize = symbuf->bufsize;
356 for (int i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
360 copySymbuf->buf[i] = gmx_strdup(symbuf->buf[i]);
363 symbuf = symbuf->next;
364 if (symbuf != nullptr)
366 snew(copySymbuf->next, 1);
367 copySymbuf = copySymbuf->next;
370 copySymtab->nr = symtab->nr;
374 void done_symtab(t_symtab* symtab)
377 t_symbuf *symbuf, *freeptr;
379 close_symtab(symtab);
380 symbuf = symtab->symbuf;
381 while (symbuf != nullptr)
383 for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
385 sfree(symbuf->buf[i]);
390 symbuf = symbuf->next;
393 symtab->symbuf = nullptr;
396 gmx_incons("Freeing symbol table (symtab) structure");
400 void free_symtab(t_symtab* symtab)
402 t_symbuf *symbuf, *freeptr;
404 close_symtab(symtab);
405 symbuf = symtab->symbuf;
406 while (symbuf != nullptr)
408 symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
410 symbuf = symbuf->next;
413 symtab->symbuf = nullptr;
416 gmx_incons("Freeing symbol table (symtab) structure");
420 void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab)
425 if (available(fp, symtab, indent, title))
427 indent = pr_title_n(fp, indent, title, symtab->nr);
430 symbuf = symtab->symbuf;
431 while (symbuf != nullptr)
433 for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
435 pr_indent(fp, indent);
436 (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
439 symbuf = symbuf->next;
443 gmx_incons("Printing symbol table (symtab) structure");