From e552571b1ea6a85ebd5a39f148518f95925b9de6 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 9 Feb 2022 09:18:05 -0800 Subject: standardize key order in .tlv files This will eliminate some spurious git diffs I keep having to clean up. --- src/tlv.c | 58 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/tlv.c b/src/tlv.c index e9494af..a01f9e9 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -128,6 +128,30 @@ void emit_multiline_string(FILE* out, const char* value) { } } +static const char* special_history_keys[] = { + "__teliva_timestamp", + "__teliva_note", + "__teliva_undo", + NULL, +}; + +/* save key and its value at top of stack to out + * no stack side effects */ +static void save_tlv_key(lua_State* L, const char* key, FILE* out) { + if (strcmp(key, "__teliva_undo") == 0) { + fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1)); + return; + } + const char* value = lua_tostring(L, -1); + if (strchr(value, ' ') || strchr(value, '\n')) { + fprintf(out, "%s:\n", key); + emit_multiline_string(out, value); + } + else { + fprintf(out, "%s: %s\n", key, value); + } +} + void save_tlv(lua_State* L, char* filename) { lua_getglobal(L, "teliva_program"); int history_array = lua_gettop(L); @@ -162,23 +186,24 @@ void save_tlv(lua_State* L, char* filename) { lua_rawgeti(L, history_array, i); int table = lua_gettop(L); int first = 1; + // standardize order of special keys + for (int k = 0; special_history_keys[k]; ++k) { + lua_getfield(L, table, special_history_keys[k]); + if (!lua_isnil(L, -1)) { + if (first) fprintf(out, "- "); + else fprintf(out, " "); + first = 0; + save_tlv_key(L, special_history_keys[k], out); + } + lua_pop(L, 1); + } for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { + if (is_special_history_key(lua_tostring(L, -2))) + continue; if (first) fprintf(out, "- "); else fprintf(out, " "); first = 0; - const char* key = lua_tostring(L, -2); - if (strcmp(key, "__teliva_undo") == 0) { - fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1)); - continue; - } - const char* value = lua_tostring(L, -1); - if (strchr(value, ' ') || strchr(value, '\n')) { - fprintf(out, "%s:\n", key); - emit_multiline_string(out, value); - } - else { - fprintf(out, "%s: %s\n", key, value); - } + save_tlv_key(L, lua_tostring(L, -2), out); } lua_pop(L, 1); } @@ -187,13 +212,6 @@ void save_tlv(lua_State* L, char* filename) { lua_pop(L, 1); } -static const char* special_history_keys[] = { - "__teliva_timestamp", - "__teliva_undo", - "__teliva_note", - NULL, -}; - int is_special_history_key(const char* key) { for (const char** curr = special_history_keys; *curr != NULL; ++curr) { if (strcmp(*curr, key) == 0) -- cgit 1.4.1-2-gfad0