1
0
Fork 0

Finished trie, started rpn

master
LEdoian 7 years ago
parent 6a99fb07d1
commit d54692a622

@ -10,9 +10,9 @@ const int cfg_log_facility = LOG_CRON;
const char cfg_log_ident[] = "kairos"; const char cfg_log_ident[] = "kairos";
const int cfg_log_opt = LOG_PID | LOG_NDELAY; const int cfg_log_opt = LOG_PID | LOG_NDELAY;
const char cfg_trie_file[] = "/home/ledoian/.kairos/trie" const char cfg_trie_file[] = "/home/ledoian/.kairos/trie";
const char cfg_tasks_file[] = "/home/ledoian/.kairos/tasks" const char cfg_tasks_file[] = "/home/ledoian/.kairos/tasks";
const char cfg_socket[] = "/tmp/kairos.sock" const char cfg_socket[] = "/tmp/kairos.sock";
const int cfg_sock_maxclients = 16; const int cfg_sock_maxclients = 16;
const int cfg_var_name_max_len = 16; const int cfg_var_name_max_len = 16;

40
rpn.c

@ -0,0 +1,40 @@
/*
* rpn.c: evalutor of RPN formulas
*/
#include <stdint.h>
#include <syslog.h>
#include <stdlib.h>
#include "config.h"
#include "rpn.h"
#include "trie.h"
// Growing stack implementation
struct grow {
struct trie_retval *arr;
int64_t elems;
int64_t alloc;
}
static struct grow *grow_init(void) {
struct grow *g = malloc(sizeof(struct grow));
if (g == NULL) {
syslog(cfg_log_facility | LOG_ERR, "rpn: Could not initialize growing array: %m");
return NULL;
}
g -> arr = NULL;
g -> elems = 0;
g -> alloc = 0;
return g;
}
static bool grow_push (int64_t val, struct grow *stack) {
if (stack -> alloc <= stack -> elems) {
stack
}
}
bool rpn_eval (char rpn[]) {
}

12
rpn.h

@ -0,0 +1,12 @@
/*
* rpn.h: a header file for rpn.c
*/
#ifndef RPN_H
#define RPN_H
#include <stdbool.h>
bool rpn_eval (char rpn[]);
#endif

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "trie.h" #include "trie.h"
#include "config.h" #include "config.h"
@ -21,11 +22,25 @@ static int indexof(char c) {
return 36; return 36;
} else { } else {
// Something weird happened // Something weird happened
syslog(cfg_log_facility|LOG_ERR, "trie: weird input character: %c", c); syslog(cfg_log_facility|LOG_NOTICE, "trie: weird character: %c", c);
return -1; return -1;
} }
} }
static char charof(int i) {
//ASCII again
if (i <= 25){
return ('A' + i);
} else if (26 <= i && i <= 35) {
return ('0' + i - 26);
} else if (i == 36) {
return '_';
} else {
syslog(cfg_log_facility|LOG_NOTICE, "trie: weird character index: %d", i);
return '\0';
}
}
static bool trie_set_deep(char string[], int64_t val, struct trie *vertex) { static bool trie_set_deep(char string[], int64_t val, struct trie *vertex) {
// Recursive function to find and set a value // Recursive function to find and set a value
if (string[0] = '\0') { if (string[0] = '\0') {
@ -60,7 +75,7 @@ bool trie_set(char string[], int64_t val) {
static struct trie_retval trie_lookup_deep(char string[], struct trie *vertex) { static struct trie_retval trie_lookup_deep(char string[], struct trie *vertex) {
if (string[0] == '\0') { if (string[0] == '\0') {
return (struct trie_retval) {vertex -> val, vertex -> def}; return (struct trie_retval) {vertex -> val, vertex -> defined};
} else { } else {
int chld_index = indexof(string[0]); int chld_index = indexof(string[0]);
if (chld_index == -1 || vertex -> children[chld_index] == NULL) { if (chld_index == -1 || vertex -> children[chld_index] == NULL) {
@ -94,12 +109,31 @@ bool trie_load(void) {
return false; return false;
} }
if(trie_set(name, val) == false) { if(trie_set(name, val) == false) {
syslog(cfg_log_facility | LOG_WARN, "trie: setting a variable failed: %s = %d", name, val); syslog(cfg_log_facility | LOG_WARNING, "trie: setting a variable failed: %s = %d", name, val);
} }
} }
return true; return true;
} }
static bool trie_dfs(char prefix[], int prefixlen, FILE *f, struct trie *vertex) {
if (vertex == NULL) {// Very special case;
return true;
} else {
bool retval = true;
if (vertex -> defined) {
prefix[prefixlen] = '\0';
fprintf(f, "%s %d\n", prefix, vertex -> val);
}
for (int i = 0; i<37; i++) {
if (vertex -> children[i] != NULL) {
prefix[prefixlen] = charof(i);
retval &= trie_dfs(prefix, prefixlen + 1, f, vertex -> children[i]);
}
}
return retval;
}
}
bool trie_save(void) { bool trie_save(void) {
FILE *f = fopen(cfg_trie_file, "w"); FILE *f = fopen(cfg_trie_file, "w");
if (f == NULL) { if (f == NULL) {
@ -108,5 +142,9 @@ bool trie_save(void) {
} }
// DFS, write every found variable into f // DFS, write every found variable into f
char prefix[cfg_var_name_max_len +1];
prefix[cfg_var_name_max_len +1] = '\0';
int prefixlen = 0;
return trie_dfs(prefix, prefixlen, f, trie_base);
} }

Loading…
Cancel
Save