Menu

[5dc849]: / server / utilities.cpp  Maximize  Restore  History

Download this file

151 lines (123 with data), 3.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
/*
Copyright (C) 2012 Grame
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
research@grame.fr
*/
#include <cstdlib> // for rand()
#include <cctype> // for isalnum()
#include <algorithm> // for back_inserter
#include <string>
#include <iostream>
#include <time.h>
#include <string.h>
#include <sstream>
#include "utilities.h"
using namespace std;
namespace guidohttpd
{
logstream * gLog;
_logend gLogEndl;
char
rand_alnum()
{
char c;
while (!std::isalnum(c = static_cast<char>(std::rand())))
;
return c;
}
bool atob(string name)
{
if (name == "true")
return true;
if (name == "True")
return true;
if (name == "yes")
return true;
return false;
}
int atoib(string name)
{
if (name == "true")
return 1;
if (name == "True")
return 1;
if (name == "yes")
return 1;
return 0;
}
int systemsDistributionToFloat(std::string sd)
{
if (sd == "auto")
return kAutoDistrib;
if (sd == "always")
return kAlwaysDistrib;
if (sd == "never")
return kNeverDistrib;
return kAutoDistrib;
}
std::string
rand_alnum_str (std::string::size_type sz)
{
std::string s;
s.reserve (sz);
generate_n (std::back_inserter(s), sz, rand_alnum);
return s;
}
long lopt(char *argv[], const char *name, long def)
{
int i;
for (i = 0; argv[i]; i++) if (!strcmp(argv[i], name)) {
return atoi(argv[i+1]);
}
return def;
}
bool bopt(char *argv[], const char *name, bool def)
{
int i;
for (i = 0; argv[i]; i++) if (!strcmp(argv[i], name)) {
return true;
}
return def;
}
std::string sopt(char *argv[], const char *name, std::string def)
{
int i;
for (i = 0; argv[i]; i++) if (!strcmp(argv[i], name)) {
return std::string (argv[i+1]);
}
return def;
}
//----------------------------------------------------------------------------------------
logstream::logstream (const char * logfile) : fDaemon (true), fPrintDate(true)
{
fFileStream.open(logfile, ios_base::out | ios_base::app);
if (!fFileStream.is_open()) {
cerr << "can't open log file " << logfile << endl;
}
}
string logstream::date()
{
time_t rawtime;
time ( &rawtime );
string datestr (ctime ( &rawtime ));
return datestr.substr(0, datestr.size()-1);
}
void logstream::printdate(std::ostream& out)
{
if (fPrintDate) {
out << "[" << date() << "] ";
fPrintDate = false;
}
}
} // end namespoace
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.