sync with common

This commit is contained in:
Jean-Francois Dockes 2016-11-08 12:41:29 +01:00
parent ee58aabdbe
commit c03bd1b91a
2 changed files with 92 additions and 16 deletions

View file

@ -14,12 +14,6 @@
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#else
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -523,15 +517,12 @@ string escapeHtml(const string& in)
{
string out;
for (string::size_type pos = 0; pos < in.length(); pos++) {
switch (in.at(pos)) {
case '<':
out += "&lt;";
break;
case '&':
out += "&amp;";
break;
default:
out += in.at(pos);
switch(in.at(pos)) {
case '<': out += "&lt;"; break;
case '>': out += "&gt;"; break;
case '&': out += "&amp;"; break;
case '"': out += "&quot;"; break;
default: out += in.at(pos); break;
}
}
return out;
@ -1323,6 +1314,66 @@ bool SimpleRegexp::operator() (const string& val) const
return simpleMatch(val);
}
string flagsToString(const vector<CharFlags>& flags, unsigned int val)
{
const char *s;
string out;
for (auto& flag : flags) {
if ((val & flag.value) == flag.value) {
s = flag.yesname;
} else {
s = flag.noname;
}
if (s && *s) {
/* We have something to write */
if (out.length()) {
// If not first, add '|' separator
out.append("|");
}
out.append(s);
}
}
return out;
}
string valToString(const vector<CharFlags>& flags, unsigned int val)
{
string out;
for (auto& flag : flags) {
if (flag.value == val) {
out = flag.yesname;
return out;
}
}
{
char mybuf[100];
sprintf(mybuf, "Unknown Value 0x%x", val);
out = mybuf;
}
return out;
}
unsigned int stringToFlags(const vector<CharFlags>& flags,
const string& input, const char *sep)
{
unsigned int out = 0;
vector<string> toks;
stringToTokens(input, toks, sep);
for (auto& tok: toks) {
trimstring(tok);
for (auto& flag : flags) {
if (!tok.compare(flag.yesname)) {
/* Note: we don't break: the same name could conceivably
set several flags. */
out |= flag.value;
}
}
}
return out;
}
// Initialization for static stuff to be called from main thread before going
// multiple
void smallut_init_mt()

View file

@ -218,4 +218,29 @@ private:
Internal *m;
};
/// Utilities for printing names for defined values (Ex: O_RDONLY->"O_RDONLY")
/// Entries for the descriptive table
struct CharFlags {
unsigned int value; // Flag or value
const char *yesname;// String to print if flag set or equal
const char *noname; // String to print if flag not set (unused for values)
};
/// Helper macro for the common case where we want to print the
/// flag/value defined name
#define CHARFLAGENTRY(NM) {NM, #NM}
/// Translate a bitfield into string description
extern std::string flagsToString(const std::vector<CharFlags>&,
unsigned int flags);
/// Translate a value into a name
extern std::string valToString(const std::vector<CharFlags>&, unsigned int val);
/// Reverse operation: translate string into bitfield
extern unsigned int
stringToFlags(const std::vector<CharFlags>&, const std::string& input,
const char *sep = "|");
#endif /* _SMALLUT_H_INCLUDED_ */