From 3a1d3898c3d42d43645b79586f0b26ab4f8ff331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 2 Nov 2019 22:29:47 +0100 Subject: Remove ALL c code. --- src/calendar.c | 173 ------------------------------- src/calendar.h | 41 -------- src/err.h | 42 -------- src/guile_type_helpers.c | 13 --- src/guile_type_helpers.h | 16 --- src/macro.h | 134 ------------------------ src/main.scm | 24 ----- src/parse.c | 264 ----------------------------------------------- src/parse.h | 99 ------------------ src/strbuf.c | 143 ------------------------- src/strbuf.h | 108 ------------------- src/struct.h | 23 ----- src/struct.scm.c | 96 ----------------- 13 files changed, 1176 deletions(-) delete mode 100644 src/calendar.c delete mode 100644 src/calendar.h delete mode 100644 src/err.h delete mode 100644 src/guile_type_helpers.c delete mode 100644 src/guile_type_helpers.h delete mode 100644 src/macro.h delete mode 100755 src/main.scm delete mode 100644 src/parse.c delete mode 100644 src/parse.h delete mode 100644 src/strbuf.c delete mode 100644 src/strbuf.h delete mode 100644 src/struct.h delete mode 100644 src/struct.scm.c (limited to 'src') diff --git a/src/calendar.c b/src/calendar.c deleted file mode 100644 index 28891330..00000000 --- a/src/calendar.c +++ /dev/null @@ -1,173 +0,0 @@ -#include "calendar.h" - -#include -#include -#include -#include -#include - -/* basename */ -#include -#include - -#include "struct.h" -#include "parse.h" -#include "err.h" - -int read_vcalendar(SCM cal, char* path) { - - struct stat statbuf; - if (stat (path, &statbuf) != 0) { - fprintf(stderr, - "Error stating file or directory, errno = %i\npath = [%s]\n", - errno, path); - } - - int type = statbuf.st_mode & 0777000; - int chmod = statbuf.st_mode & 0777; - INFO_F("file has mode 0%o, with chmod = 0%o", type, chmod); - - switch (type) { - case S_IFREG: handle_file(cal, path); break; - case S_IFDIR: handle_dir (cal, path); break; - case S_IFLNK: - ERR("Found symlink, can't be bothered to check it further."); - break; - - default: ; - } - - return 0; -} - -int handle_file(SCM cal, char* path) { - INFO("Parsing a single file"); - - /* NAME is the `fancy' name of the calendar. */ - // vcomponent_push_val(cal, "NAME", basename(path)); - SCM line = scm_make_vline(scm_from_utf8_string("file")); - scm_add_line_x(cal, scm_from_utf8_string("X-HNH-SOURCETYPE"), line); - char* resolved_path = realpath(path, NULL); - open_ics (resolved_path, cal); - free (resolved_path); - - return 0; -} - - -int handle_dir(SCM cal, char* path) { - INFO("Parsing a directory"); - DIR* dir = opendir(path); - - /* Buffer for holding search path and filename */ - char buf[PATH_MAX] = { [0 ... PATH_MAX - 1] = '\0' }; - strcpy(buf, path); - int path_len = strlen(path) + 1; - - /* Slash to guarantee we have at least one */ - buf[path_len - 1] = '/'; - - - /* NAME is the `fancy' name of the calendar. */ - // vcomponent_push_val(cal, "NAME", basename(path)); - scm_add_line_x(cal, scm_from_utf8_string("NAME"), - scm_make_vline(scm_from_utf8_stringn(basename(path), strlen(basename(path))))); - SCM line = scm_make_vline(scm_from_utf8_string("vdir")); - scm_add_line_x(cal, scm_from_utf8_string("X-HNH-SOURCETYPE"), line); - - struct dirent* d; - while ((d = readdir(dir)) != NULL) { - /* Check that it's a regular file */ - if (d->d_type != DT_REG) continue; - - /* Append filename with currentt searchpath */ - strcat(buf, d->d_name); - char* resolved_path = realpath(buf, NULL); - /* Remove file part from combined path */ - buf[path_len] = '\0'; - - FILE* f; - size_t read, size = 0x100; - char* info_buf = malloc(size); - if (strcmp (d->d_name, "color") == 0) { - f = fopen(resolved_path, "r"); - read = getline(&info_buf, &size, f); - // TODO this isn't actually needed since we trim the - // string into an SCM string directly here. - if (info_buf[read - 1] == '\n') - info_buf[read - 1] = '\0'; - - fclose(f); - scm_add_line_x(cal, scm_from_utf8_string("COLOR"), - scm_make_vline(scm_from_utf8_stringn(info_buf, read - 1))); - } else if (strcmp (d->d_name, "displayname") == 0) { - f = fopen(resolved_path, "r"); - read = getline(&info_buf, &size, f); - if (info_buf[read - 1] == '\n') - info_buf[read - 1] = '\0'; - - fclose(f); - - /* This adds the new list to the set of names, keeping the - * filename name. - * This works since *currently* values are returned in - * reverse order - */ - scm_add_line_x(cal, scm_from_utf8_string("NAME"), - scm_make_vline(scm_from_utf8_stringn(info_buf, read))); - } else { - open_ics (resolved_path, cal); - } - - free (resolved_path); - } - - closedir(dir); - return 0; -} - -int get_extension(const char* filename, char* ext, ssize_t max_len) { - - if (filename == NULL) { - ext[0] = '\0'; - return 0; - } - - int ext_idx = -1; - ext[0] = '\0'; - for (int i = 0; filename[i] != '\0'; i++) { - if (filename[i] == '.') ext_idx = i + 1; - if (filename[i] == '/') ext_idx = -1; - } - - if (ext_idx == -1) return 0; - - int ext_len = 0; - for (int i = 0; i < max_len; i++, ext_len++) { - char c = filename[i + ext_idx]; - if (c == '\0') break; - ext[i] = c; - } - ext[ext_len] = '\0'; - return ext_len; -} - -int check_ext (const char* path, const char* ext) { - char buf[10]; - int has_ext = get_extension(path, buf, 9); - - return has_ext && strcmp(buf, ext) == 0; -} - -int open_ics (char* resolved_path, SCM cal) { - if (! check_ext(resolved_path, "ics") ) return 2; - - FILE* f = fopen(resolved_path, "r"); - - if (f == NULL) return 1; - - parse_file(resolved_path, f, cal); - fclose(f); - - return 0; -} diff --git a/src/calendar.h b/src/calendar.h deleted file mode 100644 index 776d9900..00000000 --- a/src/calendar.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef CALENDAR_H -#define CALENDAR_H - -#include - -/* - * Reads all ics flies in path into the given vcomponent. The - * component is assumed to be a abstract ROOT element, whose first - * component will most likely become a VCALENDAR. - * - * path should either be a single .ics file (vcalendar), or a - * directory directly containing .ics files (vdir). - */ -int read_vcalendar(SCM cal, char* path); - -/* - * Gets extension from filename. Writes output to ext. - * Assumes that the extension is the text between the last dot and - * the end of the string, and that no slashes can occur between the - * dot and the end. - * - * Returns the length of the extension, 0 if no extension. - */ -int get_extension(const char* filename, char* ext, ssize_t max_len); - -/* Returns 1 if path has extension ext, 0 otherwise */ -int check_ext (const char* path, const char* ext); - -/* Handle a lone ics file */ -int handle_file(SCM cal, char* path); - -/* Handle a directory of ics files */ -int handle_dir(SCM cal, char* path); - -/* - * Helper for opening a single ICS file. Handles file internally, and - * writes output to cal. - */ -int open_ics (char* resolved_path, SCM cal); - -#endif /* CALENDAR_H */ diff --git a/src/err.h b/src/err.h deleted file mode 100644 index d9d19ec7..00000000 --- a/src/err.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef ERR_H -#define ERR_H - -#include - -#include "macro.h" - -#define _RESET "\x1b[m" -#define _BLACK "\x1B[0;30m" -#define _RED "\x1B[0;31m" -#define _GREEN "\x1B[0;32m" -#define _YELLOW "\x1B[0;33m" -#define _BLUE "\x1B[0;34m" -#define _PURPLE "\x1B[0;35m" -#define _CYAN "\x1B[0;36m" -#define _WHITE "\x1B[0;37m" - -#define ERR(msg) fprintf(stderr, _RED "ERR" _RESET " (%s:%i) %s\n", __FILE__, __LINE__, #msg) -#define ERR_F(fmt, ...) fprintf(stderr, _RED "ERR" _RESET " (%s:%i) " fmt "\n", \ - __FILE__, __LINE__, ##__VA_ARGS__) - -/* Parse error */ -#define ERR_P(ctx, fmt, ...) fprintf(stderr, _RED "PARSE" _RESET " (%s:%i) %i:%i " fmt "\n", \ - __FILE__, __LINE__, (ctx)->pline, (ctx)->pcolumn, ##__VA_ARGS__) - -#define INFO(msg) fprintf(stderr, _BLUE "INFO" _RESET " (%s:%i) %s\n", __FILE__, __LINE__, #msg) -#define INFO_F(fmt, ...) fprintf(stderr, _BLUE "INFO" _RESET " (%s:%i) " fmt "\n", \ - __FILE__, __LINE__, ##__VA_ARGS__) - -#define LINE(len) do { \ - printf(_GREEN); \ - FOR(int, i, len) printf("_"); \ - printf("\n"); \ -} while (0) - -#define PRINT(T, v) do { \ - char buf[0x1000]; \ - FMT(T)(v, buf); \ - INFO_F("%s", buf); \ -} while (0) - -#endif /* ERR_H */ diff --git a/src/guile_type_helpers.c b/src/guile_type_helpers.c deleted file mode 100644 index 2864df0d..00000000 --- a/src/guile_type_helpers.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "guile_type_helpers.h" - -#include "macro.h" - -SCM scm_from_strbuf(strbuf* s) { - SCM ret = scm_from_utf8_stringn (s->mem, s->len); - scm_gc_protect_object(ret); - return ret; -} - -SCM scm_from_strbuf_symbol(strbuf* s) { - return scm_string_to_symbol(scm_from_strbuf(s)); -} diff --git a/src/guile_type_helpers.h b/src/guile_type_helpers.h deleted file mode 100644 index fe0e875a..00000000 --- a/src/guile_type_helpers.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef GUILE_TYPE_HELPERS_H -#define GUILE_TYPE_HELPERS_H - -#include - -#include "strbuf.h" - -#define SCM_IS_LIST(x) scm_is_true(scm_list_p(x)) -#define string_eq(a, b) \ - scm_is_true(scm_string_eq(a, b, \ - SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED,SCM_UNDEFINED)) - -SCM scm_from_strbuf(strbuf* s); -SCM scm_from_strbuf_symbol(strbuf* s); - -#endif /* GUILE_TYPE_HELPERS_H */ diff --git a/src/macro.h b/src/macro.h deleted file mode 100644 index 7b620f83..00000000 --- a/src/macro.h +++ /dev/null @@ -1,134 +0,0 @@ -#ifndef MACRO_H -#define MACRO_H - -/* - * Token paste - */ -#define TP(a, b) a ## b -#define TP3(a, b, c) a ## b ## c -#define TP4(a, b, c, d) a ## b ## c ## d -#define TP5(a, b, c, d, e) a ## b ## c ## d ## e -#define TP6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f - -/* - * Get length of __VA_ARGS__ - * Borrowed fram: - * https://stackoverflow.com/a/35986932 - */ -#define VA_ARGS_NUM_PRIV(P1, P2, P3, P4, P5, P6, Pn, ...) Pn -#define VA_ARGS_NUM(...) VA_ARGS_NUM_PRIV(-1, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0) - -/* - * Templatization macros. Forms symbols on the from name, which - * looks really good in debuggers and the like. Unicode characters - * written in \U notation since C apparently doesn't support unicode - * literals. - * - * Can be nested (useful for container types). - * - * Doesn't use ASCII <>, but rather some other ᐸᐳ, meaning that it's - * not a reserved character. - * - * nameᐸTᐳ - */ -#define TEMPL(name, T) TP4(name, \U00001438 , T, \U00001433 ) -#define TEMPL2(name, T, V) TP6(name, \U00001438\U00001438 , T , \U00001433_\U00001438 , V, \U00001433\U00001433) -#define TEMPL_N(name, T, argcount) TP6(name, \U00001438 , T, _, argcount, \U00001433 ) - -/* Constructor type name */ -#define __INIT_T(T, C) TEMPL_N(init, T, C) - -/* Returns full type of constructor */ -#define INIT_F(T, ...) \ - int __INIT_T(T, VA_ARGS_NUM(__VA_ARGS__)) (T* self, ## __VA_ARGS__) - -/* - * Call the constructor of an object - * `int` part of the macro, to ensure that any attempt to call self - * function results in an error. - */ -#define INIT(T, N, ...) \ - __INIT_T(T, VA_ARGS_NUM(__VA_ARGS__)) (N, ## __VA_ARGS__) - -/* Allocate a new_ object on the HEAP */ -#define NEW(T, N, ...) \ - T* N = (T*) malloc(sizeof(*N)); \ - INIT(T, N, ## __VA_ARGS__); - -/* - * Reconstructs a object. Use with caution. - */ -#define RENEW(T, N, ...) do { \ - N = (T*) malloc(sizeof(*N)); \ - INIT(T, N, ## __VA_ARGS__); \ -} while (0) - - -/* Allocate a new_ object on the STACK */ -#define SNEW(T, N, ...) \ - T N; \ - INIT(T, & N, ## __VA_ARGS__); - -/* Destructor for type */ -#define FREE(T) TEMPL(free, T) - -/* Call destructor for type, and free object */ -#define FFREE(T, N) do { FREE(T)(N); free(N); } while (0) - -/* Declare destructor */ -#define FREE_F(T) int FREE(T) (T* self) - -/* generate reusable internal symbol */ -#define __INTER(s) TP3(__, s, __internal) -#define __INTER2(s) __INTER(__INTER(s)) -#define __INTER3(s) __INTER(__INTER(__INTER(s))) - -/* Iterator macros. */ -#define FOR(CONT_T, T, var, set) \ - PRE_FOR_ ## CONT_T (T) (T, var, set); \ - for( BEG_ ## CONT_T (T) (T, var, set); \ - END_ ## CONT_T (T) (T, var, set); \ - NXT_ ## CONT_T (T) (T, var, set)) - -/* Example int implementation - * FOR(int, i, 10) { } */ - -#define PRE_FOR_int(i, set) -#define BEG_int(i, set) int i = 0 -#define NXT_int(i, set) i++ -#define END_int(i, set) i < set - -/* - * General functions that different container types may implement. - * Actuall implementation and type signature is mostly left to - * individual implementations. - */ -#define DEEP_COPY(T) TEMPL(copy , T) -#define RESOLVE(T) TEMPL(resolve , T) -#define APPEND(T) TEMPL(append , T) -#define SIZE(T) TEMPL(size , T) -#define EMPTY(T) TEMPL(empty , T) -#define PUSH(T) TEMPL(push , T) -#define PEEK(T) TEMPL(peek , T) -#define POP(T) TEMPL(pop , T) -#define GET(T) TEMPL(get , T) -#define RESET(T) TEMPL(reset , T) -#define KEYS(T) TEMPL(keys , T) - -/* - * Formatting macros. - * Transform objects into string representation of themselves. - * buf should be a suffisiently large memmory location, if it's to - * small then bad stuff might happen. - * - * Should return the number of bytes written (like sprintf). - */ - -#define FMT_T(T) TEMPL(format , T) -#define FMT_F(T) int FMT_T(T)(T* self, char* buf, ...) -// TODO change order of buf and item -#define __FMT_HELP(item, buf, ...) ((item), (buf), VA_ARGS_NUM(__VA_ARGS__), ## __VA_ARGS__) -#define FMT(T) FMT_T(T) __FMT_HELP -#define fmtf(...) seek += sprintf(buf + seek, __VA_ARGS__) - -#endif /* MACRO_H */ diff --git a/src/main.scm b/src/main.scm deleted file mode 100755 index 408b9de0..00000000 --- a/src/main.scm +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/guile \ --e main -s -!# - -(add-to-load-path (dirname (current-filename))) - -(use-modules (parse)) - -(define (main args) - -;; (define *path* "/home/hugo/.local/var/cal/STABEN/599ca4a2f8eda362aaac598c999321dcc8004780a1d5cef36019c7e421b70b08.ics") -;; (define root (parse-cal-path *path*)) - -;; (format #t "root = ~a~%" root) - - - (format (current-error-port) "Parsing ~s~%" (cadr args)) - (let ((cal (read-tree (cadr args)))) - (format #t "cal = ~a~%" cal) - (format (current-error-port) "~a events~%" (length cal))) - - ) - - diff --git a/src/parse.c b/src/parse.c deleted file mode 100644 index 3edbd874..00000000 --- a/src/parse.c +++ /dev/null @@ -1,264 +0,0 @@ -#include "parse.h" - -#include -#include -#include - -#include "macro.h" - -#include "err.h" - -#include -#include "struct.h" -#include "guile_type_helpers.h" - -/* - +-------------------------------------------------------+ - v | - BEGIN → key -------------------------------→ ':' → value → CRLF -+-→ EOF - | ^ - v | - ';' → param-key → '=' → param-value --+ - ^ | - +------------------------------------+ - - - vcomponent := map> - line := pair - attributes := map> - - - */ - - -/* - * name *(";" param) ":" value CRLF - */ -int parse_file(char* filename, FILE* f, SCM root) { - - part_context p_ctx = p_key; - - SNEW(parse_ctx, ctx, f, filename); - - SNEW(strbuf, str); - SCM component = root; - SCM line = scm_make_vline(SCM_UNDEFINED); - SCM attr_key; /* string */ - SCM line_key = scm_from_utf8_string(""); - - SCM scm_filename = scm_from_utf8_stringn(filename, strlen(filename)); - SCM filename_key = scm_from_utf8_string("X-HNH-FILENAME"); - - char c; - while ( (c = fgetc(f)) != EOF) { - /* We have a linebreak */ - if (c == '\r' || c == '\n') { - if (fold(&ctx, c) > 0) { - /* Actuall end of line, handle value */ - /* - * The key being BEGIN means that we decend into a new component. - */ - if (string_eq(line_key, scm_from_utf8_string("BEGIN"))) { - /* key \in { VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE, ... } */ - SCM child = scm_make_vcomponent(scm_from_strbuf_symbol(&str)); - scm_add_child_x (component, child); - - scm_add_line_x(child, filename_key, scm_make_vline(scm_filename)); - - component = child; - - } else if (string_eq(line_key, scm_from_utf8_string("END"))) { - component = scm_component_parent(component); - - /* - * A regular key, value pair. Push it into to the current - * component. - */ - } else { - scm_struct_set_x(line, vline_value, scm_from_strbuf(&str)); - scm_add_line_x(component, line_key, line); - line = scm_make_vline(SCM_UNDEFINED); - } - - strbuf_soft_reset (&str); - p_ctx = p_key; - } /* Else continue on current line */ - - /* We have an escaped character */ - } else if (c == '\\') { - char esc = handle_escape (&ctx); - strbuf_append(&str, esc); - - /* Border between param {key, value} */ - } else if (p_ctx == p_param_name && c == '=') { - - /* Save the current parameter key */ - attr_key = scm_from_strbuf(&str); - p_ctx = p_param_value; - strbuf_soft_reset (&str); - - /* - * One of four cases: - * 1) end of key , start of value - * 2) ,, key , ,, param - * 3) ,, param, ,, param - * 4) ,, param, ,, value - */ - } else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) { - - /* We got a parameter value, push the current string to - * the current parameter set. */ - if (p_ctx == p_param_value) { - /* save current parameter value. */ - scm_add_attribute_x(line, attr_key, scm_from_strbuf(&str)); - strbuf_soft_reset (&str); - } - - /* - * Top level key. - * Copy the key into the current cline, and create a - * content_set for the upcomming value and (possible) - * parameters. - */ - if (p_ctx == p_key) { - line_key = scm_from_strbuf(&str); - strbuf_soft_reset (&str); - } - - if (c == ':') p_ctx = p_value; - else if (c == ';') p_ctx = p_param_name; - - /* - * Nothing interesting happened, append the read character to - * the current string. - */ - } else { - strbuf_append(&str, c); - - ++ctx.column; - ++ctx.pcolumn; - } - } - - if (! feof(f)) { - ERR_F("Error parsing errno = %i", errno); - } - /* Check to see if empty line */ - else if (str.ptr != 0) { - /* - * The standard (3.4, l. 2675) says that each icalobject must - * end with CRLF. My files however does not, so we also parse - * the end here. - * - * Actually we don't any more. - * Since the last thing in a file should always be END:VCALENDAR - * it might be a good idea to verify that. Or we could just, you - * know, not. - */ - - } - - FREE(strbuf)(&str); - - FREE(parse_ctx)(&ctx); - - return 0; -} - -int fold(parse_ctx* ctx, char c) { - int retval; - - char buf[2] = { - (c == '\n' ? '\n' : (char) fgetc(ctx->f)), - (char) fgetc(ctx->f) - }; - - ctx->pcolumn = 1; - - if (buf[0] != '\n') { - ERR_P(ctx, "expected new_line after CR"); - retval = -1; - - } else if (buf[1] == ' ' || buf[1] == '\t') { - retval = 0; - ctx->pcolumn++; - - } else if (ungetc(buf[1], ctx->f) != buf[1]) { - ERR_P(ctx, "Failed to put character back on FILE"); - retval = -2; - - } else { - retval = 1; - ++ctx->line; - ctx->column = 0; - } - - ++ctx->pline; - - return retval; -} - - -INIT_F(parse_ctx, FILE* f, char* filename) { - self->filename = (char*) calloc(sizeof(*filename), strlen(filename) + 1); - strcpy(self->filename, filename); - self->f = f; - - self->line = 0; - self->column = 0; - - self->pline = 1; - self->pcolumn = 1; - - return 0; -} - -FREE_F(parse_ctx) { - - free(self->filename); - - self->line = 0; - self->column = 0; - - return 0; -} - -char handle_escape (parse_ctx* ctx) { - char esc = fgetc(ctx->f); - - /* - * Escape character '\' and escaped token sepparated by a newline - * (since the standard for some reason allows that (!!!)) - * We are at least guaranteed that it's a folded line, so just - * unfold it and continue trying to find a token to escape. - */ - if (esc == '\r' || esc == '\n') { - int ret; - if ( (ret = fold(ctx, esc)) != 0) { - if (ret == 1) ERR_P(ctx, "ESC before not folded line"); - else ERR_P(ctx, "other error: val = %i", ret); - exit (2); - } else { - esc = fgetc(ctx->f); - } - } - - /* Escaped new_line */ - if (esc == 'n' || esc == 'N') { - esc = '\n'; - - /* "Standard" escaped character */ - } else if (esc == ';' || esc == ',' || esc == '\\') { - /* esc already contains character, do nothing */ - - /* Invalid escaped character */ - } else { - ERR_P(ctx, "Non escapable character '%c' (%i)", esc, esc); - } - - ++ctx->column; - ++ctx->pcolumn; - - /* Returns the escaped char, for appending to the current string */ - return esc; -} diff --git a/src/parse.h b/src/parse.h deleted file mode 100644 index 898abe5b..00000000 --- a/src/parse.h +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef PARSE_H -#define PARSE_H - -#include -#include - -#include "strbuf.h" -// #include "vcal.h" - -// #define TYPE vcomponent -// #include "linked_list.h" -// #undef TYPE - -/* - * The standard says that no line should be longer than 75 octets. - * This sets the default amount of memory to allocate for each string, - * but strings are reallocated when needed. - */ -#define SEGSIZE 75 - -/* - * Transfers a strbuf from src to target. - * Does this first copying the contents, followed by capping the - * target and reseting the src. - */ -#define TRANSFER(target, src) do { \ - DEEP_COPY(strbuf)((target), (src)); \ - strbuf_cap(target); \ - strbuf_soft_reset(src); \ -} while (0) - -/* - * Current context for the character consumer (parse_file). - */ -typedef enum { - p_key, p_value, p_param_name, p_param_value, p_escape -} part_context; - -/* - * Struct holding most state information needed while parsing. - * Kept together for simplicity. - */ -typedef struct { - /* Which file we are parsing, copied to all components to allow - * writebacks later */ - char* filename; - - FILE* f; - - /* Number for unfolded lines - * TODO remove this - * */ - int line; - int column; - - /* Actuall lines and columns from file */ - int pline; - int pcolumn; - -} parse_ctx; - -INIT_F(parse_ctx, FILE* f, char* filename); -FREE_F(parse_ctx); - - -/* - * Character consumer. Reads characters from stdin until end of file. - * Whenever it finds a token with a special value (such as ':', ';', - * ...) it saves it away. - * Once It has parsed a full line it calls handel_kv. Which build my - * actuall datastructure. - */ -int parse_file(char* filename, FILE* f, SCM cal); - -/* - * Input - * f: file to get characters from - * ctx: current parse context - * c: last read character - * output: - * 0: line folded - * 1: line ended - * - * A carrige return means that the current line is at an - * end. The following character should always be \n. - * However, if the first character on the next line is a - * whitespace then the two lines should be concatenated. - * - * NOTE - * The above is true according to the standard. But I have - * found files with only NL. The code below ends line on the - * first of NL or CR, and then ensures that the program thinks - * it got the expected CRNL. - */ -int fold(parse_ctx* ctx, char c); - -char handle_escape (parse_ctx* ctx); - -#endif /* PARSE_H */ diff --git a/src/strbuf.c b/src/strbuf.c deleted file mode 100644 index 1e1365d5..00000000 --- a/src/strbuf.c +++ /dev/null @@ -1,143 +0,0 @@ -#include "strbuf.h" - -#include -#include - -#include "err.h" - -INIT_F(strbuf) { - self->alloc = 0x10; - self->mem = (char*) calloc(sizeof(*self->mem), self->alloc); - self->ptr = 0; - self->len = 0; - return 0; -} - -int strbuf_realloc(strbuf* str, size_t len) { - str->mem = (char*) realloc(str->mem, len); - str->alloc = len; - return 0; -} - -FREE_F(strbuf) { - /* has already been freed */ - if (self->mem == NULL) return 1; - - free (self->mem); - self->mem = NULL; - self->alloc = 0; - self->len = 0; - return 0; -} - -/* - * Reallocates memmory for you. Returns 1 if memory was reallocated. - */ -int strbuf_append(strbuf* s, char c) { - int retval = 0; - - if (s->len + 1 > s->alloc) { - s->alloc <<= 1; - s->mem = (char*) realloc(s->mem, s->alloc); - retval = 1; - } - - s->mem[s->len] = c; - s->ptr = ++s->len; - return retval; -} - -char strbuf_pop(strbuf* s) { - char ret = s->mem[--s->len]; - s->mem[s->len + 1] = '\0'; - return ret; -} - -int strbuf_cap(strbuf* s) { - strbuf_append(s, 0); - --s->len; - return 0; -} - -int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) { - int retval = 0; - - if (dest->alloc < src->len) { - /* +1 in length is to have room for '\0'. */ - strbuf_realloc(dest, src->len + 1); - retval = 1; - } - - dest->len = src->len; - memcpy(dest->mem, src->mem, src->len); - return retval; -} - -int strbuf_cmp(strbuf* a, strbuf* b) { - if (a == NULL || a->alloc == 0 || - b == NULL || b->alloc == 0) - { - ERR("a or b not alloced"); - return -1; - } else { - return strncmp(a->mem, b->mem, a->len); - } -} - -int strbuf_c(strbuf* a, const char* b) { - if (a == NULL || a->alloc == 0) { - ERR("a not allocated"); - return -1; - } - - return strcmp(a->mem, b) == 0; -} - -char* charat(strbuf* s, unsigned int idx) { - if (idx > s->len) { - ERR("Index out of bounds"); - return (char*) -1; - } - - return &s->mem[idx]; -} - -char* strbuf_cur(strbuf* s) { - return &s->mem[s->ptr]; -} - -char* strbuf_end(strbuf* s) { - return &s->mem[s->len]; -} - -int strbuf_reset(strbuf* s) { - s->ptr = 0; - return 0; -} - - -int strbuf_soft_reset(strbuf* s) { - s->ptr = s->len = 0; - return 0; -} - -strbuf* RESOLVE(strbuf)(strbuf* dest, strbuf* new_) { - if (dest == NULL) return new_; - else return dest; -} - -FMT_F(strbuf) { - return sprintf(buf, "%s", self->mem); -} - -int SIZE(strbuf)(strbuf* self) { - return self->len; -} - -int strbuf_load(strbuf* self, const char* str) { - for (int i = 0; str[i] != '\0'; i++) { - strbuf_append(self, str[i]); - } - strbuf_cap(self); - return 0; -} diff --git a/src/strbuf.h b/src/strbuf.h deleted file mode 100644 index 0c028eb6..00000000 --- a/src/strbuf.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef STRBUF_H -#define STRBUF_H - -#include -#include -#include "macro.h" - -/* - * A high level string type which holds it's own length, how much - * memmory it has allocated for itself, and a seek pointer into the - * string. - * - * Also comes with a number of functions which allow for safe(er) - * access to the memmory. - */ -typedef struct { - char* mem; - /* TODO add support for negative ptr */ - int ptr; - unsigned int alloc; - unsigned int len; -} strbuf; - -/* - * Init strbuf to size of 10 - */ -INIT_F(strbuf); - -/* - * Like realloc, but for strbuf - */ -int strbuf_realloc(strbuf* str, size_t len); - -/* - * Free's contents of str, but keeps str. - */ -FREE_F(strbuf); - -int strbuf_cmp(strbuf* a, strbuf* b); -int strbuf_c(strbuf* a, const char* b); - -/* - * Copy contents from src to dest. - * Assumes that dest is already initialized. - */ -int DEEP_COPY(strbuf)(strbuf*, strbuf*); - -/* - * Append char to end of strbuf, determined by s->len. - * - * TODO rename this PUSH(strbuf)? - */ -int strbuf_append(strbuf* s, char c); - -char strbuf_pop(strbuf*); - -/* - * Calls strbuf_append with NULL. - */ -int strbuf_cap(strbuf* s); - -/* - * Returns a pointer to character at index. Allows mutation of the - * value pointed to by the return address. - */ -char* charat(strbuf* s, unsigned int idx); - -/* - * Same as `charat`, But returns the current character. - */ -char* strbuf_cur(strbuf* s); - -/* - * Resets the seek for strbuf to 0. - */ -int strbuf_reset(strbuf* s); - -/* - * Sets the length and seek ptr to 0, but doesn't touch the memmory. - */ -int strbuf_soft_reset(strbuf* s); - -/* - * Returns the character after the last, so where null hopefully is. - */ -char* strbuf_end(strbuf* s); - -/* - * Reallocs dest to be the same size as src, and copies the contents - * of src into dest. - */ -int strbuf_realloc_copy(strbuf* dest, strbuf* src); - -/* - * Copies contents from src to dest, also allocating dest in the - * process. dest should not be initialized before self call. - */ -int strbuf_init_copy(strbuf* dest, strbuf* src); - -strbuf* RESOLVE(strbuf)(strbuf*, strbuf*); - -FMT_F(strbuf); - -int SIZE(strbuf)(strbuf*); - -int strbuf_load(strbuf* self, const char* str); - -#endif /* STRBUF_H */ diff --git a/src/struct.h b/src/struct.h deleted file mode 100644 index a66dc201..00000000 --- a/src/struct.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef STRUCT_H -#define STRUCT_H - -#include - -#define vcomponent_type scm_from_uint8(0) -#define vcomponent_children scm_from_uint8(1) -#define vcomponent_parent scm_from_uint8(2) -#define vcomponent_lines scm_from_uint8(3) - -#define scm_component_parent(component) \ - scm_struct_ref (component, vcomponent_parent) - -#define vline_value scm_from_uint8(0) -#define vline_attributes scm_from_uint8(1) - -SCM scm_make_vcomponent(SCM); -SCM scm_add_line_x (SCM, SCM, SCM); -SCM scm_add_child_x (SCM, SCM); -SCM scm_make_vline (SCM); -SCM scm_add_attribute_x (SCM, SCM, SCM); - -#endif /* STRUCT_H */ diff --git a/src/struct.scm.c b/src/struct.scm.c deleted file mode 100644 index 051faf63..00000000 --- a/src/struct.scm.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "struct.h" - -#include - -#include "parse.h" -#include "calendar.h" - -SCM vcomponent_vtable; -SCM vline_vtable; - -SCM_DEFINE(scm_make_vcomponent, "make-vcomponent", 0, 1, 0, - (SCM type), - "") -{ - - if (SCM_UNBNDP (type) || scm_is_false (type)) - type = scm_from_utf8_symbol("VIRTUAL"); - - return scm_make_struct_no_tail - (vcomponent_vtable, - scm_list_4(type, SCM_EOL, SCM_BOOL_F, - scm_make_hash_table(scm_from_int(0x10)))); -} - - - -SCM_DEFINE(scm_parse_cal_path, "parse-cal-path", 1, 0, 0, - (SCM path), - "") -{ - SCM root = scm_make_vcomponent(SCM_UNDEFINED); - - char* p = scm_to_utf8_stringn(path, NULL); - read_vcalendar(root, p); - free(p); - - return root; -} - -SCM_DEFINE(scm_add_line_x, "add-line!", 3, 0, 0, - (SCM vcomponent, SCM key, SCM line), - "") -{ - scm_hash_set_x (scm_struct_ref(vcomponent, vcomponent_lines), key, line); - return SCM_UNSPECIFIED; -} - - -SCM_DEFINE(scm_add_child_x, "add-child!", 2, 0, 0, - (SCM vcomponent, SCM child), - "") -{ - scm_struct_set_x (child, vcomponent_parent, vcomponent); - scm_struct_set_x (vcomponent, vcomponent_children, - scm_cons (child, scm_struct_ref (vcomponent, vcomponent_children))); - - return SCM_UNSPECIFIED; -} - - -SCM_DEFINE(scm_make_vline, "make-vline", 0, 1, 0, - (SCM value), "") -{ - - if (SCM_UNBNDP (value)) value = SCM_BOOL_F; - - return scm_make_struct_no_tail - (vline_vtable, - scm_list_2(value, scm_make_hash_table(scm_from_int(0x10)))); -} - - -SCM_DEFINE(scm_add_attribute_x, "add-attribute!", 3, 0, 0, - (SCM vline, SCM key, SCM value), - "") -{ - SCM table = scm_struct_ref (vline, vline_attributes); - scm_hash_set_x (table, key, - scm_cons(value, scm_hash_ref(table, key, SCM_EOL))); - return SCM_UNSPECIFIED; -} - -void init_lib (void) { - SCM str = scm_from_utf8_string("pr" "pw" "pw" "pr"); - vcomponent_vtable = scm_make_vtable(str, SCM_BOOL_F); - scm_set_struct_vtable_name_x (vcomponent_vtable, scm_from_utf8_symbol("vcomponent")); - - vline_vtable = - scm_make_vtable(scm_from_utf8_string("pw" "pw"), - SCM_BOOL_F); - scm_set_struct_vtable_name_x (vline_vtable, scm_from_utf8_symbol("vline")); - -#ifndef SCM_MAGIC_SNARFER -#include "struct.x" -#endif -} -- cgit v1.2.3