Parent: [d49d2e] (diff)

Child: [176b9b] (diff)

Download this file

smallut.h    157 lines (136 with data), 5.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _SMALLUT_H_INCLUDED_
#define _SMALLUT_H_INCLUDED_
/* @(#$Id: smallut.h,v 1.33 2008-12-05 07:38:07 dockes Exp $ (C) 2004 J.F.Dockes */
#include <stdlib.h>
#include <string>
#include <list>
#include <vector>
#include <map>
#include <set>
#ifndef NO_NAMESPACES
using std::string;
using std::list;
using std::vector;
using std::map;
using std::set;
#endif /* NO_NAMESPACES */
// Note these are all ascii routines
extern int stringicmp(const string& s1, const string& s2);
extern int stringlowercmp(const string& alreadylower, const string& s2);
extern int stringuppercmp(const string& alreadyupper, const string& s2);
extern void stringtolower(string& io);
extern string stringtolower(const string& io);
// Is one string the end part of the other ?
extern int stringisuffcmp(const string& s1, const string& s2);
// Compare charset names, removing the more common spelling variations
extern bool samecharset(const string &cs1, const string &cs2);
/**
* Parse input string into list of strings.
*
* Token delimiter is " \t\n" except inside dquotes. dquote inside
* dquotes can be escaped with \ etc...
* Input is handled a byte at a time, things will work as long as space tab etc.
* have the ascii values and can't appear as part of a multibyte char. utf-8 ok
* but so are the iso-8859-x and surely others.
*/
extern bool stringToStrings(const string &s, list<string> &tokens);
extern bool stringToStrings(const string &s, vector<string> &tokens);
extern bool stringToStrings(const string &s, set<string> &tokens);
/**
* Inverse operation:
*/
extern void stringsToString(const list<string> &tokens, string &s);
extern void stringsToString(const vector<string> &tokens, string &s);
extern void stringsToString(const set<string> &tokens, string &s);
/**
* Split input string. No handling of quoting
*/
extern void stringToTokens(const string &s, list<string> &tokens,
const string &delims = " \t", bool skipinit=true);
/** Convert string to boolean */
extern bool stringToBool(const string &s);
/** Remove instances of characters belonging to set (default {space,
tab}) at beginning and end of input string */
extern void trimstring(string &s, const char *ws = " \t");
/** Escape things like < or & by turining them to entities */
extern string escapeHtml(const string &in);
/** Replace some chars with spaces (ie: newline chars). This is not utf8-aware
* so chars should only contain ascii */
extern string neutchars(const string &str, const string &chars);
extern void neutchars(const string &str, string& out, const string &chars);
/** turn string into something that won't be expanded by a shell. In practise
* quote with single-quotes and escape internal singlequotes */
extern string escapeShell(const string &str);
/** Truncate a string to a given maxlength, avoiding cutting off midword
* if reasonably possible. */
extern string truncate_to_word(const string &input, string::size_type maxlen);
/** Truncate in place in an utf8-legal way */
extern void utf8truncate(string &s, int maxlen);
/** Convert byte count into unit (KB/MB...) appropriate for display */
string displayableBytes(long size);
/** Break big string into lines */
string breakIntoLines(const string& in, unsigned int ll = 100,
unsigned int maxlines= 50);
/** Small utility to substitute printf-like percents cmds in a string */
bool pcSubst(const string& in, string& out, map<char, string>& subs);
/** Substitute printf-like percents and also %(key) */
bool pcSubst(const string& in, string& out, map<string, string>& subs);
class Chrono {
public:
Chrono();
/** Reset origin */
long restart();
/** Snapshot current time */
static void refnow();
/** Get current elapsed since creation or restart
*
* @param frozen give time since the last refnow call (this is to
* allow for using one actual system call to get values from many
* chrono objects, like when examining timeouts in a queue)
*/
long millis(int frozen = 0);
long ms() {return millis();}
long micros(int frozen = 0);
float secs(int frozen = 0);
private:
long m_secs;
long m_nsecs;
};
class TempBuf {
public:
TempBuf()
: m_buf(0)
{}
TempBuf(int n)
{
m_buf = (char *)malloc(n);
}
~TempBuf()
{
if (m_buf)
free(m_buf);
}
char *setsize(int n) { return (m_buf = (char *)realloc(m_buf, n)); }
char *buf() {return m_buf;}
char *m_buf;
};
#ifndef deleteZ
#define deleteZ(X) {delete X;X = 0;}
#endif
#endif /* _SMALLUT_H_INCLUDED_ */