62pm_string_mapped_init(
pm_string_t *
string,
const char *filepath) {
65 HANDLE file = CreateFile(filepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
67 if (file == INVALID_HANDLE_VALUE) {
68 perror(
"CreateFile failed");
73 DWORD file_size = GetFileSize(file, NULL);
74 if (file_size == INVALID_FILE_SIZE) {
76 perror(
"GetFileSize failed");
84 const uint8_t source[] =
"";
85 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
90 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
91 if (mapping == NULL) {
93 perror(
"CreateFileMapping failed");
98 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
102 if (source == NULL) {
103 perror(
"MapViewOfFile failed");
107 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
111 int fd = open(filepath, O_RDONLY);
119 if (fstat(fd, &sb) == -1) {
126 size_t size = (size_t) sb.st_size;
127 uint8_t *source = NULL;
131 const uint8_t source[] =
"";
132 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
136 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
137 if (source == MAP_FAILED) {
138 perror(
"Map failed");
143 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };