Skip to content

Commit

Permalink
error library move to C
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 27, 2024
1 parent 5e1c86c commit fb4c1d2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
3 changes: 3 additions & 0 deletions src/ccode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ private extern void bf_compile(string code);
//DOC: ymp plugin load function
public extern void load_plugin (string path);

public extern void error(int status);
public extern void error_add(string message);
public extern bool has_error();

//libc functions for vala source
private extern int chown(char* path, int uid, int gid);
Expand Down
2 changes: 1 addition & 1 deletion src/ccode/archive-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern void error_add(char* message);
extern char* build_string(char* format, ...);

#ifdef __STRICT_ANSI__
#define strdup(A) strcpy(calloc(strlen(A) + 1, sizeof(char)), A);
#define strdup(A) strcpy(calloc(strlen(A) + 1, sizeof(char)), A)
#endif


Expand Down
58 changes: 58 additions & 0 deletions src/ccode/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <array.h>

#include <error.h>

#ifdef __STRICT_ANSI__
#define strdup(A) strcpy(calloc(strlen(A) + 1, sizeof(char)), A)
#endif


extern bool get_bool(char* value);
extern char* build_string(char* format, ...);
extern void print_stderr(char* message);
extern char* colorize(char* message, int color);
extern char* gettext(char* message);
#define _(A) gettext(A)
#define red 31

static array *error_arr;

void error(int status){
if(!error_arr){
error_arr = array_new();
return;
}
size_t i;
size_t len = 0;
char** errs = array_get(error_arr, &len);
if(len > 0 && !get_bool ("ignore-error")) {
printf("%ld\n", len);
for(i=0;i<len;i++){
if(errs[i] != NULL){
print_stderr(build_string("%s: %s", colorize(_ ("ERROR"), red), strdup(errs[i])));
}
}
if(!get_bool ("shellmode")){
exit(status);
}
}
array_unref(error_arr);
error_arr = array_new();
}

void error_add(char* message) {
if(!error_arr){
error_arr = array_new();
}
array_add(error_arr, message);
}

bool has_error(){
if(!error_arr){
error_arr = array_new();
}
return array_length(error_arr) > 0;
}
4 changes: 4 additions & 0 deletions src/include/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdbool.h>
void error(int status);
void error_add(char* message);
bool has_error();
39 changes: 0 additions & 39 deletions src/util/logger.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
private string[] errors;

//DOC: ## logging functions

Expand Down Expand Up @@ -62,41 +61,3 @@ public void set_terminal_title (string msg) {
#endif
}

//DOC: `void error (int status):`
//DOC: print error message and exit if error message list not empty and status is not 0.
//DOC: This function clear error message list.
//DOC: This funtion must run after **error_add (string message)**
//DOC: Example usage:
//DOC: ```vala
//DOC: if (num == 12) {
//DOC: error_add ("Number is not 12.");
//DOC: }
//DOC: error (1);
//DOC: ```
public void error (int status) {
if (has_error ()) {
if (!get_bool ("ignore-error")) {
foreach (string error in errors) {
print_stderr ("%s: %s".printf (colorize (_ ("ERROR"), red), error));
}
}
errors = null;
if (status != 0 && !get_bool ("shellmode")) {
exit (status);
}
}
}
public bool has_error () {
return errors.length != 0;
}

//DOC: `void error_add (string message):`
//DOC: add error message to error message list
public void error_add (string message) {
if (errors == null) {
errors = {};
}
if (message != null) {
errors += message;
}
}

0 comments on commit fb4c1d2

Please sign in to comment.