File: posixSystem.cpp

package info (click to toggle)
contextfree 3.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,420 kB
  • sloc: cpp: 37,078; lex: 417; makefile: 124; sh: 42; python: 41
file content (276 lines) | stat: -rw-r--r-- 7,854 bytes parent folder | download | duplicates (3)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// posixSystem.cpp
// Context Free
// ---------------------
// Copyright (C) 2005-2007 Mark Lentczner - markl@glyphic.com
// Copyright (C) 2007-2013 John Horigan - john@glyphic.com
//
// 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.
// 
// Mark Lentczner can be contacted at markl@glyphic.com or at
// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#include "posixSystem.h"

#ifdef __APPLE__
// Linking with libicucore, not full libicu
#define U_DISABLE_RENAMING 1
#endif

#define UCHAR_TYPE char16_t
#include <unicode/ucnv.h>
#include <unicode/unorm2.h>

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>

#if defined(__GNU__) || (defined(__ILP32__) && defined(__x86_64__))
  #define NOSYSCTL
#else
  #ifndef __linux__
    #include <sys/sysctl.h>
  #endif
#endif
#include <sys/types.h>
#include <unistd.h>
#include <cstdint>

using std::cerr;
using std::endl;

#include "fdostream.h"

void
PosixSystem::clearAndCR()
{
    static const char* EraseEndofLine = "\x1b[K";
    cerr << EraseEndofLine << '\r';
}

void
PosixSystem::catastrophicError(const char* what)
{
    cerr << "\n\nUnexpected error: " << what << endl;
    std::exit(33);
}

const AbstractSystem::FileChar*
PosixSystem::tempFileDirectory()
{
    struct stat sb;
    const char *                                              tmpenv = getenv("TMPDIR");
    if (!tmpenv || stat(tmpenv, &sb) || !S_ISDIR(sb.st_mode)) tmpenv = getenv("TEMP");
    if (!tmpenv || stat(tmpenv, &sb) || !S_ISDIR(sb.st_mode)) tmpenv = getenv("TMP");
    if (!tmpenv || stat(tmpenv, &sb) || !S_ISDIR(sb.st_mode)) tmpenv = "/tmp/"; // give up
    return tmpenv;
}

namespace {
    struct MallocDeleter {
        void operator()(void* ptr) const {
            std::free(ptr);
        }
    };
    struct DirCloser {
        void operator()(DIR* ptr) const {
            (void)closedir(ptr);    // not called if nullptr
        }
    };
}

AbstractSystem::ostr_ptr
PosixSystem::tempFileForWrite(AbstractSystem::TempType tt, FileString& nameOut)
{
    std::string t(tempFileDirectory());
    if (t.back() != '/')
        t.push_back('/');
    t.append(TempPrefixes[tt]);
    t.append("XXXXXX");
    t.append(TempSuffixes[tt]);
    
    ostr_ptr f;
    
    std::unique_ptr<char, MallocDeleter> b(strdup(t.c_str()));
    int tfd = mkstemps(b.get(), (int)std::strlen(TempSuffixes[tt]));
    if (tfd != -1) {
        f = std::make_unique<boost::fdostream>(tfd);
        nameOut.assign(b.get());
    }
    
    return f;
}

std::string
PosixSystem::relativeFilePath(const std::string& base, const std::string& rel)
{
    std::string s = base;
    
    std::string::size_type i = s.rfind('/');
    if (i == std::string::npos) {
        return rel;
    }
    i += 1;
    s.replace(i, s.length() - i, rel);
    return s;
}

int
PosixSystem::deleteTempFile(const FileString& name)
{
    return remove(name.c_str());
}

std::vector<AbstractSystem::FileString>
PosixSystem::findTempFiles()
{
    std::vector<FileString> ret;
    const char* dirname = tempFileDirectory();
    std::size_t len = std::strlen(TempPrefixAll);
    std::unique_ptr<DIR, DirCloser> dirp(opendir(dirname));
    if (!dirp) return ret;
    while (dirent* der = readdir(dirp.get())) {
        if (std::strncmp(TempPrefixAll, der->d_name, len) == 0) {
            ret.emplace_back(dirname);
            if (ret.back().back() != '/')
                ret.back().push_back('/');
            ret.back().append(der->d_name);
        }
    }
    
    return ret;
}

std::size_t
PosixSystem::getPhysicalMemory()
{
#ifdef NOSYSCTL
    return 0;
#elif defined(__linux__)
  #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
    std::uint64_t size = sysconf(_SC_PHYS_PAGES) * static_cast<std::uint64_t>(sysconf(_SC_PAGESIZE));
    if (size > MaximumMemory)
        size = MaximumMemory;
    return static_cast<std::size_t>(size);
  #else
    return 0;
  #endif
#else // not __linux__ and not NOSYSCTL
    int mib[2];
  #ifdef CTL_HW
    mib[0] = CTL_HW;
  #else
    return 0;
  #endif
  #if defined(HW_MEMSIZE)
    mib[1] = HW_MEMSIZE;    // OSX
    std::uint64_t size = 0;      // 64-bit
  #elif defined(HW_PHYSMEM64)
    mib[1] = HW_PHYSMEM64;  // NetBSD, OpenBSD
    std::uint64_t size = 0;      // 64-bit
  #elif defined(HW_REALMEM)
    mib[1] = HW_REALMEM;    // FreeBSD
    unsigned int size = 0;  // 32-bit
  #elif defined(HW_PHYSMEM)
    mib[1] = HW_PHYSMEM;    // DragonFly BSD
    unsigned int size = 0;  // 32-bit
  #else
    std::uint64_t size = 0;      // need to define this anyway
    return 0;
  #endif
    std::size_t len = sizeof(size);
    if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) {
        if (size > MaximumMemory)
            size = MaximumMemory;
        return static_cast<std::size_t>(size);
    }
    return 0;
#endif // __linux__ || NOSYSCTL
}

PosixSystem::PosixSystem()
: mConverter(nullptr), mNormalizer(nullptr), mErrorReported(false)
{
}

PosixSystem::~PosixSystem()
{
    if (mConverter)
        ucnv_close(mConverter);
}

std::wstring
PosixSystem::normalize(const std::string& u8name)
{
    // Setup ICU library items
    UErrorCode status = U_ZERO_ERROR;
    if (!mConverter)
        mConverter = ucnv_open("utf-8", &status);
    if (!mNormalizer && U_SUCCESS(status))
        mNormalizer = unorm2_getNFKCInstance(&status);
    if (!mConverter || !mNormalizer) {
        if (!mErrorReported)
            catastrophicError("String conversion initialization error");
        mErrorReported = true;
        return std::wstring();
    }
    
    // Convert from utf-8 to utf-16
    std::u16string u16name(u8name.length(), L' ');
    for (;;) {
        auto sz = ucnv_toUChars(mConverter,
                                &u16name[0], static_cast<int32_t>(u16name.length()+1),
                                u8name.data(), static_cast<int32_t>(u8name.length()),
                                &status);
        if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
            CfdgError::Error(CfdgError::Default, "String conversion error");
        }
        if (sz <= static_cast<int32_t>(u16name.length())) {
            u16name.resize(sz);
            break;
        } else {
            u16name.resize(sz + 1, L' ');
        }
    }
    
    // NFKC normalize utf-16 text 
    std::wstring ret(u16name.length(), L' ');
    for (;;) {
        UChar* dest = reinterpret_cast<UChar*>(&ret[0]);
        auto sz = unorm2_normalize(mNormalizer,
                                   u16name.data(), static_cast<int32_t>(u16name.length()),
                                   dest, static_cast<int32_t>(ret.length()+1),
                                   &status);
        if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
            CfdgError::Error(CfdgError::Default, "String conversion error");
        }
        if (sz <= static_cast<int32_t>(ret.length())) {
            ret.resize(sz);
            break;
        } else {
            ret.resize(sz + 1, L' ');
        }
    }
    return ret;
}