Skip to content

Commit

Permalink
Add experimental v4d3 C arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nsauzede committed Dec 6, 2023
1 parent b52257f commit 430ca20
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 0 deletions.
21 changes: 21 additions & 0 deletions vade/src/v4d3/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MINIGREP_FS_H
#define MINIGREP_FS_H

#include <malloc.h>
#include <stdio.h>

const char *fs_read_to_string(char **_result, const char *path) {
FILE *in = fopen(path, "rt");
if (!in) {
return "No such file or directory (os error 2)";
}
fseek(in, 0, SEEK_END);
long size = ftell(in);
rewind(in);
*_result = malloc(size);
fread(*_result, size, 1, in);
fclose(in);
return 0;
}

#endif/*MINIGREP_FS_H*/
12 changes: 12 additions & 0 deletions vade/src/v4d3/minigrep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MINIGREP_H
#define MINIGREP_H

typedef struct {
char *query;
char *file_path;
} minigrep_config_t;

extern const char *minigrep_config_build(minigrep_config_t *config, int argc, char *argv[]);
extern const char *minigrep_run(minigrep_config_t *config);

#endif/*MINIGREP_H*/
32 changes: 32 additions & 0 deletions vade/src/v4d3/os_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef OS_COMPAT_H
#define OS_COMPAT_H

#ifdef WIN32
#include <string.h>
void *reallocarray(void *ptr, size_t nmemb, size_t size) {
return realloc(ptr, nmemb * size);
}
char *strchrnul(const char *s, int c) {
char *ret = strchr(s, c);
if (!ret) {
ret = (char *)s + strlen(s);
}
return ret;
}
char *strndup(const char s[], size_t n) {
size_t len = 0;
for (size_t i = 0; i < n; i++) {
if (!s[i]) {
break;
}
len++;
}
char *res = calloc(len + 1, 1);
for (size_t i = 0; i < len; i++) {
res[i] = s[i];
}
return res;
}
#endif

#endif/*OS_COMPAT_H*/
Loading

0 comments on commit 430ca20

Please sign in to comment.