Skip to content

Commit

Permalink
value library move to C
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Aug 6, 2024
1 parent f59303d commit 936c1b9
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 135 deletions.
4 changes: 1 addition & 3 deletions src/ccode/archive-create.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <archive_extract.h>
#include <logger.h>
#include <value.h>
#include <error.h>

#ifndef PATH_MAX
Expand All @@ -44,9 +45,6 @@
int lstat(const char *pathname, struct stat *statbuf);
#endif

#ifndef get_bool
int get_bool(char*variable);
#endif

#ifndef isexists
int isexists(const char*path);
Expand Down
5 changes: 1 addition & 4 deletions src/ccode/brainfuck.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#include <string.h>

#include <logger.h>
#include <value.h>
#include <error.h>

#ifndef get_bool
int get_bool(char* msg);
#endif

void brainfuck(char * code, unsigned int size) {
unsigned int ptr = 0;
unsigned int i = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/ccode/colorize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <unistd.h>
#include <stdio.h>

#ifndef get_bool
int get_bool(char* name);
#endif
#include <value.h>

#ifdef __STRICT_ANSI__
#define strdup(A) strcpy(calloc(strlen(A) + 1, sizeof(char)), A);
Expand Down
9 changes: 2 additions & 7 deletions src/ccode/fetcher_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
#include <stdlib.h>
#include <curl/curl.h>

#ifndef get_value
char* get_value(char* variable);
#endif
#ifndef get_bool
int get_bool(char* variable);
#endif

#include <logger.h>
#include <error.h>
#include <value.h>


#ifndef PATH_MAX
#define PATH_MAX 1024
Expand Down
2 changes: 1 addition & 1 deletion src/ccode/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <time.h>

/* functions from vala source */
int get_bool(char* val);
bool get_bool(char* val);
void colorize_init();

/* string add function*/
Expand Down
8 changes: 4 additions & 4 deletions src/ccode/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <sys/mount.h>
#include <sys/prctl.h>


#include <value.h>


#ifndef clear_env
void clear_env();
#endif
Expand All @@ -40,10 +44,6 @@ int sandbox_network = 1;
int sandbox_uid = 0;
int sandbox_gid = 0;

#ifndef get_bool
int get_bool(char* variable);
#endif

#ifndef get_builddir_priv
char* get_builddir_priv();
#endif
Expand Down
78 changes: 78 additions & 0 deletions src/ccode/value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

typedef struct _variable {
char* name;
char* value;
bool read_only;
} variable;

static variable* vars;
size_t var_count = 0;


static void set_value_fn(char* name, char* value, bool read_only){
if(name == NULL || value == NULL){
return;
}
size_t i=0;
for(i=0;i<var_count;i++){
if(strcmp(vars[i].name, name) == 0 && ! vars[i].read_only){
vars[i].value = strdup(value);
return;
}
}
if(vars == NULL){
vars = calloc(1, sizeof(var_count));
}
vars = realloc(vars, (var_count+1)* sizeof(variable));
vars[var_count].name = strdup(name);
vars[var_count].value = strdup(value);
vars[var_count].read_only = read_only;
var_count++;
}

void set_value(char* name, char* value){
set_value_fn(name, value, false);
}

void set_value_readonly(char* name, char* value){
set_value_fn(name, value, false);
}

char* get_value(char* name){
size_t i=0;
for(i=0;i<var_count;i++){
if(strcmp(vars[i].name, name) == 0){
/*printf("%s %s %s\n", vars[i].name, name, vars[i].value);*/
return strdup(vars[i].value);
}
}
return strdup("");
}

bool get_bool(char* name){
return strcmp(get_value(name), "true") == 0;
}

void set_bool(char* name, bool value){
if(value){
set_value(name,"true");
} else{
set_value(name,"false");
}
}

char** get_variable_names(int* len){
char** ret = calloc(var_count+1, sizeof(char*));
size_t i=0;
for(i=0; i<var_count; i++){
ret[i] = strdup(vars[i].name);
}
*len = var_count;
return ret;
}

1 change: 1 addition & 0 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ char* _(char* msg){
#include <locale.h>
#endif
#include <ymp.h>
#include <value.h>
#include <stdio.h>
#include <unistd.h>

Expand Down
7 changes: 7 additions & 0 deletions src/include/value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdbool.h>
char* get_value(char* name);
void set_value(char* name, char* value);
void set_value_readonly(char* name, char* value);
bool get_bool(char* name);
void set_bool(char* name, bool value);
char** get_variable_names(int* len);
2 changes: 1 addition & 1 deletion src/operations/shell/setget.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
private static int get_main (string[] args) {
if (args.length < 1) {
foreach (string name in list_values ()) {
foreach (string name in get_variable_names ()) {
print (name + ":" + get_value (name));
}
return 0;
Expand Down
116 changes: 4 additions & 112 deletions src/util/value.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,124 +8,12 @@
//DOC: stderr.printf ("Debug mode enabled");
//DOC: }
//DOC: ```
class variable {
public string name;
public string value;
}

class variable_integer {
public string name;
public int value;
}

private variable[] vars;

//DOC: `void set_value (string name, string value):`
//DOC: add a global ymp variable as string
public void set_value (string name, string value) {
if (vars == null) {
vars = {};
}
if (name == null || value == null) {
return;
}
debug("Set value: "+name+"="+value);
foreach (variable varx in vars) {
if (varx.name == name.up ()) {
return;
}else if (varx.name == name.down ()) {
return;
}
}
variable varx = new variable ();
varx.name = name.down ();
varx.value = value;
vars += varx;
}

public string[] get_variable_names(){
string[] ret = {};
foreach (variable varx in vars) {
ret += varx.name;
}
return ret;
}

private void set_value_readonly (string name, string value) {
if (vars == null) {
vars = {};
}
if (name == null || value == null) {
return;
}
#if DEBUG
// stderr.printf ("Set read_only value: "+name+"="+value+"\n");
#endif
foreach (variable varx in vars) {
if (varx.name == name.up ()) {
varx.value = value;
return;
}
}
set_value (name, "");
variable varx = new variable ();
varx.name = name.up ();
varx.value = value;
vars += varx;
}

//DOC: `string get_value (string name):`
//DOC: get a ymp global variable as string
//DOC: big names are read only and only defined by ymp
public string get_value (string name) {
if (vars == null) {
vars = {};
}
if (name == null) {
return "";
}
#if DEBUG
//stderr.printf ("Get value: "+name+"\n");
#endif
foreach (variable var in vars) {
if (var.name == name.up ()) {
return var.value;
}else if (var.name == name) {
return var.value;
}
}
return "";
}

//DOC: `bool get_bool (string name):`
//DOC: get a ymp global variable as bool
public bool get_bool (string name) {
return get_value (name).down () == "true";
}

//DOC: `void set_bool (string name, bool value):`
//DOC: add a global ymp variable as bool
public void set_bool (string name, bool value) {
debug("Set bool: "+name+"="+value.to_string());
if (value) {
set_value (name, "true");
}else {
set_value (name, "false");
}
}

//DOC: `string[] list_values ():`
//DOC: return array of all ymp global value names
public string[] list_values () {
string[] ret = {};
foreach (variable var in vars) {
if (var.value != "") {
ret += var.name;
}
}
return ret;
}

//DOC: `void set_env (string variable, string value):`
//DOC: add environmental variable
public void set_env (string variable, string value) {
Expand All @@ -145,6 +33,10 @@ public string get_env (string variable) {
public string get_home () {
return GLib.Environment.get_home_dir ();
}
class variable {
public string name;
public string value;
}


private variable[] envs;
Expand Down
19 changes: 19 additions & 0 deletions src/vapi/value.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[CCode (cheader_filename = "value.h")]
public string get_value(string name);

[CCode (cheader_filename = "value.h")]
public void set_value(string name, string value);

[CCode (cheader_filename = "value.h")]
public void set_value_readonly(string name, string value);

[CCode (cheader_filename = "value.h")]
public string[] get_variable_names();

[CCode (cheader_filename = "value.h")]
public bool get_bool(string name);

[CCode (cheader_filename = "value.h")]
public void set_bool(string name, bool value);


0 comments on commit 936c1b9

Please sign in to comment.