~ubuntu-branches/ubuntu/intrepid/enigma/intrepid

« back to all changes in this revision

Viewing changes to src/px/tools.cc

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2005-08-28 15:30:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050828153009-sky64kb6tcq37xt5
Tags: 0.92.1-1
* New upstream subversion checkout
* Remove menu.s3m, which we are allowed to distributed but not to modify
  also copyright notice is confusing... (Closes: #321669)
* Rebuild with new libzipios (Closes: #325405)
  I hope this works without a versioned build-dependency
* Added "enigma replaces enigma-data" for upgrades (Closes: #308558)
* Added notes about the fonts copyright.
* updated to policy 3.6.2.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002,2003 Ralf Westram
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
16
 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
 
 *
18
 
 * $Id: tools.cc,v 1.4 2003/10/30 19:52:31 reallysoft Exp $
19
 
 */
20
 
 
21
 
#include "tools.hh"
22
 
#include "system.hh"
23
 
 
24
 
#include <cstdarg>
25
 
#include <cstdio>
26
 
#include <cassert>
27
 
 
28
 
using namespace std;
29
 
using namespace px;
30
 
 
31
 
string px::concat_paths (const string& path, const string& filename)
32
 
{
33
 
    // concatenate path and filename (or relative subpath)
34
 
    return
35
 
        path.substr(0, path.find_last_not_of (PathSeparator)+1) +
36
 
        PathSeparator +
37
 
        filename.substr(filename.find_first_not_of (PathSeparator));
38
 
}
39
 
 
40
 
bool px::split_path (const string& path, string* dir_part, string* filename_part)
41
 
{
42
 
    size_t lslash = path.find_last_of (PathSeparator);
43
 
    if (lslash == path.length()-1) // trailing slash
44
 
        return split_path(path.substr(0, lslash), dir_part, filename_part);
45
 
 
46
 
    if (lslash == string::npos)
47
 
        return false;
48
 
 
49
 
    size_t lnslash = path.find_last_not_of (PathSeparator, lslash);
50
 
 
51
 
    if (dir_part) *dir_part           = path.substr(0, lnslash+1);
52
 
    if (filename_part) *filename_part = path.substr(lslash+1);
53
 
 
54
 
    return true;
55
 
}
56
 
 
57
 
namespace {
58
 
    string vstrf(const char *format, va_list argPtr) {
59
 
        static size_t  buf_size = 128;
60
 
        static char   *buffer   = new char[buf_size];
61
 
 
62
 
        size_t length;
63
 
        while (1) {
64
 
            if (!buffer) {
65
 
                assert(buffer); // to stop when debugging
66
 
                return "<alloc problem>";
67
 
            }
68
 
 
69
 
            length = vsnprintf(buffer, buf_size, format, argPtr);
70
 
            if (length < buf_size) break; // string fits into current buffer
71
 
 
72
 
            // otherwise resize buffer :
73
 
            buf_size += buf_size/2;
74
 
            // fprintf(stderr, "Reallocating vstrf-buffer to size=%u\n", buf_size);
75
 
            delete [] buffer;
76
 
            buffer    = new char[buf_size];
77
 
        }
78
 
 
79
 
        return string(buffer, length);
80
 
    }
81
 
}
82
 
 
83
 
string px::strf(const char *format, ...)
84
 
{
85
 
    va_list argPtr;
86
 
    va_start(argPtr, format);
87
 
    string result = vstrf(format, argPtr);
88
 
    va_end(argPtr);
89
 
 
90
 
    return result;
91
 
}
92