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
|
/*
* Copyright (C) 2002,2003 Ralf Westram
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "ecl_util.hh"
#include "ecl_system.hh"
#include <cstdarg>
#include <cstdio>
#include <cassert>
using namespace std;
using namespace ecl;
string ecl::concat_paths (const string& path, const string& filename)
{
// concatenate path and filename (or relative subpath)
return
path.substr(0, path.find_last_not_of (PathSeparator)+1) +
PathSeparator +
filename.substr(filename.find_first_not_of (PathSeparator));
}
bool ecl::split_path (const string& path, string* dir_part, string* filename_part)
{
size_t lslash = path.find_last_of (PathSeparators);
if (
#ifdef __MINGW32__
lslash == 2 && path[1] == ':' && (path[0] >= 'A' && path[0] <= 'Z')
#else
lslash == 0
#endif
) {
return false; // we cannot split the root directory apart
}
if (lslash == path.length()-1) // trailing slash
return split_path(path.substr(0, lslash), dir_part, filename_part);
if (lslash == string::npos)
return false;
size_t lnslash = path.find_last_not_of (PathSeparators, lslash);
if (dir_part) *dir_part = path.substr(0, lnslash+1);
if (filename_part) *filename_part = path.substr(lslash+1);
return true;
}
std::string ecl::strf(const char *format, ...) {
static size_t buf_size = 512;
static char *buffer = new char[buf_size];
va_list argPtr;
size_t length;
while (true) {
if (!buffer) {
assert(buffer); // to stop when debugging
return "<alloc problem>";
}
va_start(argPtr, format);
length = vsnprintf(buffer, buf_size, format, argPtr);
va_end(argPtr);
if (length < buf_size - 1)
// string fits into current buffer
return std::string(buffer, length);
// otherwise resize buffer :
buf_size *= 2;
// fprintf(stderr, "Reallocating vstrf-buffer to size=%u\n", buf_size);
delete [] buffer;
buffer = new char[buf_size];
}
}
std::string ecl::timeformat(int duration) {
int hours = duration / 3600;
int minutes = (duration - 3600 * hours) / 60;
int seconds = duration % 60;
if (hours > 0)
return ecl::strf("%d:%02d'%02d\"", hours, minutes, seconds);
else
return ecl::strf("%d'%02d\"", minutes, seconds);
}
// string_match accepts simple wildcards
// '?' means 'any character'
// '*' means '0 or more characters'
bool ecl::string_match(const char *str, const char *templ) {
while (true) {
char t = *templ++;
char s = *str++;
if (t == s) {
if (!t) return true;
continue;
}
else { // mismatch
if (t == '?') continue;
if (t != '*') break;
t = *templ++;
if (!t) return true; // '*' at EOS
while (1) {
if (!s) break;
if (s == t) {
if (string_match(str, templ))
return true;
}
s = *str++;
}
}
}
return false;
}
bool ecl::string_match(std::string str, std::string templ) {
return string_match(str.c_str(), templ.c_str());
}
|