Moved everything from Lib to simgear
This commit is contained in:
16
lib/misc/CREDITS
Normal file
16
lib/misc/CREDITS
Normal file
@@ -0,0 +1,16 @@
|
||||
The following files were unashamedly borrowed from other projects:
|
||||
|
||||
zfstream.hxx
|
||||
zfstream.cxx
|
||||
zlib/contrib/iostream
|
||||
|
||||
stopwatch.hxx was (blitz/time.h)
|
||||
blitz
|
||||
|
||||
Some modifications have been made to fit in with the FlightGear scheme of things.
|
||||
|
||||
As far as I'm aware they are all covered by GNU's licensing agreements.
|
||||
|
||||
Many thanks to their respective authors.
|
||||
|
||||
Bernie Bright (bbright@c031.aone.net.au)
|
||||
12
lib/misc/Makefile.am
Normal file
12
lib/misc/Makefile.am
Normal file
@@ -0,0 +1,12 @@
|
||||
## Process this file with automake to produce Makefile.in
|
||||
|
||||
noinst_LIBRARIES = libMisc.a
|
||||
|
||||
libMisc_a_SOURCES = \
|
||||
fgpath.cxx fgpath.hxx \
|
||||
fgstream.cxx fgstream.hxx \
|
||||
stopwatch.hxx \
|
||||
strutils.cxx strutils.hxx \
|
||||
zfstream.cxx zfstream.hxx
|
||||
|
||||
INCLUDES += -I$(top_builddir) -I$(top_builddir)/Lib
|
||||
101
lib/misc/fgpath.cxx
Normal file
101
lib/misc/fgpath.cxx
Normal file
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// fgpath.cxx -- routines to abstract out path separator differences
|
||||
// between MacOS and the rest of the world
|
||||
//
|
||||
// Written by Curtis L. Olson, started April 1999.
|
||||
//
|
||||
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
|
||||
#include "fgpath.hxx"
|
||||
|
||||
|
||||
// If Unix, replace all ":" with "/". If MacOS, replace all "/" with
|
||||
// ":" it should go without saying that neither of these characters
|
||||
// should be used in file or directory names. In windoze, allow the
|
||||
// second character to be a ":" for things like c:\foo\bar
|
||||
|
||||
static string fix_path( const string path ) {
|
||||
string result = path;
|
||||
|
||||
for ( int i = 0; i < (int)path.size(); ++i ) {
|
||||
#if defined( WIN32 )
|
||||
// for windoze, don't replace the ":" for the second character
|
||||
if ( i == 1 ) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if ( result[i] == FG_BAD_PATH_SEP ) {
|
||||
result[i] = FG_PATH_SEP;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// default constructor
|
||||
FGPath::FGPath() {
|
||||
path = "";
|
||||
}
|
||||
|
||||
|
||||
// create a path based on "path"
|
||||
FGPath::FGPath( const string p ) {
|
||||
set( p );
|
||||
}
|
||||
|
||||
|
||||
// destructor
|
||||
FGPath::~FGPath() {
|
||||
}
|
||||
|
||||
|
||||
// set path
|
||||
void FGPath::set( const string p ) {
|
||||
path = fix_path( p );
|
||||
}
|
||||
|
||||
|
||||
// append another piece to the existing path
|
||||
void FGPath::append( const string p ) {
|
||||
string part = fix_path( p );
|
||||
|
||||
if ( path.size() == 0 ) {
|
||||
path = part;
|
||||
} else {
|
||||
if ( part[0] != FG_PATH_SEP ) {
|
||||
path += FG_PATH_SEP;
|
||||
}
|
||||
path += part;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// concatenate a string to the end of the path without inserting a
|
||||
// path separator
|
||||
void FGPath::concat( const string p ) {
|
||||
string part = fix_path( p );
|
||||
|
||||
if ( path.size() == 0 ) {
|
||||
path = part;
|
||||
} else {
|
||||
path += part;
|
||||
}
|
||||
}
|
||||
85
lib/misc/fgpath.hxx
Normal file
85
lib/misc/fgpath.hxx
Normal file
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// fgpath.hxx -- routines to abstract out path separator differences
|
||||
// between MacOS and the rest of the world
|
||||
//
|
||||
// Written by Curtis L. Olson, started April 1999.
|
||||
//
|
||||
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
|
||||
#ifndef _FGPATH_HXX
|
||||
#define _FGPATH_HXX
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <Include/compiler.h>
|
||||
|
||||
#include STL_STRING
|
||||
|
||||
FG_USING_STD(string);
|
||||
|
||||
|
||||
#ifdef MACOS
|
||||
# define FG_PATH_SEP ':'
|
||||
# define FG_BAD_PATH_SEP '/'
|
||||
#else
|
||||
# define FG_PATH_SEP '/'
|
||||
# define FG_BAD_PATH_SEP ':'
|
||||
#endif
|
||||
|
||||
|
||||
class FGPath {
|
||||
|
||||
private:
|
||||
|
||||
string path;
|
||||
|
||||
public:
|
||||
|
||||
// default constructor
|
||||
FGPath();
|
||||
|
||||
// create a path based on "path"
|
||||
FGPath( const string p );
|
||||
|
||||
// destructor
|
||||
~FGPath();
|
||||
|
||||
// set path
|
||||
void set( const string p );
|
||||
|
||||
// append another piece to the existing path
|
||||
void append( const string p );
|
||||
|
||||
// concatenate a string to the end of the path without inserting a
|
||||
// path separator
|
||||
void concat( const string p );
|
||||
|
||||
// get the path string
|
||||
inline string str() const { return path; }
|
||||
inline const char *c_str() { return path.c_str(); }
|
||||
};
|
||||
|
||||
|
||||
#endif // _FGPATH_HXX
|
||||
|
||||
|
||||
157
lib/misc/fgstream.cxx
Normal file
157
lib/misc/fgstream.cxx
Normal file
@@ -0,0 +1,157 @@
|
||||
// zlib input file stream wrapper.
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#include <ctype.h> // isspace()
|
||||
#include <Misc/fgstream.hxx>
|
||||
|
||||
fg_gzifstream::fg_gzifstream()
|
||||
: istream(&gzbuf)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Open a possibly gzipped file for reading.
|
||||
//
|
||||
fg_gzifstream::fg_gzifstream( const string& name, ios_openmode io_mode )
|
||||
: istream(&gzbuf)
|
||||
{
|
||||
this->open( name, io_mode );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Attach a stream to an already opened file descriptor.
|
||||
//
|
||||
fg_gzifstream::fg_gzifstream( int fd, ios_openmode io_mode )
|
||||
: istream(&gzbuf)
|
||||
{
|
||||
gzbuf.attach( fd, io_mode );
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Open a possibly gzipped file for reading.
|
||||
// If the initial open fails and the filename has a ".gz" extension then
|
||||
// remove the extension and try again.
|
||||
// If the initial open fails and the filename doesn't have a ".gz" extension
|
||||
// then append ".gz" and try again.
|
||||
//
|
||||
void
|
||||
fg_gzifstream::open( const string& name, ios_openmode io_mode )
|
||||
{
|
||||
gzbuf.open( name.c_str(), io_mode );
|
||||
if ( ! gzbuf.is_open() )
|
||||
{
|
||||
string s = name;
|
||||
if ( s.substr( s.length() - 3, 3 ) == ".gz" )
|
||||
{
|
||||
// remove ".gz" suffix
|
||||
s.replace( s.length() - 3, 3, "" );
|
||||
// s.erase( s.length() - 3, 3 );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Append ".gz" suffix
|
||||
s += ".gz";
|
||||
}
|
||||
|
||||
// Try again.
|
||||
gzbuf.open( s.c_str(), io_mode );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
fg_gzifstream::attach( int fd, ios_openmode io_mode )
|
||||
{
|
||||
gzbuf.attach( fd, io_mode );
|
||||
}
|
||||
|
||||
//
|
||||
// Manipulators
|
||||
//
|
||||
|
||||
istream&
|
||||
skipeol( istream& in )
|
||||
{
|
||||
char c = '\0';
|
||||
// skip to end of line.
|
||||
|
||||
#ifdef __MWERKS__
|
||||
while ( in.get(c) && c != '\0' ) {
|
||||
#else
|
||||
while ( in.get(c) ) {
|
||||
#endif
|
||||
if ( (c == '\n') || (c == '\r') ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
istream&
|
||||
skipws( istream& in ) {
|
||||
char c;
|
||||
#ifdef __MWERKS__
|
||||
while ( in.get(c) && c != '\0' ) {
|
||||
#else
|
||||
while ( in.get(c) ) {
|
||||
#endif
|
||||
|
||||
#ifdef __MWERKS__
|
||||
if ( ! isspace( c ) && c != '\n' ) {
|
||||
#else
|
||||
if ( ! isspace( c ) ) {
|
||||
#endif
|
||||
// put pack the non-space character
|
||||
in.putback(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
istream&
|
||||
skipcomment( istream& in )
|
||||
{
|
||||
while ( in )
|
||||
{
|
||||
// skip whitespace
|
||||
#ifdef __MWERKS__
|
||||
in >> ::skipws;
|
||||
#else
|
||||
in >> skipws;
|
||||
#endif
|
||||
|
||||
char c;
|
||||
if ( in.get( c ) && c != '#' )
|
||||
{
|
||||
// not a comment
|
||||
in.putback(c);
|
||||
break;
|
||||
}
|
||||
in >> skipeol;
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
102
lib/misc/fgstream.hxx
Normal file
102
lib/misc/fgstream.hxx
Normal file
@@ -0,0 +1,102 @@
|
||||
// zlib input file stream wrapper.
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#ifndef _FGSTREAM_HXX
|
||||
#define _FGSTREAM_HXX
|
||||
|
||||
#ifndef __cplusplus
|
||||
# error This library requires C++
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "Include/config.h"
|
||||
#endif
|
||||
|
||||
#include <Include/compiler.h>
|
||||
|
||||
#if defined( FG_HAVE_STD_INCLUDES )
|
||||
# include <istream>
|
||||
#elif defined ( FG_HAVE_NATIVE_SGI_COMPILERS )
|
||||
# include <CC/stream.h>
|
||||
#elif defined ( __BORLANDC__ )
|
||||
# include <iostream>
|
||||
#else
|
||||
# include <istream.h>
|
||||
#endif
|
||||
|
||||
#include STL_STRING
|
||||
|
||||
#include "zfstream.hxx"
|
||||
|
||||
FG_USING_STD(string);
|
||||
|
||||
#ifndef FG_HAVE_NATIVE_SGI_COMPILERS
|
||||
FG_USING_STD(istream);
|
||||
#endif
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Envelope class for gzifstream.
|
||||
//
|
||||
class fg_gzifstream : private gzifstream_base, public istream
|
||||
{
|
||||
public:
|
||||
//
|
||||
fg_gzifstream();
|
||||
|
||||
// Attempt to open a file with and without ".gz" extension.
|
||||
fg_gzifstream( const string& name,
|
||||
ios_openmode io_mode = ios_in | ios_binary );
|
||||
|
||||
//
|
||||
fg_gzifstream( int fd, ios_openmode io_mode = ios_in|ios_binary );
|
||||
|
||||
// Attempt to open a file with and without ".gz" extension.
|
||||
void open( const string& name,
|
||||
ios_openmode io_mode = ios_in|ios_binary );
|
||||
|
||||
void attach( int fd, ios_openmode io_mode = ios_in|ios_binary );
|
||||
|
||||
void close() { gzbuf.close(); }
|
||||
|
||||
bool is_open() { return gzbuf.is_open(); }
|
||||
|
||||
private:
|
||||
// Not defined!
|
||||
fg_gzifstream( const fg_gzifstream& );
|
||||
void operator= ( const fg_gzifstream& );
|
||||
};
|
||||
|
||||
// istream manipulator that skips to end of line.
|
||||
istream& skipeol( istream& in );
|
||||
|
||||
// istream manipulator that skips over white space.
|
||||
istream& skipws( istream& in );
|
||||
|
||||
// istream manipulator that skips comments and white space.
|
||||
// A comment starts with '#'.
|
||||
istream& skipcomment( istream& in );
|
||||
|
||||
|
||||
#endif /* _FGSTREAM_HXX */
|
||||
|
||||
121
lib/misc/stopwatch.hxx
Normal file
121
lib/misc/stopwatch.hxx
Normal file
@@ -0,0 +1,121 @@
|
||||
/***************************************************************************
|
||||
* stopwatch.hxx Timer class, for use in benchmarking
|
||||
*
|
||||
* Based on blitz/Timer.h
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Suggestions: blitz-suggest@cybervision.com
|
||||
* Bugs: blitz-bugs@cybervision.com
|
||||
*
|
||||
* For more information, please see the Blitz++ Home Page:
|
||||
* http://seurat.uwaterloo.ca/blitz/
|
||||
*
|
||||
*/
|
||||
|
||||
// This class is not portable to non System V platforms.
|
||||
// It will need to be rewritten for Windows, NT, Mac.
|
||||
// NEEDS_WORK
|
||||
|
||||
#ifndef _STOPWATCH_HXX
|
||||
#define _STOPWATCH_HXX
|
||||
|
||||
#ifndef __cplusplus
|
||||
# error This library requires C++
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) && ! defined(HAVE_GETRUSAGE)
|
||||
# define HAVE_GETRUSAGE
|
||||
#endif
|
||||
|
||||
#if defined( WIN32 ) && defined( HAVE_GETRUSAGE )
|
||||
# undef HAVE_GETRUSAGE
|
||||
#endif // WIN32
|
||||
|
||||
#if defined( HAVE_GETRUSAGE )
|
||||
# if defined( __FreeBSD__ )
|
||||
# include <sys/types.h>
|
||||
# endif
|
||||
# include <sys/time.h>
|
||||
# include <sys/resource.h>
|
||||
# include <unistd.h>
|
||||
#elif defined( WIN32 )
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <time.h>
|
||||
#endif
|
||||
|
||||
class StopWatch {
|
||||
|
||||
public:
|
||||
StopWatch()
|
||||
{
|
||||
// state_ = uninitialized;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
// state_ = running;
|
||||
t1_ = systemTime();
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
t2_ = systemTime();
|
||||
// BZPRECONDITION(state_ == running);
|
||||
// state_ = stopped;
|
||||
}
|
||||
|
||||
double elapsedSeconds()
|
||||
{
|
||||
// BZPRECONDITION(state_ == stopped);
|
||||
return t2_ - t1_;
|
||||
}
|
||||
|
||||
private:
|
||||
StopWatch(StopWatch&) { }
|
||||
void operator=(StopWatch&) { }
|
||||
|
||||
double systemTime()
|
||||
{
|
||||
#if defined( HAVE_GETRUSAGE )
|
||||
getrusage(RUSAGE_SELF, &resourceUsage_);
|
||||
double seconds = resourceUsage_.ru_utime.tv_sec
|
||||
+ resourceUsage_.ru_stime.tv_sec;
|
||||
double micros = resourceUsage_.ru_utime.tv_usec
|
||||
+ resourceUsage_.ru_stime.tv_usec;
|
||||
return seconds + micros/1.0e6;
|
||||
#elif defined( WIN32 )
|
||||
return double(GetTickCount()) * double(1e-3);
|
||||
#else
|
||||
return clock() / (double) CLOCKS_PER_SEC;
|
||||
#endif
|
||||
}
|
||||
|
||||
// enum { uninitialized, running, stopped } state_;
|
||||
|
||||
#if defined( HAVE_GETRUSAGE )
|
||||
struct rusage resourceUsage_;
|
||||
#endif
|
||||
|
||||
double t1_, t2_;
|
||||
};
|
||||
|
||||
#endif // _STOPWATCH_HXX
|
||||
|
||||
71
lib/misc/strutils.cxx
Normal file
71
lib/misc/strutils.cxx
Normal file
@@ -0,0 +1,71 @@
|
||||
// String utilities.
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#include "strutils.hxx"
|
||||
|
||||
const string whitespace = " \n\r\t";
|
||||
|
||||
//
|
||||
string
|
||||
trimleft( const string& s, const string& trimmings )
|
||||
{
|
||||
string result;
|
||||
string::size_type pos = s.find_first_not_of( trimmings );
|
||||
if ( pos != string::npos )
|
||||
{
|
||||
result.assign( s.substr( pos ) );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string
|
||||
trimright( const string& s, const string& trimmings )
|
||||
{
|
||||
string result;
|
||||
|
||||
string::size_type pos = s.find_last_not_of( trimmings );
|
||||
if ( pos == string::npos )
|
||||
{
|
||||
// Not found, return the original string.
|
||||
result = s;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.assign( s.substr( 0, pos+1 ) );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string
|
||||
trim( const string& s, const string& trimmings )
|
||||
{
|
||||
return trimright( trimleft( s, trimmings ), trimmings );
|
||||
}
|
||||
|
||||
64
lib/misc/strutils.hxx
Normal file
64
lib/misc/strutils.hxx
Normal file
@@ -0,0 +1,64 @@
|
||||
// String utilities.
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#ifndef STRUTILS_H
|
||||
#define STRUTILS_H
|
||||
|
||||
#include <Include/compiler.h>
|
||||
#include STL_STRING
|
||||
|
||||
#ifdef FG_HAVE_STD_INCLUDES
|
||||
# include <cstdlib>
|
||||
#else
|
||||
# include <stdlib.h>
|
||||
#endif
|
||||
|
||||
FG_USING_STD(string);
|
||||
|
||||
// Default characters to remove.
|
||||
extern const string whitespace;
|
||||
|
||||
// Returns a string with trailing characters removed.
|
||||
string trimleft( const string& s, const string& trimmings = whitespace );
|
||||
|
||||
// Returns a string with leading characters removed.
|
||||
string trimright( const string& s, const string& trimmings = whitespace );
|
||||
|
||||
// Returns a string with leading and trailing characters removed.
|
||||
string trim( const string& s, const string& trimmings = whitespace );
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
inline double
|
||||
atof( const string& str )
|
||||
{
|
||||
return ::atof( str.c_str() );
|
||||
}
|
||||
|
||||
inline int
|
||||
atoi( const string& str )
|
||||
{
|
||||
return ::atoi( str.c_str() );
|
||||
}
|
||||
|
||||
#endif // STRUTILS_H
|
||||
|
||||
309
lib/misc/zfstream.cxx
Normal file
309
lib/misc/zfstream.cxx
Normal file
@@ -0,0 +1,309 @@
|
||||
// A C++ I/O streams interface to the zlib gz* functions
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
// Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#include <memory.h>
|
||||
#include "zfstream.hxx"
|
||||
|
||||
//
|
||||
// Construct a gzfilebuf object.
|
||||
// Allocate memory for 'get' buffer and zero all buffer pointers.
|
||||
//
|
||||
gzfilebuf::gzfilebuf()
|
||||
: streambuf(),
|
||||
file(NULL),
|
||||
mode(0),
|
||||
own_file_descriptor(false),
|
||||
ibuf_size(0),
|
||||
ibuffer(0)
|
||||
{
|
||||
// try {
|
||||
ibuf_size = page_size / sizeof(char);
|
||||
ibuffer = new char [ibuf_size];
|
||||
// } catch (...) {
|
||||
// delete [] ibuffer;
|
||||
// }
|
||||
|
||||
// Null get and set pointers.
|
||||
this->setg(0,0,0);
|
||||
this->setp(0,0);
|
||||
}
|
||||
|
||||
gzfilebuf::~gzfilebuf()
|
||||
{
|
||||
sync();
|
||||
if ( own_file_descriptor )
|
||||
this->close();
|
||||
delete [] ibuffer;
|
||||
}
|
||||
|
||||
void
|
||||
gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
|
||||
{
|
||||
// memset( char_mode, '\0', 10 );
|
||||
// char* p = char_mode;
|
||||
|
||||
if ( io_mode & ios_in )
|
||||
{
|
||||
mode = ios_in;
|
||||
*p++ = 'r';
|
||||
}
|
||||
else if ( io_mode & ios_app )
|
||||
{
|
||||
mode = ios_app;
|
||||
*p++ = 'a';
|
||||
}
|
||||
else
|
||||
{
|
||||
mode = ios_out;
|
||||
*p++ = 'w';
|
||||
}
|
||||
|
||||
if ( io_mode & ios_binary )
|
||||
{
|
||||
mode |= ios_binary;
|
||||
*p++ = 'b';
|
||||
}
|
||||
|
||||
// Hard code the compression level
|
||||
if ( io_mode & (ios_out | ios_app) )
|
||||
{
|
||||
*p++ = '9';
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
gzfilebuf*
|
||||
gzfilebuf::open( const char *name, ios_openmode io_mode )
|
||||
{
|
||||
if ( is_open() )
|
||||
return NULL;
|
||||
|
||||
char char_mode[10];
|
||||
cvt_iomode( char_mode, io_mode );
|
||||
if ( (file = gzopen(name, char_mode)) == NULL )
|
||||
return NULL;
|
||||
|
||||
own_file_descriptor = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
gzfilebuf*
|
||||
gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
|
||||
{
|
||||
if ( is_open() )
|
||||
return NULL;
|
||||
|
||||
char char_mode[10];
|
||||
cvt_iomode( char_mode, io_mode );
|
||||
if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
|
||||
return NULL;
|
||||
|
||||
own_file_descriptor = false;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
gzfilebuf*
|
||||
gzfilebuf::close()
|
||||
{
|
||||
if ( is_open() )
|
||||
{
|
||||
sync();
|
||||
gzclose( file );
|
||||
file = NULL;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// int
|
||||
// gzfilebuf::setcompressionlevel( int comp_level )
|
||||
// {
|
||||
// return gzsetparams(file, comp_level, -2);
|
||||
// }
|
||||
|
||||
// int
|
||||
// gzfilebuf::setcompressionstrategy( int comp_strategy )
|
||||
// {
|
||||
// return gzsetparams(file, -2, comp_strategy);
|
||||
// }
|
||||
|
||||
|
||||
streampos
|
||||
gzfilebuf::seekoff( streamoff, ios_seekdir, int )
|
||||
{
|
||||
return streampos(EOF);
|
||||
}
|
||||
|
||||
gzfilebuf::int_type
|
||||
gzfilebuf::overflow( int_type )
|
||||
{
|
||||
#if 0
|
||||
if ( !is_open() || !(mode & ios::out) )
|
||||
return EOF;
|
||||
|
||||
if ( !base() )
|
||||
{
|
||||
if ( allocate() == EOF )
|
||||
return EOF;
|
||||
setg(0,0,0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (in_avail())
|
||||
{
|
||||
return EOF;
|
||||
}
|
||||
|
||||
if (out_waiting())
|
||||
{
|
||||
if (flushbuf() == EOF)
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
|
||||
int bl = blen();
|
||||
setp( base(), base() + bl);
|
||||
|
||||
if ( c != EOF )
|
||||
{
|
||||
*pptr() = c;
|
||||
pbump(1);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
gzfilebuf::sync()
|
||||
{
|
||||
if ( !is_open() )
|
||||
return EOF;
|
||||
|
||||
if ( pptr() != 0 && pptr() > pbase() )
|
||||
return flushbuf();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
gzfilebuf::int_type
|
||||
gzfilebuf::flushbuf()
|
||||
{
|
||||
char* q = pbase();
|
||||
int n = pptr() - q;
|
||||
|
||||
if ( gzwrite( file, q, n) < n )
|
||||
return traits_type::eof();
|
||||
|
||||
setp(0,0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
gzfilebuf::int_type
|
||||
gzfilebuf::underflow()
|
||||
{
|
||||
// cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
|
||||
// Error if the file not open for reading.
|
||||
if ( !is_open() || !(mode & ios_in) )
|
||||
return traits_type::eof();
|
||||
|
||||
// If the input buffer is empty then try to fill it.
|
||||
if ( gptr() != 0 && gptr() < egptr() )
|
||||
{
|
||||
return int_type(*gptr());
|
||||
}
|
||||
else
|
||||
{
|
||||
return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Load the input buffer from the underlying gz file.
|
||||
// Returns number of characters read, or EOF.
|
||||
//
|
||||
int
|
||||
gzfilebuf::fillbuf()
|
||||
{
|
||||
int t = gzread( file, ibuffer, ibuf_size );
|
||||
if ( t <= 0)
|
||||
{
|
||||
// disable get area
|
||||
setg(0,0,0);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
// Set the input (get) pointers
|
||||
setg( ibuffer, ibuffer, ibuffer+t );
|
||||
|
||||
// cerr << "gzfilebuf::fillbuf():"
|
||||
// << " t=" << t
|
||||
// << ", ibuffer=" << (void*)ibuffer
|
||||
// << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
#if 0
|
||||
gzifstream::gzifstream()
|
||||
: istream(&buffer), buffer()
|
||||
{
|
||||
clear( ios_badbit );
|
||||
}
|
||||
|
||||
gzifstream::gzifstream( const char *name, ios_openmode io_mode )
|
||||
: istream(&buffer), buffer()
|
||||
{
|
||||
this->open( name, io_mode );
|
||||
}
|
||||
|
||||
gzifstream::gzifstream( int fd, ios_openmode io_mode )
|
||||
: istream(&buffer), buffer()
|
||||
{
|
||||
buffer.attach( fd, io_mode );
|
||||
}
|
||||
|
||||
gzifstream::~gzifstream()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
gzifstream::open( const char *name, ios_openmode io_mode )
|
||||
{
|
||||
if ( !buffer.open( name, io_mode ) )
|
||||
clear( ios_failbit | ios_badbit );
|
||||
else
|
||||
clear();
|
||||
}
|
||||
|
||||
void
|
||||
gzifstream::close()
|
||||
{
|
||||
if ( !buffer.close() )
|
||||
clear( ios_failbit | ios_badbit );
|
||||
}
|
||||
#endif
|
||||
|
||||
154
lib/misc/zfstream.hxx
Normal file
154
lib/misc/zfstream.hxx
Normal file
@@ -0,0 +1,154 @@
|
||||
// A C++ I/O streams interface to the zlib gz* functions
|
||||
//
|
||||
// Written by Bernie Bright, 1998
|
||||
// Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
|
||||
//
|
||||
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#ifndef _zfstream_hxx
|
||||
#define _zfstream_hxx
|
||||
|
||||
#include "zlib/zlib.h"
|
||||
#include "Include/compiler.h"
|
||||
|
||||
#ifdef FG_HAVE_STD_INCLUDES
|
||||
|
||||
# include <streambuf>
|
||||
# include <istream>
|
||||
|
||||
# define ios_openmode ios_base::openmode
|
||||
# define ios_in ios_base::in
|
||||
# define ios_out ios_base::out
|
||||
# define ios_app ios_base::app
|
||||
# define ios_binary ios_base::binary
|
||||
|
||||
# define ios_seekdir ios_base::seekdir
|
||||
|
||||
# define ios_badbit ios_base::badbit
|
||||
# define ios_failbit ios_base::failbit
|
||||
|
||||
FG_USING_STD(streambuf);
|
||||
FG_USING_STD(ios_base);
|
||||
FG_USING_STD(streampos);
|
||||
FG_USING_STD(streamoff);
|
||||
|
||||
#else
|
||||
|
||||
# ifdef FG_HAVE_STREAMBUF
|
||||
# include <streambuf.h>
|
||||
# include <istream.h>
|
||||
# else
|
||||
# include <iostream.h>
|
||||
# endif
|
||||
|
||||
//# define ios_openmode ios::open_mode
|
||||
# define ios_openmode int
|
||||
# define ios_in ios::in
|
||||
# define ios_out ios::out
|
||||
# define ios_app ios::app
|
||||
|
||||
#if defined(__GNUC__) && __GNUC_MINOR__ < 8
|
||||
# define ios_binary ios::bin
|
||||
#elif defined( FG_HAVE_NATIVE_SGI_COMPILERS )
|
||||
# define ios_binary 0
|
||||
#else
|
||||
# define ios_binary ios::binary
|
||||
#endif
|
||||
|
||||
# define ios_seekdir ios::seek_dir
|
||||
|
||||
# define ios_badbit ios::badbit
|
||||
# define ios_failbit ios::failbit
|
||||
|
||||
# include "Include/fg_traits.hxx"
|
||||
|
||||
#endif // FG_HAVE_STD_INCLUDES
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//
|
||||
//
|
||||
class gzfilebuf : public streambuf
|
||||
{
|
||||
public:
|
||||
|
||||
#ifndef FG_HAVE_STD_INCLUDES
|
||||
typedef char_traits<char> traits_type;
|
||||
typedef char_traits<char>::int_type int_type;
|
||||
typedef char_traits<char>::pos_type pos_type;
|
||||
typedef char_traits<char>::off_type off_type;
|
||||
#endif
|
||||
|
||||
gzfilebuf();
|
||||
virtual ~gzfilebuf();
|
||||
|
||||
gzfilebuf* open( const char* name, ios_openmode io_mode );
|
||||
gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
|
||||
gzfilebuf* close();
|
||||
|
||||
// int setcompressionlevel( int comp_level );
|
||||
// int setcompressionstrategy( int comp_strategy );
|
||||
bool is_open() const { return (file != NULL); }
|
||||
virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
|
||||
virtual int sync();
|
||||
|
||||
protected:
|
||||
|
||||
virtual int_type underflow();
|
||||
virtual int_type overflow( int_type c = traits_type::eof() );
|
||||
|
||||
private:
|
||||
|
||||
int_type flushbuf();
|
||||
int fillbuf();
|
||||
|
||||
// Convert io_mode to "rwab" string.
|
||||
void cvt_iomode( char* mode_str, ios_openmode io_mode );
|
||||
|
||||
private:
|
||||
|
||||
gzFile file;
|
||||
ios_openmode mode;
|
||||
bool own_file_descriptor;
|
||||
|
||||
// Get (input) buffer.
|
||||
int ibuf_size;
|
||||
char* ibuffer;
|
||||
|
||||
enum { page_size = 4096 };
|
||||
|
||||
private:
|
||||
// Not defined
|
||||
gzfilebuf( const gzfilebuf& );
|
||||
void operator= ( const gzfilebuf& );
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
//
|
||||
//
|
||||
struct gzifstream_base
|
||||
{
|
||||
gzifstream_base() {}
|
||||
|
||||
gzfilebuf gzbuf;
|
||||
};
|
||||
|
||||
#endif // _zfstream_hxx
|
||||
|
||||
Reference in New Issue
Block a user