8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 08:03:04 +01:00

Added native firebird::string class.

This commit is contained in:
alexpeshkoff 2004-02-08 17:08:34 +00:00
parent 835a61a746
commit efb64b2aab
20 changed files with 694 additions and 144 deletions

View File

@ -129,6 +129,10 @@ SOURCE=..\..\..\src\jrd\db_alias.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\src\common\classes\fb_string.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\src\jrd\os\win32\fbsyslog.cpp
# End Source File
# Begin Source File

View File

@ -129,6 +129,10 @@ SOURCE=..\..\..\src\jrd\db_alias.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\src\common\classes\fb_string.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\src\jrd\os\win32\fbsyslog.cpp
# End Source File
# Begin Source File

View File

@ -125,6 +125,10 @@ SOURCE=..\..\..\src\common\config\dir_list.cpp
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\src\common\classes\fb_string.cpp
# End Source File
# Begin Source File
SOURCE=..\..\..\src\jrd\os\win32\fbsyslog.cpp
# End Source File
# Begin Source File

View File

@ -32,7 +32,8 @@
#include "../jrd/gdsassert.h"
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
/******************************************************************************
*
@ -223,7 +224,7 @@ ConfigImpl::~ConfigImpl()
delete[] values;
}
string ConfigImpl::getValue(ConfigFile& file, ConfigKey key)
string ConfigImpl::getValue(ConfigFile& file, const ConfigKey key)
{
return file.doesKeyExist(key) ? file.getString(key) : "";
}

View File

@ -52,7 +52,8 @@
#undef INFORM_ON_NO_CONF
#endif
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
/******************************************************************************
*
@ -61,7 +62,7 @@ typedef Firebird::string string;
bool ConfigFile::key_compare::operator()(const string& x, const string& y) const
{
return Firebird::PathName(x) < Firebird::PathName(y);
return x < y;
}
/******************************************************************************
@ -224,12 +225,12 @@ void ConfigFile::loadConfig()
parameters.clear();
std::ifstream configFileStream(configFile.c_str());
FILE* ifile = fopen(configFile.c_str(), "rt");
#ifdef EXIT_ON_NO_CONF
int BadLinesCount = 0;
#endif
if (!configFileStream)
if (!ifile)
{
// config file does not exist
#ifdef INFORM_ON_NO_CONF
@ -240,7 +241,7 @@ void ConfigFile::loadConfig()
#endif
Firebird::Syslog::Record(fExitOnError ?
Firebird::Syslog::Error :
Firebird::Syslog::Warning, Msg);
Firebird::Syslog::Warning, Msg.ToString());
#ifdef EXIT_ON_NO_CONF
if (fExitOnError)
exit(1);
@ -250,9 +251,9 @@ void ConfigFile::loadConfig()
}
string inputLine;
while (!configFileStream.eof())
while (!feof(ifile))
{
std::getline(configFileStream, inputLine);
inputLine.LoadFromFile(ifile);
stripComments(inputLine);
stripLeadingWhiteSpace(inputLine);
@ -264,8 +265,8 @@ void ConfigFile::loadConfig()
if (inputLine.find('=') == string::npos)
{
string Msg = configFile + ": illegal line \"" +
inputLine + "\"";
Firebird::string Msg = (configFile + ": illegal line \"" +
inputLine + "\"").ToString();
Firebird::Syslog::Record(fExitOnError ?
Firebird::Syslog::Error :
Firebird::Syslog::Warning, Msg);

View File

@ -56,7 +56,8 @@
class ConfigFile
{
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
// used to provide proper filename handling in various OS
class key_compare : public std::binary_function<const string&, const string&, bool>

View File

@ -39,7 +39,8 @@ class ConfigImpl : public ConfigRoot
{
friend class Config;
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
enum ConfigType
{
@ -62,7 +63,7 @@ class ConfigImpl : public ConfigRoot
public:
~ConfigImpl();
static string getValue(ConfigFile&, ConfigKey);
static string getValue(ConfigFile&, const ConfigKey);
static int asInteger(const string&);
static bool asBoolean(const string&);

View File

@ -1,62 +1,595 @@
/*
* fb_string.h
* firebird_test
*
* Created by john on Fri Dec 14 2001.
* Copyright (c) 2001 __MyCompanyName__. All rights reserved.
*
*/
#ifndef FB_STRING_H
#define FB_STRING_H
#include <stdio.h>
#include <string.h>
#include "../include/fb_types.h"
#include "../include/fb_exception.h"
#include "../common/classes/alloc.h"
#include "../include/gen/autoconfig.h"
#include <string>
namespace Firebird
{
typedef std::basic_string<char, std::char_traits<char>,
Firebird::allocator<char> > string;
class PathName : public string {
public:
inline PathName(const string &s) : string(s) {}
inline PathName(const char *s) : string(s) {}
inline PathName(void) : string() {}
inline bool operator<(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) < string(r)) :
(fb_stricmp(c_str(), r.c_str()) < 0);
class StringAllocator {
private:
MemoryPool* pool;
protected:
inline StringAllocator() {
pool = getDefaultMemoryPool();
}
inline bool operator<=(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) <= string(r)) :
(fb_stricmp(c_str(), r.c_str()) <= 0);
inline StringAllocator(MemoryPool* p) {
pool = p;
}
inline bool operator==(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) == string(r)) :
(fb_stricmp(this->c_str(), r.c_str()) == 0);
}
inline bool operator>=(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) >= string(r)) :
(fb_stricmp(this->c_str(), r.c_str()) >= 0);
}
inline bool operator>(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) > string(r)) :
(fb_stricmp(this->c_str(), r.c_str()) > 0);
}
inline bool operator!=(const PathName &r) const {
return CASE_SENSITIVITY ?
(string(*this) != string(r)) :
(fb_stricmp(this->c_str(), r.c_str()) != 0);
inline char* getMemory(unsigned int n
#ifdef DEV_BUILD
, const char *fun
#endif
) {
char *rc = new(*pool
#ifdef DEV_BUILD
, fun, 0
#endif
) char[n];
if (! rc) {
fatal_exception::raise("Firebird::string - out of memory");
}
return rc;
}
};
class AbstractString : public StringAllocator {
public:
typedef char char_type;
typedef unsigned int size_type;
typedef int difference_type;
typedef char* pointer;
typedef const char* const_pointer;
typedef char& reference;
typedef const char& const_reference;
typedef char value_type;
typedef pointer iterator;
typedef const_pointer const_iterator;
static const size_type npos;
enum {smallStorageSize = 32, reserveSize = 16, keepSize = 512};
protected:
union {
char_type smallStorage[smallStorageSize];
struct {
char_type* bigStorage;
};
};
unsigned short userSize, actualSize;
private:
inline void checkPos(size_type pos) const {
if (pos >= length()) {
fatal_exception::raise("Firebird::string - pos out of range");
}
}
static inline void checkSize(size_type size) {
if (size > max_size()) {
fatal_exception::raise("Firebird::string - size exceeds predefined limit");
}
}
// Returns bigStorage for string sized <uSize>,
// not taking care about ability to place it
// in smallStorage. Changes actualSize of string.
inline void allocateStorage(size_type uSize
#ifdef DEV_BUILD
, const char *fun
#endif
) {
fb_assert(uSize >= smallStorageSize);
size_type aSize = uSize + reserveSize + 1;
actualSize = aSize;
bigStorage = getMemory(aSize
#ifdef DEV_BUILD
, fun
#endif
);
}
// Returns valid pointer to new storage
// and sets all size related information of this string.
// Primary usage - constructors
inline pointer createStorage(size_type uSize) {
checkSize(uSize);
userSize = uSize;
if (uSize < smallStorageSize) {
actualSize = smallStorageSize;
smallStorage[uSize] = 0;
return smallStorage;
}
allocateStorage(uSize
#ifdef DEV_BUILD
, "createStorage"
#endif
);
bigStorage[uSize] = 0;
return bigStorage;
}
// Returns (possibly reallocated) newStorage for current string,
// setting values for oldStorage (if string is not
// updated inplace) and oldSize. oldStorage
// should be released after use if oldSize >= smallStorageSize
struct StoragePair {
pointer newStorage, oldStorage;
size_type oldSize;
char_type oldSmallStorage[smallStorageSize];
~StoragePair() {
if (oldSize >= AbstractString::smallStorageSize)
delete oldStorage;
}
};
friend struct StoragePair;
inline void openStorage(StoragePair& rc, size_type newSize
#ifdef DEV_BUILD
, const char *fun
#endif
) {
checkSize(newSize);
rc.oldStorage = 0;
rc.oldSize = userSize;
userSize = newSize;
if (newSize < smallStorageSize) {
if (actualSize > smallStorageSize) {
rc.oldStorage = bigStorage;
}
rc.newStorage = smallStorage;
actualSize = smallStorageSize;
return;
}
if (newSize < actualSize && newSize + keepSize > actualSize) {
rc.newStorage = bigStorage;
return;
}
if (actualSize <= smallStorageSize) {
memcpy(rc.oldSmallStorage, smallStorage, rc.oldSize);
rc.oldStorage = rc.oldSmallStorage;
}
else {
rc.oldStorage = bigStorage;
}
allocateStorage(newSize
#ifdef DEV_BUILD
, fun
#endif
);
rc.newStorage = bigStorage;
}
// Returns pointer to current storage.
inline pointer getStorage() {
return actualSize <= smallStorageSize ?
smallStorage : bigStorage;
}
protected:
AbstractString(size_type size, const_pointer data);
AbstractString(const_pointer p1, size_type n1,
const_pointer p2, size_type n2);
AbstractString(const AbstractString& v);
inline AbstractString() {
actualSize = smallStorageSize;
userSize = 0;
smallStorage[0] = 0;
}
AbstractString(size_type size, char_type c);
pointer Modify(void) {
return getStorage();
}
static void AdjustRange(size_type length, size_type& pos, size_type& n);
pointer baseAssign(size_type n);
pointer baseAppend(size_type n);
pointer baseInsert(size_type p0, size_type n);
void baseErase(size_type p0, size_type n);
enum TrimType {TrimLeft, TrimRight, TrimBoth};
void baseTrim(TrimType WhereTrim, const_pointer ToTrim);
public:
inline const_pointer c_str() const {
return actualSize <= smallStorageSize ?
smallStorage : bigStorage;
}
inline size_type length() const {
return userSize;
}
//void reserve(size_type n = 0);
void resize(size_type n, char_type c = ' ');
inline size_type copy(pointer s, size_type n, size_type pos = 0) const {
AdjustRange(length(), pos, n);
memcpy(s, &c_str()[pos], n);
return n;
}
/* inline void swap(AbstractString& str) {
Storage *tmp = StringData;
StringData = str.StringData;
str.StringData = tmp;
}*/
inline size_type find(const AbstractString& str, size_type pos = 0) const {
return find(str.c_str(), pos);
}
inline size_type find(const_pointer s, size_type pos = 0) const {
const_pointer p = strstr(&c_str()[pos], s);
return p ? p - c_str() : npos;
}
inline size_type find(char_type c, size_type pos = 0) const {
const_pointer p = strchr(&c_str()[pos], c);
return p ? p - c_str() : npos;
}
inline size_type rfind(const AbstractString& str, size_type pos = npos) const {
return rfind(str.c_str(), pos);
}
size_type rfind(const_pointer s, size_type pos = npos) const;
size_type rfind(char_type c, size_type pos = npos) const;
inline size_type find_first_of(const AbstractString& str, size_type pos = 0) const {
return find_first_of(str.c_str(), pos, str.length());
}
size_type find_first_of(const_pointer s, size_type pos, size_type n) const;
inline size_type find_first_of(const_pointer s, size_type pos = 0) const {
return find_first_of(s, pos, strlen(s));
}
inline size_type find_first_of(char_type c, size_type pos = 0) const {
return find(c, pos);
}
inline size_type find_last_of(const AbstractString& str, size_type pos = npos) const {
return find_last_of(str.c_str(), pos, str.length());
}
size_type find_last_of(const_pointer s, size_type pos, size_type n = npos) const;
inline size_type find_last_of(const_pointer s, size_type pos = npos) const {
return find_last_of(s, pos, strlen(s));
}
inline size_type find_last_of(char_type c, size_type pos = npos) const {
return rfind(c, pos);
}
inline size_type find_first_not_of(const AbstractString& str, size_type pos = 0) const {
return find_first_not_of(str.c_str(), pos, str.length());
}
size_type find_first_not_of(const_pointer s, size_type pos, size_type n) const;
inline size_type find_first_not_of(const_pointer s, size_type pos = 0) const {
return find_first_not_of(s, pos, strlen(s));
}
inline size_type find_first_not_of(char_type c, size_type pos = 0) const {
char s[2];
s[0] = c;
s[1] = 0;
return find_first_not_of(s, pos, 1);
}
inline size_type find_last_not_of(const AbstractString& str, size_type pos = npos) const {
return find_last_not_of(str.c_str(), pos, str.length());
}
size_type find_last_not_of(const_pointer s, size_type pos, size_type n = npos) const;
inline size_type find_last_not_of(const_pointer s, size_type pos = npos) const {
return find_last_not_of(s, pos, strlen(s));
}
inline size_type find_last_not_of(char_type c, size_type pos = npos) const {
char s[2];
s[0] = c;
s[1] = 0;
return find_last_not_of(s, pos, 1);
}
inline iterator begin() {
return Modify();
}
inline const_iterator begin() const {
return c_str();
}
inline iterator end() {
return &Modify()[length()];
}
inline const_iterator end() const {
return &c_str()[length()];
}
inline const_reference at(size_type pos) const {
checkPos(pos);
return c_str()[pos];
}
inline reference at(size_type pos) {
checkPos(pos);
return Modify()[pos];
}
inline const_reference operator[](size_type pos) const {
return at(pos);
}
inline reference operator[](size_type pos) {
return at(pos);
}
inline const_pointer data() const {
return c_str();
}
inline size_type size() const {
return length();
}
static inline size_type max_size() {
return 0xfffe;
}
inline size_type capacity() const {
return actualSize - 1;
}
inline bool empty() const {
return length() == 0;
}
void upper();
void lower();
inline void ltrim(const_pointer ToTrim = " ") {
baseTrim(TrimLeft, ToTrim);
}
inline void rtrim(const_pointer ToTrim = " ") {
baseTrim(TrimRight, ToTrim);
}
inline void trim(const_pointer ToTrim = " ") {
baseTrim(TrimBoth, ToTrim);
}
inline void alltrim(const_pointer ToTrim = " ") {
baseTrim(TrimBoth, ToTrim);
}
bool LoadFromFile(FILE *file);
inline ~AbstractString() {
if (actualSize > smallStorageSize)
delete bigStorage;
}
};
class StringComparator {
public:
static inline int compare(AbstractString::const_pointer s1, AbstractString::const_pointer s2, unsigned int n) {
return memcmp(s1, s2, n);
}
};
class PathNameComparator {
public:
static int compare(AbstractString::const_pointer s1, AbstractString::const_pointer s2, unsigned int n);
};
template<typename Comparator>
class StringBase : public AbstractString {
typedef StringBase<Comparator> StringType;
protected:
inline StringBase<Comparator>(const_pointer p1, size_type n1,
const_pointer p2, size_type n2) :
AbstractString(p1, n1, p2, n2) {;}
private:
inline StringType add(const_pointer s, size_type n) const {
return StringBase<Comparator>(c_str(), length(), s, n);
}
public:
inline StringBase<Comparator>() : AbstractString() {}
inline StringBase<Comparator>(const StringType& v) : AbstractString(v) {}
inline StringBase<Comparator>(const_pointer s, size_type n) : AbstractString(n, s) {}
inline StringBase<Comparator>(const_pointer s) : AbstractString(strlen(s), s) {}
inline StringBase<Comparator>(size_type n, char_type c) : AbstractString(n, c) {}
inline StringBase<Comparator>(char_type c) : AbstractString(1, c) {}
inline StringBase<Comparator>(const_iterator first, const_iterator last) : AbstractString(last - first, first) {}
inline StringType& append(const StringType& str) {
fb_assert(&str != this);
return append(str.c_str(), str.length());
}
inline StringType& append(const StringType& str, size_type pos, size_type n) {
fb_assert(&str != this);
AdjustRange(str.length(), pos, n);
return append(&str.c_str()[pos], n);
}
inline StringType& append(const_pointer s, size_type n) {
memcpy(baseAppend(n), s, n);
return *this;
}
inline StringType& append(const_pointer s) {
return append(s, strlen(s));
}
inline StringType& append(size_type n, char_type c) {
memset(baseAppend(n), c, n);
return *this;
}
inline StringType& append(const_iterator first, const_iterator last) {
return append(first, last - first);
}
inline StringType& assign(const StringType& str) {
fb_assert(&str != this);
return assign(str.c_str(), str.length());
}
inline StringType& assign(const StringType& str, size_type pos, size_type n) {
fb_assert(&str != this);
AdjustRange(str.length(), pos, n);
return assign(&str.c_str()[pos], n);
}
inline StringType& assign(const_pointer s, size_type n) {
memcpy(baseAssign(n), s, n);
return *this;
}
inline StringType& assign(const_pointer s) {
return assign(s, strlen(s));
}
inline StringType& assign(size_type n, char_type c) {
memset(baseAssign(n), c, n);
return *this;
}
inline StringType& assign(const_iterator first, const_iterator last) {
return assign(first, last - first);
}
inline StringType& operator=(const StringType& v) {
fb_assert(&v != this);
return assign(v);
}
inline StringType& operator=(const_pointer s) {
return assign(s, strlen(s));
}
inline StringType& operator=(char_type c) {
return assign(&c, 1);
}
inline StringType& operator+=(const StringType& v) {
fb_assert(&v != this);
return append(v);
}
inline StringType& operator+=(const_pointer s) {
return append(s);
}
inline StringType& operator+=(char_type c) {
return append(1, c);
}
inline StringType operator+(const StringType& v) const {
return add(v.c_str(), v.length());
fb_assert(&v != this);
}
inline StringType operator+(const_pointer s) const {
return add(s, strlen(s));
}
inline StringType operator+(char_type c) const {
return add(&c, 1);
}
inline StringBase<StringComparator> ToString() {
return StringBase<StringComparator>(c_str());
}
inline StringBase<StringComparator> ToPathName() {
return StringBase<PathNameComparator>(c_str());
}
inline StringType& insert(size_type p0, const StringType& str) {
fb_assert(&str != this);
return insert(p0, str.c_str(), str.length());
}
inline StringType& insert(size_type p0, const StringType& str, size_type pos, size_type n) {
fb_assert(&str != this);
AdjustRange(str.length(), pos, n);
return insert(p0, &str.c_str()[pos], n);
}
inline StringType& insert(size_type p0, const_pointer s, size_type n) {
if (p0 >= length()) {
return append(s, n);
}
memcpy(baseInsert(p0, n), s, n);
return *this;
}
inline StringType& insert(size_type p0, const_pointer s) {
return insert(p0, s, strlen(s));
}
inline StringType& insert(size_type p0, size_type n, char_type c) {
if (p0 >= length()) {
return append(n, c);
}
memset(baseInsert(p0, n), c, n);
return *this;
}
// iterator insert(iterator it, char_type c); // what to return here?
inline void insert(iterator it, size_type n, char_type c) {
insert(it - c_str(), n, c);
}
inline void insert(iterator it, const_iterator first, const_iterator last) {
insert(it - c_str(), first, last - first);
}
inline StringType& erase(size_type p0 = 0, size_type n = npos) {
baseErase(p0, n);
return *this;
}
inline iterator erase(iterator it) {
erase(it - c_str(), 1);
return it;
}
inline iterator erase(iterator first, iterator last) {
erase(first - c_str(), last-first);
return first;
}
inline StringType& replace(size_type p0, size_type n0, const StringType& str) {
fb_assert(&str != this);
return replace(p0, n0, str.c_str(), str.length());
}
inline StringType& replace(size_type p0, size_type n0, const StringType& str, size_type pos, size_type n) {
fb_assert(&str != this);
AdjustRange(str.length(), pos, n);
return replace(p0, n0, &str.c_str()[pos], n);
}
inline StringType& replace(size_type p0, size_type n0, const_pointer s, size_type n) {
erase(p0, n0);
return insert(p0, s, n);
}
inline StringType& replace(size_type p0, size_type n0, const_pointer s) {
return replace(p0, n0, s, strlen(s));
}
inline StringType& replace(size_type p0, size_type n0, size_type n, char_type c) {
erase(p0, n0);
return insert(p0, n, c);
}
inline StringType& replace(iterator first0, iterator last0, const StringType& str) {
fb_assert(&str != this);
return replace(first0 - c_str(), last0 - first0, str);
}
inline StringType& replace(iterator first0, iterator last0, const_pointer s, size_type n) {
return replace(first0 - c_str(), last0 - first0, s, n);
}
inline StringType& replace(iterator first0, iterator last0, const_pointer s) {
return replace(first0 - c_str(), last0 - first0, s);
}
inline StringType& replace(iterator first0, iterator last0, size_type n, char_type c) {
return replace(first0 - c_str(), last0 - first0, n, c);
}
inline StringType& replace(iterator first0, iterator last0, const_iterator first, const_iterator last) {
return replace(first0 - c_str(), last0 - first0, first, last - first);
}
inline StringType substr(size_type pos = 0, size_type n = npos) const {
AdjustRange(length(), pos, n);
return StringType(&c_str()[pos], n);
}
inline int compare(const StringType& str) const {
return compare(0, length(), str.c_str(), str.length());
}
inline int compare(size_type p0, size_type n0, const StringType& str) {
return compare(p0, n0, str.c_str(), str.length());
}
inline int compare(size_type p0, size_type n0, const StringType& str, size_type pos, size_type n) {
AdjustRange(str.length(), pos, n);
return compare(p0, n0, &str.c_str()[pos], n);
}
inline int compare(const_pointer s) const {
return compare(0, length(), s, strlen(s));
}
int compare(size_type p0, size_type n0, const_pointer s, size_type n) const {
AdjustRange(length(), p0, n0);
size_type ml = n0 < n ? n0 : n;
int rc = Comparator::compare(&c_str()[p0], s, ml);
return rc ? rc : n - n0;
}
inline bool operator< (const StringType& str) const {return compare(str) < 0;}
inline bool operator<=(const StringType& str) const {return compare(str) <= 0;}
inline bool operator==(const StringType& str) const {return compare(str) == 0;}
inline bool operator>=(const StringType& str) const {return compare(str) >= 0;}
inline bool operator> (const StringType& str) const {return compare(str) > 0;}
inline bool operator!=(const StringType& str) const {return compare(str) != 0;}
};
typedef StringBase<StringComparator> string;
inline string operator+(string::const_pointer s, const string& str) {
string rc(s);
rc += str;
return rc;
}
inline string operator+(string::char_type c, const string& str) {
string rc(c);
rc += str;
return rc;
}
typedef StringBase<PathNameComparator> PathName;
inline PathName operator+(PathName::const_pointer s, const PathName& str) {
PathName rc(s);
rc += str;
return rc;
}
inline PathName operator+(PathName::char_type c, const PathName& str) {
PathName rc(c);
rc += str;
return rc;
}
}

View File

@ -29,7 +29,7 @@
#include "../jrd/os/path_utils.h"
#include "../jrd/gds_proto.h"
typedef Firebird::string string;
typedef Firebird::PathName string;
const char* ALIAS_FILE = "aliases.conf";

View File

@ -3064,7 +3064,7 @@ void DYN_define_relation( GBL gbl, const UCHAR** ptr)
request = NULL;
id = -1;
Firebird::string Path, Name;
Firebird::PathName Path, Name;
try {

View File

@ -43,7 +43,7 @@
*
*/
/*
$Id: flu.cpp,v 1.42 2004-02-02 11:01:34 robocop Exp $
$Id: flu.cpp,v 1.43 2004-02-08 17:08:31 alexpeshkoff Exp $
*/
#include "firebird.h"
@ -51,10 +51,6 @@ $Id: flu.cpp,v 1.42 2004-02-02 11:01:34 robocop Exp $
#include "../common/config/dir_list.h"
#include "../jrd/os/path_utils.h"
#include <string>
using namespace std;
#include "../jrd/common.h"
#include "../jrd/flu.h"
#include "../jrd/gdsassert.h"
@ -487,7 +483,7 @@ FPTR_INT ISC_lookup_entrypoint(TEXT* module,
}
if (!mod) { // Start looking for "libxxxx.so" module names
string moduleName = "lib";
FireBird::PathName moduleName = "lib";
moduleName += (const char*) absolute_module;
mod = search_for_module((TEXT*) moduleName.c_str(), name, ShowAccessError);
}
@ -603,7 +599,7 @@ FPTR_INT ISC_lookup_entrypoint(TEXT* module,
#define REQUIRED_MODULE_ACCESS 4
#define OPEN_HANDLE(name) AdjustAndLoad(name)
HMOD AdjustAndLoad(Firebird::string name) {
HMOD AdjustAndLoad(Firebird::PathName name) {
/**************************************
*
* A d j u s t A n d L o a d ( W I N _ N T )
@ -615,10 +611,10 @@ HMOD AdjustAndLoad(Firebird::string name) {
* name, to provide stable behaviour (implicit .DLL suffix).
*
**************************************/
Firebird::string suffix = name.length() > 4 ?
name.substr(name.length() - 4, 4) : Firebird::string("");
if (stricmp(suffix.c_str(), ".dll") != 0) {
if (name.substr(name.length() - 1, 1) != ".") {
Firebird::PathName suffix = name.length() > 4 ?
name.substr(name.length() - 4, 4) : Firebird::PathName("");
if (suffix != ".dll") {
if (name[name.length() - 1] != '.') {
name += '.';
}
}

View File

@ -595,8 +595,7 @@ static DWORD ShortToLongPathName(
const char sep = '\\';
const char colon = ':';
// Make some short type aliases
typedef Firebird::string tstring;
typedef tstring::traits_type traits;
typedef Firebird::PathName tstring;
typedef tstring::size_type size;
size const npos = tstring::npos;
@ -683,7 +682,7 @@ static DWORD ShortToLongPathName(
// The file was found - replace the short name with the long.
size old_len = (npos == right) ? path.length() - left : right - left;
size new_len = traits::length(fd.cFileName);
size new_len = strlen(fd.cFileName);
path.replace(left, old_len, fd.cFileName, new_len);
// More to do?
@ -707,7 +706,7 @@ static DWORD ShortToLongPathName(
return path.length() + 1;
// Copy the buffer and return the number of characters copied.
traits::copy(lpszLongPath, path.c_str(), path.length() + 1);
memcpy(lpszLongPath, path.c_str(), path.length() + 1);
return path.length();
}

View File

@ -29,6 +29,8 @@
#include "fb_types.h"
#include "fb_string.h"
#include "../jrd/os/path_utils.h"
/**
Since the original (isc.cpp) code wasn't able to provide powerful and
easy-to-use abilities to work with complex configurations, a decision
@ -49,7 +51,8 @@
class ConfigRoot
{
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
public:
ConfigRoot();
@ -61,7 +64,7 @@ protected:
const char *getConfigFile() const;
private:
string root_dir;
string root_dir;
// copy prohibition
ConfigRoot(const ConfigRoot&);

View File

@ -61,7 +61,7 @@ public:
It is the callers responsibility to delete the returned module object
when it is no longer needed.
**/
static Module *loadModule(const Firebird::string&);
static Module *loadModule(const Firebird::PathName&);
/** doctorModuleExtention modifies the given path name to add the platform
specific module extention. This allows the user to provide the root name
@ -69,13 +69,13 @@ public:
host operating system. This function is typically called after a failed attempt
to load the module without the extention.
**/
static void doctorModuleExtention(Firebird::string&);
static void doctorModuleExtention(Firebird::PathName&);
/** isLoadableModule checks the given file to see if it is a loadable
module. This function is required because different operating
systems require different checks.
**/
static bool isLoadableModule(const Firebird::string&);
static bool isLoadableModule(const Firebird::PathName&);
};
#endif

View File

@ -2,6 +2,7 @@
#define PATH_UTILS_H
#include "fb_string.h"
#include "../common/classes/alloc.h"
/** This is a utility class that provides a platform independent way to do some
@ -18,7 +19,7 @@ public:
static const char dir_sep;
/// String used to point to parent directory
static const Firebird::string up_dir_link;
static const char* up_dir_link;
/** An abstract base class for iterating through the contents of a directory.
Instances of this class are created using the newDirItr method of
@ -31,7 +32,7 @@ public:
public:
/// The constructor requires a string that is the path of the
/// directory being iterater.
dir_iterator(const Firebird::string& p) : dirPrefix(p) {}
dir_iterator(const Firebird::PathName& p) : dirPrefix(p) {}
/// destructor provided for memory cleanup.
virtual ~dir_iterator() {}
@ -44,7 +45,7 @@ public:
/// item in the iteration. This path is prefixed with the path of
/// the directory. If the last element of the path is wanted use
/// PathUtils::splitLastComponent on the result of this function.
virtual const Firebird::string& operator*() = 0;
virtual const Firebird::PathName& operator*() = 0;
/// Tests if the iterator has reached the end of the iteration.
/// It is implemented in such a way to make the following for loop
@ -53,7 +54,7 @@ public:
protected:
/// Stores the path to the directory as given in the constructor.
const Firebird::string dirPrefix;
const Firebird::PathName dirPrefix;
private:
/// default constructor not allowed.
@ -70,25 +71,25 @@ public:
For example, the path 'firebird/bin' is a relative path in unix while
the path '/opt/firebird/bin' is not.
**/
static bool isRelative(const Firebird::string& path);
static bool isRelative(const Firebird::PathName& path);
/** isSymLink returns true if the given path is symbolic link, and false if not.
Use of this links may provide way to override system security.
Example: ln -s /usr/firebird/ExternalTables/mytable /etc/xinet.d/remoteshell
and insert desired rows into mytable.
**/
static bool isSymLink(const Firebird::string& path);
static bool isSymLink(const Firebird::PathName& path);
/** canAccess returns true if the given path can be accessed
by this process. mode - like in ACCESS(2).
**/
static bool canAccess(const Firebird::string& path, int mode);
static bool canAccess(const Firebird::PathName& path, int mode);
/** comparePaths returns true if two given paths
point to the same place in FileSystem.
**/
static bool comparePaths(const Firebird::string& path1,
const Firebird::string& path2);
static bool comparePaths(const Firebird::PathName& path1,
const Firebird::PathName& path2);
/** Concatenates the two paths given in the second and third parameters,
and writes the resulting path into the first parameter. The
@ -98,8 +99,8 @@ public:
a simple string concatination of the arguments with the directory
separator character.
**/
static void concatPath(Firebird::string&, const Firebird::string&,
const Firebird::string&);
static void concatPath(Firebird::PathName&, const Firebird::PathName&,
const Firebird::PathName&);
/** splitLastComponent takes a path as the third argument and
removes the last component in that path (usually a file or directory name).
@ -108,8 +109,8 @@ public:
If the input path has only one component that component is returned in the
second parameter and the first parameter is set to the empty string.
**/
static void splitLastComponent(Firebird::string&, Firebird::string&,
const Firebird::string&);
static void splitLastComponent(Firebird::PathName&, Firebird::PathName&,
const Firebird::PathName&);
/** This is the factory method for allocating dir_iterator objects.
It takes a reference to a memory pool to use for all heap allocations,
@ -118,7 +119,7 @@ public:
All errors result in either exceptions being thrown, or a valid empty
dir_iterator being returned.
**/
static dir_iterator* newDirItr(MemoryPool&, const Firebird::string&);
static dir_iterator* newDirItr(MemoryPool&, const Firebird::PathName&);
};
#endif

View File

@ -31,7 +31,8 @@
#include "../jrd/os/path_utils.h"
#include "../utilities/install/registry.h"
typedef Firebird::string string;
// config_file works with OS case-sensitivity
typedef Firebird::PathName string;
static const char *CONFIG_FILE = "firebird.conf";

View File

@ -7,6 +7,7 @@
#include <windows.h>
typedef Firebird::string string;
typedef Firebird::PathName PathName;
/// This is the Win32 implementation of the mod_loader abstraction.
@ -21,7 +22,7 @@ private:
HMODULE module;
};
bool ModuleLoader::isLoadableModule(const string& module)
bool ModuleLoader::isLoadableModule(const PathName& module)
{
LPCSTR pszName = module.c_str();
HINSTANCE hMod = LoadLibraryEx(pszName, 0, LOAD_LIBRARY_AS_DATAFILE);
@ -31,15 +32,15 @@ bool ModuleLoader::isLoadableModule(const string& module)
return hMod != 0;
}
void ModuleLoader::doctorModuleExtention(Firebird::string& name)
void ModuleLoader::doctorModuleExtention(Firebird::PathName& name)
{
string::size_type pos = name.rfind(".dll");
if (pos != string::npos && pos == name.length() - 4)
PathName::size_type pos = name.rfind(".dll");
if (pos != PathName::npos && pos == name.length() - 4)
return;
name += ".dll";
}
ModuleLoader::Module *ModuleLoader::loadModule(const Firebird::string& modPath)
ModuleLoader::Module *ModuleLoader::loadModule(const Firebird::PathName& modPath)
{
HMODULE module = GetModuleHandle(modPath.c_str());
if (!module)

View File

@ -5,28 +5,28 @@
/// The Win32 implementation of the path_utils abstraction.
const char PathUtils::dir_sep = '\\';
const Firebird::string PathUtils::up_dir_link = "..";
const char* PathUtils::up_dir_link = "..";
class Win32DirItr : public PathUtils::dir_iterator
{
public:
Win32DirItr(const Firebird::string&);
Win32DirItr(const Firebird::PathName&);
~Win32DirItr();
const PathUtils::dir_iterator& operator++();
const Firebird::string& operator*() { return file; }
const Firebird::PathName& operator*() { return file; }
operator bool() { return !done; }
private:
HANDLE dir;
WIN32_FIND_DATA fd;
Firebird::string file;
Firebird::PathName file;
bool done;
};
Win32DirItr::Win32DirItr(const Firebird::string& path)
Win32DirItr::Win32DirItr(const Firebird::PathName& path)
: dir_iterator(path), dir(0), done(false)
{
Firebird::string dirPrefix2 = dirPrefix;
Firebird::PathName dirPrefix2 = dirPrefix;
if (dirPrefix.length() && dirPrefix[dirPrefix.length()-1] != PathUtils::dir_sep)
dirPrefix2 = dirPrefix2 + PathUtils::dir_sep;
@ -61,22 +61,22 @@ const PathUtils::dir_iterator& Win32DirItr::operator++()
return *this;
}
PathUtils::dir_iterator *PathUtils::newDirItr(MemoryPool& p, const Firebird::string& path)
PathUtils::dir_iterator *PathUtils::newDirItr(MemoryPool& p, const Firebird::PathName& path)
{
return FB_NEW(p) Win32DirItr(path);
}
void PathUtils::splitLastComponent(Firebird::string& path, Firebird::string& file,
const Firebird::string& orgPath)
void PathUtils::splitLastComponent(Firebird::PathName& path, Firebird::PathName& file,
const Firebird::PathName& orgPath)
{
Firebird::string::size_type pos;
Firebird::PathName::size_type pos;
pos = orgPath.rfind(PathUtils::dir_sep);
if (pos == Firebird::string::npos)
if (pos == Firebird::PathName::npos)
{
pos = orgPath.rfind('/'); // temp hack to make it work with paths,
// not exoanded by ISC_expand_filename
if (pos == Firebird::string::npos)
if (pos == Firebird::PathName::npos)
{
path = "";
file = orgPath;
@ -90,9 +90,9 @@ void PathUtils::splitLastComponent(Firebird::string& path, Firebird::string& fil
file.append(orgPath, pos+1, orgPath.length() - pos - 1);
}
void PathUtils::concatPath(Firebird::string& result,
const Firebird::string& first,
const Firebird::string& second)
void PathUtils::concatPath(Firebird::PathName& result,
const Firebird::PathName& first,
const Firebird::PathName& second)
{
if (second.length() == 0)
{
@ -122,7 +122,7 @@ void PathUtils::concatPath(Firebird::string& result,
result = first + second;
}
bool PathUtils::isRelative(const Firebird::string& path)
bool PathUtils::isRelative(const Firebird::PathName& path)
{
if (path.length() > 0) {
char ds = path[0];
@ -139,16 +139,16 @@ bool PathUtils::isRelative(const Firebird::string& path)
return true;
}
bool PathUtils::isSymLink(const Firebird::string& path)
bool PathUtils::isSymLink(const Firebird::PathName& path)
{
return false;
}
bool PathUtils::comparePaths(const Firebird::string& path1,
const Firebird::string& path2) {
bool PathUtils::comparePaths(const Firebird::PathName& path1,
const Firebird::PathName& path2) {
return stricmp(path1.c_str(), path2.c_str()) == 0;
}
bool PathUtils::canAccess(const Firebird::string& path, int mode) {
bool PathUtils::canAccess(const Firebird::PathName& path, int mode) {
return _access(path.c_str(), mode) == 0;
}

View File

@ -11,7 +11,7 @@
#include "../jrd/os/path_utils.h"
#include "../jrd/gds_proto.h"
PluginManager::Plugin PluginManager::findPlugin(const Firebird::string &name)
PluginManager::Plugin PluginManager::findPlugin(const Firebird::PathName &name)
{
for (Module *itr = moduleList; itr; itr = itr->next)
{
@ -49,8 +49,8 @@ void PluginManager::loadAllPlugins()
Firebird::list<Path>::iterator pathItr;
char fb_lib_path[MAXPATHLEN];
gds__prefix(fb_lib_path, "");
Firebird::string fbLibPath(fb_lib_path);
Firebird::string checkDir;
Firebird::PathName fbLibPath(fb_lib_path);
Firebird::PathName checkDir;
for (pathItr = searchPaths.begin(); pathItr != searchPaths.end(); ++pathItr)
{
@ -80,9 +80,9 @@ void PluginManager::loadAllPlugins()
// Check to see if the module has been explicitly excluded from loading
if (!alreadyLoaded && ignoreModules.size() > 0)
{
Firebird::string pathComponent, modName;
Firebird::PathName pathComponent, modName;
PathUtils::splitLastComponent(pathComponent, modName, **dirItr);
for (Firebird::list<Firebird::string>::iterator itr2 = ignoreModules.begin();
for (Firebird::list<Firebird::PathName>::iterator itr2 = ignoreModules.begin();
itr2 != ignoreModules.end(); ++itr2)
{
if (modName == *itr2)
@ -112,12 +112,12 @@ void PluginManager::loadAllPlugins()
}
}
PluginManager::Module *PluginManager::loadPluginModule(const Firebird::string& name)
PluginManager::Module *PluginManager::loadPluginModule(const Firebird::PathName& name)
{
char fb_lib_path[MAXPATHLEN];
gds__prefix(fb_lib_path, "");
Firebird::string fbLibPath(fb_lib_path);
Firebird::string checkPath;
Firebird::PathName fbLibPath(fb_lib_path);
Firebird::PathName checkPath;
Firebird::list<Path>::iterator itr;
// Check to see if the module name was specified as a relative path
@ -160,18 +160,18 @@ PluginManager::Module *PluginManager::loadPluginModule(const Firebird::string& n
{
for (itr = searchPaths.begin(); itr != searchPaths.end(); ++itr)
{
Firebird::string::size_type pos = 0;
Firebird::string::size_type checkPos;
Firebird::PathName::size_type pos = 0;
Firebird::PathName::size_type checkPos;
if (itr->second) // use fb path prefix
{
checkPos = name.find(fbLibPath, pos);
if (checkPos == Firebird::string::npos || checkPos != pos)
if (checkPos == Firebird::PathName::npos || checkPos != pos)
continue; // The fb path prefix isn't a prefix for this module. Opps.
pos += fbLibPath.length();
}
checkPos = name.find(itr->first, pos);
if (checkPos == Firebird::string::npos || checkPos != pos)
if (checkPos == Firebird::PathName::npos || checkPos != pos)
continue; // The search path isn't a prefix for this module. Opps.
// OK, the module has the correct prefix path, lets try to load it.
if (ModuleLoader::isLoadableModule(name))
@ -194,12 +194,12 @@ PluginManager::Module *PluginManager::loadPluginModule(const Firebird::string& n
return 0;
}
PluginManager::Module *PluginManager::loadBuiltinModule(const Firebird::string& name)
PluginManager::Module *PluginManager::loadBuiltinModule(const Firebird::PathName& name)
{
return 0;
}
void PluginManager::addSearchPath(const Firebird::string& path, bool isFBRelative)
void PluginManager::addSearchPath(const Firebird::PathName& path, bool isFBRelative)
{
Firebird::list<Path>::iterator itr;
@ -212,7 +212,7 @@ void PluginManager::addSearchPath(const Firebird::string& path, bool isFBRelativ
searchPaths.push_back(Path(path, isFBRelative));
}
void PluginManager::removeSearchPath(const Firebird::string& path, bool isFBRelative)
void PluginManager::removeSearchPath(const Firebird::PathName& path, bool isFBRelative)
{
Firebird::list<Path>::iterator itr;

View File

@ -71,7 +71,7 @@ private:
/// The constructor requires the name of the module. The initial reference count
/// is 1, to indicate that the PluginManager itself has a reference. This is
/// verified in debug builds in the PluginManager destructor.
Module(const Firebird::string& name)
Module(const Firebird::PathName& name)
: refCnt(1), module_name(name), prev(0), next(0) {}
/// The destructor is responsible for removing the module from the linked
/// list.
@ -84,7 +84,7 @@ private:
/// must be balanced with a call to release.
void release() { refCnt--; }
/// Accessor function that returns the name of the module
const Firebird::string& name() const { return module_name; }
const Firebird::PathName& name() const { return module_name; }
/// lookupSymbol searches through a module's symbol table and attempts to
/// locate the given symbol name. If successful it returns a pointer to
/// the symbol's location in the current address space. If the symbol can't
@ -95,7 +95,7 @@ private:
friend class PluginManager;
friend class iterator;
int refCnt;
Firebird::string module_name;
Firebird::PathName module_name;
Module **prev;
Module *next;
@ -177,19 +177,19 @@ public:
/// Searches for the plugin with the given name using the
/// preset search criteria.
Plugin findPlugin(const Firebird::string&);
Plugin findPlugin(const Firebird::PathName&);
/// Adds a path to the list of paths to be searched for a modules. The
/// second parameter indicates if the path is absolute, or relative to
/// the current working directory.
void addSearchPath(const Firebird::string&, bool = true);
void addSearchPath(const Firebird::PathName&, bool = true);
/// Removes the given search path from the list of search paths. The path name
/// (1st parameter) and the relative indicator (2nd parameter) must exactly match
/// those passed in to addSearchPath.
void removeSearchPath(const Firebird::string&, bool = true);
void removeSearchPath(const Firebird::PathName&, bool = true);
/// Adds a module to the ignore list. Ignored modules are not loaded automatically
/// when a directory is scanned, but may be loaded manually via the findPlugin
/// function.
void addIgnoreModule(const Firebird::string &mod)
void addIgnoreModule(const Firebird::PathName &mod)
{ ignoreModules.push_back(mod); }
/// Loads all the plugins found in the current set of search paths, except those that
/// have been ignored. This function must be called after adding all the needed search paths,
@ -207,18 +207,18 @@ public:
private:
typedef void (*engineRegistrationFuncType)(Plugin*);
typedef std::pair<Firebird::string, bool> Path;
typedef std::pair<Firebird::PathName, bool> Path;
Module *moduleList;
Firebird::list<Path> searchPaths;
Firebird::list<Firebird::string> ignoreModules;
Firebird::list<Firebird::PathName> ignoreModules;
Module *loadPluginModule(const Firebird::string& name);
Module *loadBuiltinModule(const Firebird::string& name);
Module *loadPluginModule(const Firebird::PathName& name);
Module *loadBuiltinModule(const Firebird::PathName& name);
class BuiltinModule : public Module
{
public:
BuiltinModule(const Firebird::string& name) : Module(name) {}
BuiltinModule(const Firebird::PathName& name) : Module(name) {}
private:
Firebird::map<Firebird::string, void*> symbols;
@ -229,7 +229,7 @@ private:
class PluginModule : public Module
{
public:
PluginModule(const Firebird::string &name, ModuleLoader::Module *mod)
PluginModule(const Firebird::PathName &name, ModuleLoader::Module *mod)
: Module(name), module(mod) {}
private: