Browse Source

Return mtime from get_contents as well

Lucas Stadler 9 years ago
parent
commit
b386294fdc
1 changed files with 19 additions and 14 deletions
  1. 19 14
      c/ton/main.c

+ 19 - 14
c/ton/main.c

@ -6,6 +6,7 @@
6 6
#include <stdlib.h>
7 7
#include <string.h>
8 8
#include <sys/stat.h>
9
#include <time.h>
9 10
10 11
#include <JavaScriptCore/JavaScript.h>
11 12
@ -27,7 +28,7 @@ char *munge(char *s);
27 28
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
28 29
JSObjectRef get_function(JSContextRef ctx, char *namespace, char *name);
29 30
30
char* get_contents(char *path);
31
char* get_contents(char *path, time_t *last_modified);
31 32
void write_contents(char *path, char *contents);
32 33
int mkdir_p(char *path);
33 34
@ -97,14 +98,15 @@ JSValueRef function_read_file(JSContextRef ctx, JSObjectRef function, JSObjectRe
97 98
		// TODO: should not load from here?
98 99
		snprintf(full_path, 150, "%s/%s", "out", path);
99 100
100
		char *contents = get_contents(full_path);
101
		time_t last_modified = 0;
102
		char *contents = get_contents(full_path, &last_modified);
101 103
		if (contents != NULL) {
102 104
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
103 105
			free(contents);
104 106
105 107
			JSValueRef res[2];
106 108
			res[0] = JSValueMakeString(ctx, contents_str);
107
			res[1] = JSValueMakeNumber(ctx, 0);
109
			res[1] = JSValueMakeNumber(ctx, last_modified);
108 110
			return JSObjectMakeArray(ctx, 2, res, NULL);
109 111
		}
110 112
	}
@ -131,18 +133,17 @@ JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thi
131 133
132 134
		JSValueRef contents_val = NULL;
133 135
134
		char *contents = get_contents(full_path);
136
		time_t last_modified = 0;
137
		char *contents = get_contents(full_path, &last_modified);
135 138
		if (contents != NULL) {
136 139
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
137 140
			free(contents);
138 141
139
			contents_val = JSValueMakeString(ctx, contents_str);
142
			JSValueRef res[2];
143
			res[0] = JSValueMakeString(ctx, contents_str);;
144
			res[1] = JSValueMakeNumber(ctx, last_modified);
145
			return JSObjectMakeArray(ctx, 2, res, NULL);
140 146
		}
141
142
		JSValueRef res[2];
143
		res[0] = contents_val;
144
		res[1] = JSValueMakeNumber(ctx, 0);
145
		return JSObjectMakeArray(ctx, 2, res, NULL);
146 147
	}
147 148
148 149
	return JSValueMakeNull(ctx);
@ -272,7 +273,7 @@ JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObje
272 273
273 274
		char full_path[150];
274 275
		snprintf(full_path, 150, "%s/%s", "out", path);
275
		char *buf = get_contents(full_path);
276
		char *buf = get_contents(full_path, NULL);
276 277
		if (buf == NULL) {
277 278
			goto err;
278 279
		}
@ -653,7 +654,7 @@ void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path) {
653 654
	evaluate_script(ctx, "CLOSURE_IMPORT_SCRIPT = function(src) { IMPORT_SCRIPT('goog/' + src); return true; }", source);
654 655
655 656
	// Load goog base
656
	char *base_script_str = get_contents(goog_base_path);
657
	char *base_script_str = get_contents(goog_base_path, NULL);
657 658
	if (base_script_str == NULL) {
658 659
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
659 660
		exit(1);
@ -662,7 +663,7 @@ void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path) {
662 663
	free(base_script_str);
663 664
664 665
	// Load the deps file
665
	char *deps_script_str = get_contents(deps_file_path);
666
	char *deps_script_str = get_contents(deps_file_path, NULL);
666 667
	if (deps_script_str == NULL) {
667 668
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
668 669
		exit(1);
@ -690,7 +691,7 @@ void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path) {
690 691
			"};", source);
691 692
}
692 693
693
char *get_contents(char *path) {
694
char *get_contents(char *path, time_t *last_modified) {
694 695
/*#ifdef DEBUG
695 696
	printf("get_contents(\"%s\")\n", path);
696 697
#endif*/
@ -709,6 +710,10 @@ char *get_contents(char *path) {
709 710
		goto err;
710 711
	}
711 712
713
	if (last_modified != NULL) {
714
		*last_modified = f_stat.st_mtim.tv_sec;
715
	}
716
712 717
	char *buf = malloc(f_stat.st_size + 1);
713 718
	memset(buf, 0, f_stat.st_size);
714 719
	fread(buf, f_stat.st_size, 1, f);