-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.cpp
107 lines (82 loc) · 2.77 KB
/
Syntax.cpp
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
/* Cycript - The Truly Universal Scripting Language
* Copyright (C) 2009-2016 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <cmath>
#include <sstream>
#include "Syntax.hpp"
// XXX: this implementation will break if value[size] is a digit
double CYCastDouble(const char *value, size_t size) {
char *end;
double number(strtod(value, &end));
if (end != value + size)
return NAN;
return number;
}
double CYCastDouble(const char *value) {
return CYCastDouble(value, strlen(value));
}
double CYCastDouble(CYUTF8String value) {
return CYCastDouble(value.data, value.size);
}
template <>
::pthread_key_t CYLocal<CYPool>::key_ = Key_();
CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
// XXX: this really should not be here ... :/
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "String.hpp"
struct CYFile {
void *data_;
size_t size_;
CYFile(void *data, size_t size) :
data_(data),
size_(size)
{
}
};
static void CYFileExit(void *data) {
CYFile *file(reinterpret_cast<CYFile *>(data));
_syscall(munmap(file->data_, file->size_));
}
void *CYPoolFile(CYPool &pool, const char *path, size_t *psize) {
int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT));
if (fd == -1)
return NULL;
struct stat stat;
_syscall(fstat(fd, &stat));
size_t size(stat.st_size);
*psize = size;
void *base;
if (size == 0)
base = pool.strndup("", 0);
else {
_syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
CYFile *file(new (pool) CYFile(base, size));
pool.atexit(&CYFileExit, file);
}
_syscall(close(fd));
return base;
}
CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path) {
CYUTF8String data;
data.data = reinterpret_cast<char *>(CYPoolFile(pool, path, &data.size));
return data;
}