migrated to new version of nb.h

This commit is contained in:
2025-09-03 17:34:49 +03:00
parent d7205a90bb
commit 0c0cfc2c7e
3 changed files with 77 additions and 77 deletions

130
nb.h
View File

@@ -5,7 +5,7 @@
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
typedef struct{
int capacity;
@@ -13,6 +13,10 @@ typedef struct{
char** value;
} nb_arr;
#define nb_append_da(nb_arr, ...) \
nb_append_va(nb_arr, \
((const char*[]){__VA_ARGS__}), \
(sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*)))
typedef struct{
FILE *filep;
@@ -21,11 +25,12 @@ typedef struct{
char *buf;
} nb_file;
void nb_init(nb_arr *newarr, int initial_capacity);
void nb_init(nb_arr *newarr, int initial_capacity); // obsolete
void nb_append(nb_arr *newarr, char *newval);
void nb_append_int(nb_arr *newarr, int myint);
void nb_append_float(nb_arr *newarr, float myfloat);
void nb_append_int(nb_arr *newarr, int myint); // will deprecate soon
void nb_append_float(nb_arr *newarr, float myfloat); // will deprecate soon
void nb_append_va(nb_arr *newarr, const char *items[], int count);
void nb_free(nb_arr *newarr);
@@ -38,22 +43,18 @@ void nb_print_info(nb_arr *newarr);
void nb_cmd(nb_arr *newarr);
// void copy_file(char* old_file_name, char* new_file_name);
// File utils
void nb_copy_file(char* old_file_name, char* new_file_name);
char* nb_read_file(char* file_name);
bool nb_did_file_change(char *filename);
bool nb_does_file_exist(char *filename);
//bool needs_rebuild(); // need to implement rename file first to .old or something like nob does
bool nb_did_file_change(char *filename);
void nb_rebuild(int argc, char **argv);
bool nb_does_file_exist(char *filename);
void nb_rebuild(char filename[]);
char* nb_read_file(char* file_name);
#ifdef NB_IMPLEMENTATION // make sure to define this before using the header
@@ -134,18 +135,35 @@ void nb_free(nb_arr *newarr){
}
void nb_cmd(nb_arr *newarr){
if (newarr->arrsize < 1){
printf("USAGE: provide more parameters\n");
}
void nb_cmd(nb_arr *newarr) {
if (newarr->arrsize < 1) {
printf("USAGE: provide more parameters\n");
return;
}
char* cmd = (char*)malloc(sizeof(char*) *newarr->capacity);
for (int i=0; i < newarr->arrsize; i++){
strcat(cmd, strcat(newarr->value[i]," "));
}
system(cmd);
// Allocate bufferchar
char* cmd = (char*) malloc(sizeof(char*) * newarr->capacity);
cmd[0] = '\0';
for (int i = 0; i < newarr->arrsize; i++) {
strcat(cmd, newarr->value[i]);
if (i < newarr->arrsize - 1) {
strcat(cmd, " ");
}
}
printf("[CMD] %s\n", cmd);
if (system(cmd) == -1) {
perror("system");
}
free(cmd);
for (int i=0; i < newarr->arrsize; ++i){
nb_free(newarr);
}
}
@@ -188,37 +206,30 @@ bool nb_did_file_change(char *filename){
struct stat file_new;
char buf[64];
sprintf(buf, "%s.new", filename);
sprintf(buf, "%s.old", filename);
stat(buf, &file_new);
if (file_old.st_mtim.tv_sec > file_new.st_mtim.tv_sec){
return true;
} else {
return false;
}
return difftime(file_old.st_mtime, file_new.st_mtime) > 0;
}
bool nb_does_file_exist(char *filename){
char buf[64];
sprintf(buf, "%s.new", filename);
if (access("test.c.new", F_OK) == 0){
if (access(filename, F_OK) == 0){
return true;
}
return false;
}
void nb_rebuild(char filename[]){
char new_file[128];
sprintf(new_file, "%s.new", filename);
void nb_rebuild(int argc, char **argv){
char *filename = "builder.c";
char cloned_file[128];
sprintf(cloned_file, "%s.old", filename);
if (nb_does_file_exist(new_file)){
printf("%s does exist\n", new_file);
if (nb_does_file_exist(cloned_file)){
// printf("%s does exist\n", cloned_file);
if (nb_did_file_change(filename)){
printf("file did change\n");
nb_copy_file(filename, new_file);
printf("[Rebuilding]\n");
nb_copy_file(filename, cloned_file);
nb_arr cmd;
char fname[128];
@@ -229,42 +240,55 @@ void nb_rebuild(char filename[]){
char *dot = strrchr(fname, '.');
if (dot != NULL) {
*dot = '\0';
}
printf("fname is: %s\n", fname);
}
nb_append(&cmd, "gcc");
nb_append(&cmd, "-o");
nb_append(&cmd, fname);
nb_append(&cmd, filename);
// nb_print_info(&cmd);
nb_cmd(&cmd);
nb_print_info(&cmd);
printf("rebuilt\n");
printf("[INFO] rebuilt %s\n", filename);
nb_free(&cmd);
// printf("[INFO] %s", argv)
printf("\n");
for (int i=0; i<argc; ++i){
nb_append_da(&cmd, argv[i]);
}
nb_cmd(&cmd);
exit(1);
} else {
printf("file did not change\n");
// printf("file did not change\n");
}
}else{
printf("created %s", filename);
nb_copy_file(filename, new_file);
// printf("created %s.old\n", filename);
nb_copy_file(filename, cloned_file);
}
}
char* nb_read_file(char* file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file.
char* nb_read_file(char* file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file. I should think more about adding error handling
nb_file file;
file.filep = fopen(file_name, "rb");
fseek(file.filep, 0, SEEK_END);
file.filesize = ftell(file.filep);
file.buf = (char*)malloc(file.filesize);
file.buf = (char*)malloc(file.filesize+1);
fseek(file.filep, 0, SEEK_SET);
fread(file.buf, 1, file.filesize, file.filep);
fclose(file.filep);
file.buf[file.filesize] = '\0';
return file.buf;
}
void nb_append_va(nb_arr *newarr, const char *items[], int count) {
for (int i = 0; i < count; i++) {
nb_append(newarr, (char*)items[i]);
}
}
#endif //NB_IMPLEMENTATION