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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: CommonUtils.cpp,v 1.10 2006/07/20 02:30:53 vlg Exp $
//------------------------------------------------------------------------------
// CommonUtils.cpp
//------------------------------------------------------------------------------
// Copyright (C) 1997-2003 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h> // getcwd(3)
#ifdef WIN32
static char *home_dir = "."; /* we feel (no|every)where at home */
#else
# include <pwd.h>
#endif
#include "assa/Logger.h"
#include "assa/CommonUtils.h"
void
ASSA::Utils::
split (const char* src_, std::vector<std::string>& vec_)
{
std::istringstream input (src_);
vec_.erase (vec_.begin (), vec_.end ());
std::string token;
while (input >> token) {
vec_.push_back (token);
}
}
int
ASSA::Utils::
split_pair (const string& text_, char sep_, string& lhs_, string& rhs_)
{
int pos = 0;
if ((pos = text_.find (sep_)) == string::npos) {
return -1;
}
lhs_ = text_.substr (0, pos);
rhs_ = text_.substr (pos+1, text_.size ());
pos = rhs_.size () -1;
if (rhs_[0] == '"' || rhs_[0] == '\'') {
rhs_[0] = ' ';
}
if (rhs_[pos] == '"' || rhs_[pos] == '\'') {
rhs_[pos] = ' ';
}
return 0;
}
int
ASSA::Utils::
ltrim (std::string& text_, const std::string& delim_)
{
std::string::size_type idx;
idx = text_.find_first_of (delim_);
if (idx != std::string::npos) {
text_.replace (0, idx+1, "");
return 0;
}
return -1;
}
int
ASSA::Utils::
rtrim (std::string& text_, const std::string& delim_)
{
std::string::size_type idx;
idx = text_.find_last_of (delim_);
if (idx != std::string::npos) {
text_.replace (idx, text_.size (), "");
return 0;
}
return -1;
}
void
ASSA::Utils::
trim_sides (std::string& text_)
{
std::string::size_type idx;
idx = text_.find_first_not_of (" \t");
if (idx != std::string::npos) {
text_.replace (0, idx, "");
}
idx = text_.find_last_not_of (" \t");
if (idx != std::string::npos) {
text_.replace (idx + 1, text_.size (), "");
}
}
void
ASSA::Utils::
find_and_replace_char (std::string& text_, char src_, char dest_)
{
string::iterator pos = text_.begin ();
while (pos != text_.end ()) {
if ((*pos) == src_) {
(*pos) = dest_;
}
pos++;
}
}
std::string
ASSA::Utils::
strenv (const char* in)
{
char b [1024];
char* ret = b;
char* r = ret;
if (*in == '~') { // '~' OR '~/'
if ( *(in+1) == 0 || *(in+1) == '/' ) {
in++;
strcpy (ret, getenv ("HOME") ? getenv ("HOME") : "");
r += strlen (ret);
}
else {
in++;
char lname [256];
char* lp = lname;
const char* sp = strchr (in, '/'); // find first '/' in string
if ( sp ) {
while (in != sp) *lp++ = *in++;
*lp = 0;
}
else {
while (*in) *lp++ = *in++;
*lp = 0;
}
#ifdef WIN32
strcpy (ret, home_dir);
r += strlen (ret);
#else
// lookup user's home directory in /etc/passwd file
struct passwd* p = getpwnam (lname);
if ( p ) {
strcpy (ret, p->pw_dir ? p->pw_dir : "");
r += strlen (ret);
}
#endif
}
}
while (*in) {
if (*in == '$') {
char varname [80];
if (*++in == '(') {
++in;
const char *end = strchr (in,')');
if (!end)
break;
strncpy (varname, in, end-in);
varname [end-in] = '\0';
in = end+1;
}
else if (*in == '{') {
const char *end = strchr (in,'}');
if (!end)
break;
strncpy (varname, in, end-in);
varname [end-in] = '\0';
in = end+1;
}
else {
char* vp = varname;
while (isalnum (*in) || *in == '_' ) { // letter OR digit
*vp++ = *in++;
}
*vp = '\0';
}
char* ep = ::getenv (varname);
while (ep && *ep) *r++ = *ep++;
continue;
}
else if (*in == '\\' && *(in+1)) {
in++; // allow escaped dollar signs
}
*r++ = *in++;
}
*r = '\0';
return ret;
}
std::string
ASSA::Utils::
get_cwd_name (void)
{
std::string ret;
int size = 256;
char* chr_ptr = 0;
while (true) {
chr_ptr = new char [size];
if (::getcwd (chr_ptr, size-1) != NULL) {
ret = chr_ptr;
delete [] chr_ptr;
return ret;
}
if (errno != ERANGE) {
return ret; // Any error other then a path name too long
// for the buffer is bad news.
}
delete [] chr_ptr;
size += 256;
}
}
|