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

View File

@@ -32,8 +32,6 @@ float str_to_float(char *strif){
return new_int; return new_int;
} }
typedef enum{ typedef enum{
TOKEN_PLUS, TOKEN_PLUS,
TOKEN_MINUS, TOKEN_MINUS,

114
nb.h
View File

@@ -5,7 +5,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <time.h>
typedef struct{ typedef struct{
int capacity; int capacity;
@@ -13,6 +13,10 @@ typedef struct{
char** value; char** value;
} nb_arr; } 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{ typedef struct{
FILE *filep; FILE *filep;
@@ -21,11 +25,12 @@ typedef struct{
char *buf; char *buf;
} nb_file; } 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(nb_arr *newarr, char *newval);
void nb_append_int(nb_arr *newarr, int myint); void nb_append_int(nb_arr *newarr, int myint); // will deprecate soon
void nb_append_float(nb_arr *newarr, float myfloat); 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); void nb_free(nb_arr *newarr);
@@ -38,22 +43,18 @@ void nb_print_info(nb_arr *newarr);
void nb_cmd(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); 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 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 #ifdef NB_IMPLEMENTATION // make sure to define this before using the header
@@ -135,17 +136,34 @@ void nb_free(nb_arr *newarr){
void nb_cmd(nb_arr *newarr) { void nb_cmd(nb_arr *newarr) {
if (newarr->arrsize < 1) { if (newarr->arrsize < 1) {
printf("USAGE: provide more parameters\n"); printf("USAGE: provide more parameters\n");
return;
} }
// Allocate bufferchar
char* cmd = (char*) malloc(sizeof(char*) * newarr->capacity); char* cmd = (char*) malloc(sizeof(char*) * newarr->capacity);
for (int i=0; i < newarr->arrsize; i++){
strcat(cmd, strcat(newarr->value[i]," ")); 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);
} }
system(cmd);
} }
@@ -188,37 +206,30 @@ bool nb_did_file_change(char *filename){
struct stat file_new; struct stat file_new;
char buf[64]; char buf[64];
sprintf(buf, "%s.new", filename); sprintf(buf, "%s.old", filename);
stat(buf, &file_new); stat(buf, &file_new);
if (file_old.st_mtim.tv_sec > file_new.st_mtim.tv_sec){ return difftime(file_old.st_mtime, file_new.st_mtime) > 0;
return true;
} else {
return false;
}
} }
bool nb_does_file_exist(char *filename){ bool nb_does_file_exist(char *filename){
char buf[64]; if (access(filename, F_OK) == 0){
sprintf(buf, "%s.new", filename);
if (access("test.c.new", F_OK) == 0){
return true; return true;
} }
return false; return false;
} }
void nb_rebuild(char filename[]){ void nb_rebuild(int argc, char **argv){
char *filename = "builder.c";
char cloned_file[128];
sprintf(cloned_file, "%s.old", filename);
char new_file[128]; if (nb_does_file_exist(cloned_file)){
sprintf(new_file, "%s.new", filename); // printf("%s does exist\n", cloned_file);
if (nb_does_file_exist(new_file)){
printf("%s does exist\n", new_file);
if (nb_did_file_change(filename)){ if (nb_did_file_change(filename)){
printf("file did change\n"); printf("[Rebuilding]\n");
nb_copy_file(filename, new_file); nb_copy_file(filename, cloned_file);
nb_arr cmd; nb_arr cmd;
char fname[128]; char fname[128];
@@ -230,41 +241,54 @@ void nb_rebuild(char filename[]){
if (dot != NULL) { if (dot != NULL) {
*dot = '\0'; *dot = '\0';
} }
printf("fname is: %s\n", fname);
nb_append(&cmd, "gcc"); nb_append(&cmd, "gcc");
nb_append(&cmd, "-o"); nb_append(&cmd, "-o");
nb_append(&cmd, fname); nb_append(&cmd, fname);
nb_append(&cmd, filename); nb_append(&cmd, filename);
// nb_print_info(&cmd);
nb_cmd(&cmd); nb_cmd(&cmd);
nb_print_info(&cmd); printf("[INFO] rebuilt %s\n", filename);
printf("rebuilt\n"); 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 { } else {
printf("file did not change\n"); // printf("file did not change\n");
} }
}else{ }else{
printf("created %s", filename); // printf("created %s.old\n", filename);
nb_copy_file(filename, new_file); 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; nb_file file;
file.filep = fopen(file_name, "rb"); file.filep = fopen(file_name, "rb");
fseek(file.filep, 0, SEEK_END); fseek(file.filep, 0, SEEK_END);
file.filesize = ftell(file.filep); file.filesize = ftell(file.filep);
file.buf = (char*)malloc(file.filesize); file.buf = (char*)malloc(file.filesize+1);
fseek(file.filep, 0, SEEK_SET); fseek(file.filep, 0, SEEK_SET);
fread(file.buf, 1, file.filesize, file.filep); fread(file.buf, 1, file.filesize, file.filep);
fclose(file.filep); fclose(file.filep);
file.buf[file.filesize] = '\0';
return file.buf; 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 #endif //NB_IMPLEMENTATION

22
nob.c
View File

@@ -1,22 +0,0 @@
#include "nb.h"
int main(void){
nb_arr cmd;
nb_append(&cmd, "gcc");
nb_append(&cmd, "-Wall -Wextra");
nb_append(&cmd, "lexer.c");
nb_append(&cmd, "-o lex");
nb_print_info(&cmd);
nb_cmd(&cmd);
nb_free(&cmd);
nb_append(&cmd, "./lex");
nb_print_info(&cmd);
nb_cmd(&cmd);
}