Fix tg_termpos1 for 64-bit termpos
[xapian.git] / xapian-applications / omega / utils.cc
blobf4a4d2494a3ca6bcbecdc16fb16e266f69f31196
1 /** @file
2 * @brief string conversion utility functions for omega
3 */
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2003,2004,2006,2010,2011,2019 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <config.h>
25 #include "utils.h"
27 #include <cassert>
28 #include <cstdio> // for snprintf
29 #include <cstdlib>
31 #include <string>
33 using namespace std;
35 int
36 string_to_int(const string &s)
38 return atoi(s.c_str());
41 string
42 double_to_string(double val)
44 // This should be more than enough.
45 const int BUFSIZE = 100;
46 char buf[BUFSIZE];
47 int len = snprintf(buf, BUFSIZE, "%f", val);
48 return string(buf, (len == -1 || len > BUFSIZE) ? BUFSIZE : len);
51 void
52 trim(string & s)
54 string::size_type first_nonspace;
55 first_nonspace = s.find_first_not_of(" \t\r\n\v");
56 if (first_nonspace == string::npos) {
57 // String is all whitespace.
58 s.resize(0);
59 } else {
60 // Remove any trailing whitespace.
61 string::size_type len = s.find_last_not_of(" \t\r\n\v");
62 assert(len != string::npos);
63 if (len < s.size() - 1)
64 s.resize(len + 1);
65 // Remove any leading whitespace.
66 if (first_nonspace > 0)
67 s.erase(0, first_nonspace);