]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - The Truly Universal Scripting Language | |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* GNU Affero General Public License, Version 3 {{{ */ | |
6 | /* | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU Affero General Public License for more details. | |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
20 | /* }}} */ | |
21 | ||
22 | #include <cmath> | |
23 | #include <sstream> | |
24 | ||
25 | #include "Syntax.hpp" | |
26 | ||
27 | // XXX: this implementation will break if value[size] is a digit | |
28 | double CYCastDouble(const char *value, size_t size) { | |
29 | char *end; | |
30 | double number(strtod(value, &end)); | |
31 | if (end != value + size) | |
32 | return NAN; | |
33 | return number; | |
34 | } | |
35 | ||
36 | double CYCastDouble(const char *value) { | |
37 | return CYCastDouble(value, strlen(value)); | |
38 | } | |
39 | ||
40 | double CYCastDouble(CYUTF8String value) { | |
41 | return CYCastDouble(value.data, value.size); | |
42 | } | |
43 | ||
44 | template <> | |
45 | ::pthread_key_t CYLocal<CYPool>::key_ = Key_(); | |
46 | ||
47 | CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 | |
48 | CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ | |
49 | CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 | |
50 | ||
51 | ||
52 | ||
53 | // XXX: this really should not be here ... :/ | |
54 | ||
55 | #include <sys/mman.h> | |
56 | #include <sys/stat.h> | |
57 | #include <fcntl.h> | |
58 | #include <unistd.h> | |
59 | ||
60 | #include "String.hpp" | |
61 | ||
62 | struct CYFile { | |
63 | void *data_; | |
64 | size_t size_; | |
65 | ||
66 | CYFile(void *data, size_t size) : | |
67 | data_(data), | |
68 | size_(size) | |
69 | { | |
70 | } | |
71 | }; | |
72 | ||
73 | static void CYFileExit(void *data) { | |
74 | CYFile *file(reinterpret_cast<CYFile *>(data)); | |
75 | _syscall(munmap(file->data_, file->size_)); | |
76 | } | |
77 | ||
78 | void *CYPoolFile(CYPool &pool, const char *path, size_t *psize) { | |
79 | int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT)); | |
80 | if (fd == -1) | |
81 | return NULL; | |
82 | ||
83 | struct stat stat; | |
84 | _syscall(fstat(fd, &stat)); | |
85 | size_t size(stat.st_size); | |
86 | ||
87 | *psize = size; | |
88 | ||
89 | void *base; | |
90 | if (size == 0) | |
91 | base = pool.strndup("", 0); | |
92 | else { | |
93 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
94 | ||
95 | CYFile *file(new (pool) CYFile(base, size)); | |
96 | pool.atexit(&CYFileExit, file); | |
97 | } | |
98 | ||
99 | _syscall(close(fd)); | |
100 | return base; | |
101 | } | |
102 | ||
103 | CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path) { | |
104 | CYUTF8String data; | |
105 | data.data = reinterpret_cast<char *>(CYPoolFile(pool, path, &data.size)); | |
106 | return data; | |
107 | } |