1#include "prism/internal/source.h"
3#include "prism/internal/allocator.h"
4#include "prism/internal/buffer.h"
5#include "prism/internal/oom.h"
13#elif defined(_POSIX_MAPPED_FILES)
17#elif defined(PRISM_HAS_FILESYSTEM)
22static const uint8_t empty_source[] =
"";
28pm_source_alloc(
const uint8_t *source,
size_t length, pm_source_type_t type) {
30 if (result == NULL) PRISM_OOM();
45pm_source_constant_new(
const uint8_t *data,
size_t length) {
46 return pm_source_alloc(data, length, PM_SOURCE_CONSTANT);
53pm_source_shared_new(
const uint8_t *data,
size_t length) {
54 return pm_source_alloc(data, length, PM_SOURCE_SHARED);
61pm_source_owned_new(uint8_t *data,
size_t length) {
62 return pm_source_alloc(data, length, PM_SOURCE_OWNED);
79} pm_source_file_handle_t;
85pm_source_file_handle_open(pm_source_file_handle_t *handle,
const char *filepath) {
86 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
89 handle->path_size =
sizeof(WCHAR) * ((
size_t) length);
90 handle->path =
xmalloc(handle->path_size);
91 if ((handle->path == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, handle->path, length) == 0)) {
92 xfree_sized(handle->path, handle->path_size);
96 handle->file = CreateFileW(handle->path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
97 if (handle->file == INVALID_HANDLE_VALUE) {
100 if (GetLastError() == ERROR_ACCESS_DENIED) {
101 DWORD attributes = GetFileAttributesW(handle->path);
102 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
107 xfree_sized(handle->path, handle->path_size);
118pm_source_file_handle_close(pm_source_file_handle_t *handle) {
119 xfree_sized(handle->path, handle->path_size);
120 CloseHandle(handle->file);
133 pm_source_file_handle_t handle;
134 *result = pm_source_file_handle_open(&handle, filepath);
138 DWORD file_size = GetFileSize(handle.file, NULL);
139 if (file_size == INVALID_FILE_SIZE) {
140 pm_source_file_handle_close(&handle);
146 if (file_size == 0) {
147 pm_source_file_handle_close(&handle);
149 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
153 HANDLE mapping = CreateFileMapping(handle.file, NULL, PAGE_READONLY, 0, 0, NULL);
154 if (mapping == NULL) {
155 pm_source_file_handle_close(&handle);
161 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
162 CloseHandle(mapping);
163 pm_source_file_handle_close(&handle);
165 if (source == NULL) {
171 return pm_source_alloc(source, (
size_t) file_size, PM_SOURCE_MAPPED);
172#elif defined(_POSIX_MAPPED_FILES)
174 int fd = open(filepath, O_RDONLY | open_flags);
182 if (fstat(fd, &sb) == -1) {
189 if (S_ISDIR(sb.st_mode)) {
199 if (!S_ISREG(sb.st_mode)) {
206 size_t size = (size_t) sb.st_size;
211 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
214 uint8_t *source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
215 if (source == MAP_FAILED) {
223 return pm_source_alloc(source, size, PM_SOURCE_MAPPED);
226 return pm_source_file_new(filepath, result);
237 pm_source_file_handle_t handle;
238 *result = pm_source_file_handle_open(&handle, filepath);
242 const DWORD file_size = GetFileSize(handle.file, NULL);
243 if (file_size == INVALID_FILE_SIZE) {
244 pm_source_file_handle_close(&handle);
250 if (file_size == 0) {
251 pm_source_file_handle_close(&handle);
253 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
257 uint8_t *source =
xmalloc(file_size);
258 if (source == NULL) {
259 pm_source_file_handle_close(&handle);
266 if (!ReadFile(handle.file, source, file_size, &bytes_read, NULL)) {
267 xfree_sized(source, file_size);
268 pm_source_file_handle_close(&handle);
274 if (bytes_read != file_size) {
275 xfree_sized(source, file_size);
276 pm_source_file_handle_close(&handle);
281 pm_source_file_handle_close(&handle);
283 return pm_source_alloc(source, (
size_t) file_size, PM_SOURCE_OWNED);
284#elif defined(PRISM_HAS_FILESYSTEM)
286 int fd = open(filepath, O_RDONLY);
294 if (fstat(fd, &sb) == -1) {
301 if (S_ISDIR(sb.st_mode)) {
308 size_t size = (size_t) sb.st_size;
312 return pm_source_alloc(empty_source, 0, PM_SOURCE_CONSTANT);
315 const size_t length = (size_t) size;
316 uint8_t *source =
xmalloc(length);
317 if (source == NULL) {
323 ssize_t bytes_read = read(fd, source, length);
326 if (bytes_read == -1 || (
size_t) bytes_read != length) {
327 xfree_sized(source, length);
333 return pm_source_alloc(source, length, PM_SOURCE_OWNED);
337 perror(
"pm_source_file_new is not implemented for this platform");
349 pm_source_t *source = pm_source_alloc(NULL, 0, PM_SOURCE_STREAM);
350 source->stream.buffer = pm_buffer_new();
351 source->stream.stream = stream;
352 source->stream.fgets = fgets;
353 source->stream.feof = feof;
354 source->stream.eof =
false;
369#define LINE_SIZE 4096
370 char line[LINE_SIZE];
379#define LINE_FILL 0xff
387 while (memset(line, LINE_FILL, LINE_SIZE), (source->stream.fgets)(line, LINE_SIZE, source->stream.stream) != NULL) {
392 size_t length = LINE_SIZE;
393 while (length > 0 && line[length - 1] !=
'\0') length--;
394 if (length > 0) length--;
406 pm_buffer_append_string(buffer, line, length);
408 bool newline = (line[length - 1] ==
'\n');
418 if (!newline && !(source->stream.feof)(source->stream.stream)) {
430 if (memcmp(line,
"__END__", 7) == 0) {
431 source->source = (
const uint8_t *) pm_buffer_value(buffer);
432 source->length = pm_buffer_length(buffer);
437 if (memcmp(line,
"__END__\n", 8) == 0) {
438 source->source = (
const uint8_t *) pm_buffer_value(buffer);
439 source->length = pm_buffer_length(buffer);
444 if (memcmp(line,
"__END__\r\n", 9) == 0) {
445 source->source = (
const uint8_t *) pm_buffer_value(buffer);
446 source->length = pm_buffer_length(buffer);
465 source->stream.eof =
true;
466 source->source = (
const uint8_t *) pm_buffer_value(buffer);
467 source->length = pm_buffer_length(buffer);
476 return source->stream.eof;
484 switch (source->type) {
485 case PM_SOURCE_CONSTANT:
486 case PM_SOURCE_SHARED:
489 case PM_SOURCE_OWNED:
490 xfree_sized((
void *) source->source, source->length);
492 case PM_SOURCE_MAPPED:
494 if (source->length > 0) {
495 UnmapViewOfFile((
void *) source->source);
497#elif defined(_POSIX_MAPPED_FILES)
498 if (source->length > 0) {
499 munmap((
void *) source->source, source->length);
503 case PM_SOURCE_STREAM:
504 pm_buffer_free(source->stream.buffer);
516 return source->length;
524 return source->source;
#define xmalloc
Old name of ruby_xmalloc.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int() pm_source_stream_feof_t(void *stream)
This function is used to check whether a stream is at EOF.
char *() pm_source_stream_fgets_t(char *string, int size, void *stream)
This function is used to retrieve a line of input from a stream.
pm_source_init_result_t
Represents the result of initializing a source from a file.
@ PM_SOURCE_INIT_ERROR_GENERIC
Indicates a generic error from a source init function, where the type of error should be read from er...
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
@ PM_SOURCE_INIT_ERROR_NON_REGULAR
Indicates that the file is not a regular file (e.g.
@ PM_SOURCE_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.