enigma-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Enigma-cvs] enigma/src/px tools.cc,NONE,1.1 tools.hh,1.10,1.11


From: Ralf Westram <address@hidden>
Subject: [Enigma-cvs] enigma/src/px tools.cc,NONE,1.1 tools.hh,1.10,1.11
Date: Mon, 27 Oct 2003 11:45:03 +0000

Update of /cvsroot/enigma/enigma/src/px
In directory subversions:/tmp/cvs-serv11601/src/px

Modified Files:
        tools.hh 
Added Files:
        tools.cc 
Log Message:
- added concat_paths, split_path, strf



--- NEW FILE: tools.cc ---
/*
 * 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.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * $Id: tools.cc,v 1.1 2003/10/27 11:45:00 reallysoft Exp $
 */

#include "tools.hh"
#include "system.hh"

#include <cstdarg>

using namespace std;
using namespace px;
using px::sysdep::path_separator;

string
px::concat_paths(const string& path, const string& filename)
{
    // concatenate path and filename (or relative subpath)
    return
        path.substr(0, path.find_last_not_of(path_separator)+1) +
        path_separator +
        filename.substr(filename.find_first_not_of(path_separator));
}

bool
px::split_path(const string& path, string* dir_part, string* filename_part)
{
    size_t lslash = path.find_last_of(path_separator);
    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(path_separator, lslash);

    if (dir_part) *dir_part           = path.substr(0, lnslash+1);
    if (filename_part) *filename_part = path.substr(lslash+1);

    return true;
}

namespace {
    string vstrf(const char *format, va_list argPtr) {
        static size_t  buf_size = 128;
        static char   *buffer   = new char[buf_size];

        size_t length;
        while (1) {
            if (!buffer) {
                assert(buffer); // to stop when debugging
                throw std::bad_alloc();
            }

            length = vsnprintf(buffer, buf_size, format, argPtr);
            if (length < buf_size) break; // string fits into current buffer

            // otherwise resize buffer :
            buf_size += buf_size/2;
            // fprintf(stderr, "Reallocating vstrf-buffer to size=%u\n", 
buf_size);
            delete [] buffer;
            buffer    = new char[buf_size];
        }

        return string(buffer, length);
    }
}

string
px::strf(const char *format, ...)
{
    va_list argPtr;
    va_start(argPtr, format);
    string result = vstrf(format, argPtr);
    va_end(argPtr);

    return result;
}


Index: tools.hh
===================================================================
RCS file: /cvsroot/enigma/enigma/src/px/tools.hh,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** tools.hh    17 Jul 2003 20:12:11 -0000      1.10
--- tools.hh    27 Oct 2003 11:45:00 -0000      1.11
***************
*** 23,26 ****
--- 23,34 ----
  #include <string>
  
+ /* hide GNU extensions for non-gnu compilers: */
+ #ifndef __GNU__
+ # ifndef __attribute__
+ // #  define __attribute__(x)
+ // # error wuah
+ # endif
+ #endif
+ 
  namespace px
  {
***************
*** 241,244 ****
--- 249,261 ----
          return (static_cast<int>(val & flags) == static_cast<int>(flags));
      }
+ 
+     /*
+     ** Some helper functions for strings and filenames
+     */
+ 
+     std::string strf(const char *format, ...) __attribute__((format(printf, 
1, 2)));
+ 
+     std::string concat_paths(const std::string& path, const std::string& 
filename);
+     bool        split_path(const std::string& path, std::string* dir_part, 
std::string* filename_part);
  
  }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]