summaryrefslogtreecommitdiff
path: root/zen/zstring.h
blob: f4a79181bb1cacc9b799861afc542eab797cb6cb (plain)
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
140
141
142
143
144
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef ZSTRING_H_INCLUDED
#define ZSTRING_H_INCLUDED

#include "string_base.h"
#ifdef FFS_LINUX
#include <cstring> //strcmp
#elif defined FFS_MAC
#include <strings.h> //strcasecmp
#endif


#ifndef NDEBUG
namespace z_impl
{
void leakCheckerInsert(const void* ptr, size_t size);
void leakCheckerRemove(const void* ptr);
}
#endif //NDEBUG

class AllocatorFreeStoreChecked
{
public:
    static void* allocate(size_t size) //throw std::bad_alloc
    {
        void* newMem = ::operator new(size);
#ifndef NDEBUG
        z_impl::leakCheckerInsert(newMem, size); //test Zbase for memory leaks
#endif
        return newMem;
    }

    static void deallocate(void* ptr)
    {
#ifndef NDEBUG
        z_impl::leakCheckerRemove(ptr); //check for memory leaks
#endif
        ::operator delete(ptr);
    }

    static size_t calcCapacity(size_t length) { return std::max<size_t>(16, length + length / 2); } //exponential growth + min size
};


//############################## helper functions #############################################

#ifdef FFS_WIN //Windows encodes Unicode as UTF-16 wchar_t
typedef wchar_t Zchar;
#define Zstr(x) L ## x
const Zchar FILE_NAME_SEPARATOR = L'\\';

#elif defined FFS_LINUX || defined FFS_MAC //Linux uses UTF-8
typedef char Zchar;
#define Zstr(x) x
const Zchar FILE_NAME_SEPARATOR = '/';
#endif

//"The reason for all the fuss above" - Loki/SmartPtr
//a high-performance string for interfacing with native OS APIs and multithreaded contexts
typedef zen::Zbase<Zchar, zen::StorageRefCountThreadSafe, AllocatorFreeStoreChecked> Zstring;



//Compare filenames: Windows does NOT distinguish between upper/lower-case, while Linux DOES
template <template <class, class> class SP, class AP>
int cmpFileName(const zen::Zbase<Zchar, SP, AP>& lhs, const zen::Zbase<Zchar, SP, AP>& rhs);

struct LessFilename //case-insensitive on Windows, case-sensitive on Linux
{
    template <template <class, class> class SP, class AP>
    bool operator()(const zen::Zbase<Zchar, SP, AP>& lhs, const zen::Zbase<Zchar, SP, AP>& rhs) const { return cmpFileName(lhs, rhs) < 0; }
};

struct EqualFilename //case-insensitive on Windows, case-sensitive on Linux
{
    template <template <class, class> class SP, class AP>
    bool operator()(const zen::Zbase<Zchar, SP, AP>& lhs, const zen::Zbase<Zchar, SP, AP>& rhs) const { return cmpFileName(lhs, rhs) == 0; }
};

#if defined FFS_WIN || defined FFS_MAC
template <template <class, class> class SP, class AP>
void makeUpper(zen::Zbase<Zchar, SP, AP>& str);
#endif

inline
Zstring appendSeparator(Zstring path) //support rvalue references!
{
    return endsWith(path, FILE_NAME_SEPARATOR) ? path : (path += FILE_NAME_SEPARATOR);
}






//################################# inline implementation ########################################
namespace z_impl
{
#if defined FFS_WIN
int compareFilenamesNoCase(const Zchar* lhs, const Zchar* rhs, size_t sizeLhs, size_t sizeRhs);
#endif
#if defined FFS_WIN || defined FFS_MAC
void makeFilenameUpperCase(Zchar* str, size_t size);
#endif
}


template <template <class, class> class SP, class AP> inline
int cmpFileName(const zen::Zbase<Zchar, SP, AP>& lhs, const zen::Zbase<Zchar, SP, AP>& rhs)
{
#if defined FFS_WIN
    return z_impl::compareFilenamesNoCase(lhs.data(), rhs.data(), lhs.length(), rhs.length());
#elif defined FFS_LINUX
    return std::strcmp(lhs.c_str(), rhs.c_str()); //POSIX filenames don't have embedded 0
#elif defined FFS_MAC
    return ::strcasecmp(lhs.c_str(), rhs.c_str()); //locale-dependent!
#endif
}


#if defined FFS_WIN || defined FFS_MAC
template <template <class, class> class SP, class AP> inline
void makeUpper(zen::Zbase<Zchar, SP, AP>& str)
{
    z_impl::makeFilenameUpperCase(str.begin(), str.length());
}
#endif


namespace std
{
template<> inline
void swap(Zstring& rhs, Zstring& lhs)
{
    rhs.swap(lhs);
}
}

#endif //ZSTRING_H_INCLUDED
bgstack15