-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
63 lines (53 loc) · 1.23 KB
/
util.c
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
// vim: ts=4 sw=4 noet cc=80
#include <stdio.h>
#include <stdlib.h>
#include <epoxy/gl.h>
#include <GLFW/glfw3.h>
#include "util.h"
// Read file into char pointer
char *readfile(const char file[])
{
// Open the file
FILE *fp = fopen(file, "r");
if (!fp) {
perror("ERROR");
fprintf(stderr, "Failed to open file: %s\n", file);
return NULL;
}
// Get file size
if (fseek(fp, 0L, SEEK_END)) {
perror("ERROR");
fprintf(stderr, "Failed to seek end of file: %s\n", file);
fclose(fp);
return NULL;
}
long filesize = ftell(fp);
rewind(fp);
// Allocate memory for the file content
char *content = (char *)malloc(filesize + 1);
if (!content) {
perror("ERROR");
fprintf(stderr, "Failed to allocate memory for file: %s\n", file);
fclose(fp);
return NULL;
}
// Read file into char pointer
if (!fread(content, 1, filesize, fp)) {
perror("ERROR");
fprintf(stderr, "Failed to read file: %s\n", file);
fclose(fp);
free(content);
return NULL;
}
// Close the file
fclose(fp);
// Null-terminate the file content
content[filesize] = '\0';
return content;
}
void print_glfw_version(void)
{
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
printf("Running GLFW %d.%d.%d\n", major, minor, revision);
}