From 762b099ef6c6ad5b6b61d29e473baa6df8d64ab9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 18 Jul 2015 15:22:49 -0700 Subject: 1818 --- html/082persist.cc.html | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 html/082persist.cc.html (limited to 'html/082persist.cc.html') diff --git a/html/082persist.cc.html b/html/082persist.cc.html new file mode 100644 index 00000000..cf056dba --- /dev/null +++ b/html/082persist.cc.html @@ -0,0 +1,109 @@ + + + + +Mu - 082persist.cc + + + + + + + + + + +
+//: 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")
+Recipe_ordinal["restore"] = RESTORE;
+:(before "End Primitive Recipe Implementations")
+case RESTORE: {
+  if (!scalar(ingredients.at(0)))
+    raise << "restore: illegal operand " << current_instruction().ingredients.at(0).to_string() << '\n';
+  products.resize(1);
+  products.at(0).push_back(new_string(slurp("lesson/"+current_instruction().ingredients.at(0).name)));
+  break;
+}
+
+:(code)
+string slurp(const string& filename) {
+//?   cerr << filename << '\n'; //? 1
+  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();
+//?   cerr << "=> " << result.str(); //? 1
+  return result.str();
+}
+
+:(before "End Primitive Recipe Declarations")
+SAVE,
+:(before "End Primitive Recipe Numbers")
+Recipe_ordinal["save"] = SAVE;
+:(before "End Primitive Recipe Implementations")
+case SAVE: {
+  if (!scalar(ingredients.at(0)))
+    raise << "save: illegal operand 0 " << current_instruction().ingredients.at(0).to_string() << '\n';
+  string filename = current_instruction().ingredients.at(0).name;
+  if (!is_literal(current_instruction().ingredients.at(0))) {
+    ostringstream tmp;
+    tmp << ingredients.at(0).at(0);
+    filename = tmp.str();
+  }
+  ofstream fout(("lesson/"+filename).c_str());
+  if (!scalar(ingredients.at(1)))
+    raise << "save: illegal operand 1 " << current_instruction().ingredients.at(1).to_string() << '\n';
+  string contents = to_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
+  int status = system("cd lesson; git add .; git diff HEAD --exit-code >/dev/null || git commit -a -m . >/dev/null");
+  if (status != 0)
+    raise << "error in commit: contents " << contents << '\n';
+  break;
+}
+
+:(code)
+bool exists(const string& filename) {
+  struct stat dummy;
+  return 0 == stat(filename.c_str(), &dummy);
+}
+
+:(before "End Includes")
+#include<sys/stat.h>
+
+ + + -- cgit 1.4.1-2-gfad0