-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1715 from lf-lang/file-access
File access
- Loading branch information
Showing
8 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
Submodule reactor-c
updated
13 files
+2 −1 | core/CMakeLists.txt | |
+1 −1 | core/trace.c | |
+2 −2 | include/api/set.h | |
+2 −2 | include/core/reactor.h | |
+1 −1 | include/core/trace.h | |
+16 −48 | util/audio_loop.h | |
+3 −53 | util/audio_loop_linux.c | |
+3 −26 | util/audio_loop_mac.c | |
+16 −80 | util/sensor_simulator.c | |
+53 −69 | util/sensor_simulator.h | |
+4 −43 | util/wave_file_reader.c | |
+1 −0 | util/wave_file_reader.cmake | |
+16 −33 | util/wave_file_reader.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** Test reading a file at a location relative to the source file. */ | ||
target C | ||
|
||
reactor Source { | ||
output out: char* // Use char*, not string, so memory is freed. | ||
|
||
reaction(startup) -> out {= | ||
char* file_path = | ||
LF_SOURCE_DIRECTORY | ||
LF_FILE_SEPARATOR "lib" | ||
LF_FILE_SEPARATOR "FileReader.txt"; | ||
|
||
FILE* file = fopen(file_path, "rb"); | ||
if (file == NULL) lf_print_error_and_exit("Error opening file at path %s.", file_path); | ||
|
||
// Determine the file size | ||
fseek(file, 0, SEEK_END); | ||
long file_size = ftell(file); | ||
fseek(file, 0, SEEK_SET); | ||
|
||
// Allocate memory for the buffer | ||
char* buffer = (char *) malloc(file_size + 1); | ||
if (buffer == NULL) lf_print_error_and_exit("Out of memory."); | ||
|
||
// Read the file into the buffer | ||
fread(buffer, file_size, 1, file); | ||
buffer[file_size] = '\0'; | ||
fclose(file); | ||
|
||
lf_set(out, buffer); | ||
=} | ||
} | ||
|
||
main reactor { | ||
preamble {= | ||
#include <string.h> | ||
=} | ||
s = new Source() | ||
|
||
reaction(s.out) {= | ||
printf("Received: %s\n", s.out->value); | ||
if (strcmp("Hello World", s.out->value) != 0) { | ||
lf_print_error_and_exit("Expected 'Hello World'"); | ||
} | ||
=} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** Test reading a file at a location relative to the source file. */ | ||
target C { | ||
timeout: 0 s | ||
} | ||
|
||
reactor Source { | ||
output out: char* // Use char*, not string, so memory is freed. | ||
|
||
reaction(startup) -> out {= | ||
char* file_path = | ||
LF_SOURCE_DIRECTORY | ||
LF_FILE_SEPARATOR ".." | ||
LF_FILE_SEPARATOR "lib" | ||
LF_FILE_SEPARATOR "FileReader.txt"; | ||
|
||
FILE* file = fopen(file_path, "rb"); | ||
if (file == NULL) lf_print_error_and_exit("Error opening file at path %s.", file_path); | ||
|
||
// Determine the file size | ||
fseek(file, 0, SEEK_END); | ||
long file_size = ftell(file); | ||
fseek(file, 0, SEEK_SET); | ||
|
||
// Allocate memory for the buffer | ||
char* buffer = (char *) malloc(file_size + 1); | ||
if (buffer == NULL) lf_print_error_and_exit("Out of memory."); | ||
|
||
// Read the file into the buffer | ||
fread(buffer, file_size, 1, file); | ||
buffer[file_size] = '\0'; | ||
fclose(file); | ||
|
||
// For federated version, have to use lf_set_array so array size is know | ||
// to the serializer. | ||
lf_set_array(out, buffer, file_size + 1); | ||
=} | ||
} | ||
|
||
reactor Check { | ||
preamble {= | ||
#include <string.h> | ||
=} | ||
input in: char* | ||
|
||
reaction(in) {= | ||
printf("Received: %s\n", in->value); | ||
if (strcmp("Hello World", in->value) != 0) { | ||
lf_print_error_and_exit("Expected 'Hello World'"); | ||
} | ||
=} | ||
} | ||
|
||
federated reactor { | ||
s = new Source() | ||
c = new Check() | ||
s.out -> c.in | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters