about summary refs log tree commit diff stats
path: root/092persist.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
commitc603cd6cef43100aa83a62e15f96fd54c9fb987e (patch)
treed8631b1964a0200d170a996314f6e023686ff8de /092persist.cc
parent05d3592047a76db4cc8d77102508c21ca1b86e7b (diff)
downloadmu-c603cd6cef43100aa83a62e15f96fd54c9fb987e.tar.gz
2430 - make room for more transforms
Diffstat (limited to '092persist.cc')
-rw-r--r--092persist.cc122
1 files changed, 122 insertions, 0 deletions
diff --git a/092persist.cc b/092persist.cc
new file mode 100644
index 00000000..774ce08f
--- /dev/null
+++ b/092persist.cc
@@ -0,0 +1,122 @@
+//: Dead simple persistence.
+//:   'restore' - reads string from a file
+//:   'save' - writes string to a file
+
+:(before "End Primitive Recipe Declarations")
+RESTORE,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "restore", RESTORE);
+:(before "End Primitive Recipe Checks")
+case RESTORE: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise_error << maybe(get(Recipe, r).name) << "'restore' requires exactly one ingredient, but got " << inst.to_string() << '\n' << end();
+    break;
+  }
+  string filename;
+  if (is_literal_string(inst.ingredients.at(0))) {
+    ;
+  }
+  else if (is_mu_string(inst.ingredients.at(0))) {
+    ;
+  }
+  else {
+    raise_error << maybe(get(Recipe, r).name) << "first ingredient of 'restore' should be a string, but got " << inst.ingredients.at(0).to_string() << '\n' << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case RESTORE: {
+  string filename;
+  if (is_literal_string(current_instruction().ingredients.at(0))) {
+    filename = current_instruction().ingredients.at(0).name;
+  }
+  else if (is_mu_string(current_instruction().ingredients.at(0))) {
+    filename = read_mu_string(ingredients.at(0).at(0));
+  }
+  if (Current_scenario) {
+    // do nothing in tests
+    products.resize(1);
+    products.at(0).push_back(0);
+    break;
+  }
+  string contents = slurp("lesson/"+filename);
+  products.resize(1);
+  if (contents.empty())
+    products.at(0).push_back(0);
+  else
+    products.at(0).push_back(new_mu_string(contents));
+  break;
+}
+
+:(code)
+string slurp(const string& filename) {
+  ostringstream result;
+  ifstream fin(filename.c_str());
+  fin.peek();
+  if (!fin) return result.str();  // don't bother checking errno
+  const int N = 1024;
+  char buf[N];
+  while (!fin.eof()) {
+    bzero(buf, N);
+    fin.read(buf, N-1);  // leave at least one null
+    result << buf;
+  }
+  fin.close();
+  return result.str();
+}
+
+:(before "End Primitive Recipe Declarations")
+SAVE,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "save", SAVE);
+:(before "End Primitive Recipe Checks")
+case SAVE: {
+  if (SIZE(inst.ingredients) != 2) {
+    raise_error << maybe(get(Recipe, r).name) << "'save' requires exactly two ingredients, but got " << inst.to_string() << '\n' << end();
+    break;
+  }
+  if (is_literal_string(inst.ingredients.at(0))) {
+    ;
+  }
+  else if (is_mu_string(inst.ingredients.at(0))) {
+    ;
+  }
+  else {
+    raise_error << maybe(get(Recipe, r).name) << "first ingredient of 'save' should be a string, but got " << inst.ingredients.at(0).to_string() << '\n' << end();
+    break;
+  }
+  if (!is_mu_string(inst.ingredients.at(1))) {
+    raise_error << maybe(get(Recipe, r).name) << "second ingredient of 'save' should be an address:array:character, but got " << inst.ingredients.at(1).to_string() << '\n' << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case SAVE: {
+  if (Current_scenario) break;  // do nothing in tests
+  string filename;
+  if (is_literal_string(current_instruction().ingredients.at(0))) {
+    filename = current_instruction().ingredients.at(0).name;
+  }
+  else if (is_mu_string(current_instruction().ingredients.at(0))) {
+    filename = read_mu_string(ingredients.at(0).at(0));
+  }
+  ofstream fout(("lesson/"+filename).c_str());
+  string contents = read_mu_string(ingredients.at(1).at(0));
+  fout << contents;
+  fout.close();
+  if (!exists("lesson/.git")) break;
+  // bug in git: git diff -q messes up --exit-code
+  // explicitly say '--all' for git 1.9
+  int status = system("cd lesson; git add --all .; git diff HEAD --exit-code >/dev/null || git commit -a -m . >/dev/null");
+  if (status != 0)
+    raise_error << "error in commit: contents " << contents << '\n' << end();
+  break;
+}
+
+:(code)
+bool exists(const string& filename) {
+  struct stat dummy;
+  return 0 == stat(filename.c_str(), &dummy);
+}