changeset 63:4b9ffe15a87d

Refactor Load/LoadSys
author Thibaut GIRKA <thib@sitedethib.com>
date Sat, 06 Feb 2010 17:29:33 +0100
parents 3b1593186f12
children 045ca45f9610
files scn2k/scn2k_cmd.cc scn2k/scn2k_flags.h
diffstat 2 files changed, 80 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/scn2k/scn2k_cmd.cc
+++ b/scn2k/scn2k_cmd.cc
@@ -223,10 +223,82 @@ string Flags::Str(int type, unsigned int
 	return "";
 }
 
+void Flags::Load(const char* save) {
+	Load(save, false);
+}
+
+void Flags::LoadSys(const char* save) {
+	Load(save, true);
+}
+
+void Flags::Load(const char* save, bool sys) {
+	int i, j;
+	int start, end;
+	std::string *var_str;
+	char string_field[] = "V<?>[%04d]=";
+
+	start = (sys) ? (TYPE_NONSYSVARMAX + 1) : 0;
+	end = (sys) ? (TYPE_NONSYSVARMAX + 2) : TYPE_NONSYSVARMAX;
+
+	var_str = (sys) ? sys_str : str;
+	string_field[2] = (sys) ? 'M' : 'C';
+
+	for (i=start; i <= end; i++) {
+		for (j=0; j < 2000; j++) {
+			var[i][j] = 0;
+		}
+	}
+	for (j=0; j<2000; j++) {
+		var_str[j] = "";
+	}
+	sys = 0;
+
+	save = strstr(save, "\n[Flags]\n");
+
+	if (save) {
+		save += strlen("\n[Flags]\n");
+		do {
+			if (save[0] == '[') break; // next section
+			if (strncmp(save, string_field, 2) == 0) {
+				if (strncmp(save, string_field, 5) == 0) { // string
+					char buf[1024];
+					int n;
+					if (sscanf(save, string_field, &n) == 1) {
+						const char* s = strchr(save, '=');
+						s++;
+						const char* send = strchr(s, '\n');
+						int slen = send - s;
+						strncpy(buf, s, slen);
+						buf[slen] = 0;
+						if (n >= 0 && n < 2000)
+							var_str[n] = buf;
+					}
+				} else if (save[2] >= '0' && save[2] <= '9') {
+					int c, n, v;
+					if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
+						if (n >= 0 && n < 2000)
+						{
+							if (!sys && c >= 0 && c <= TYPE_NONSYSVARMAX)
+								var[c][n] = v;
+							else if (sys && c == TYPE_NONSYSVARMAX + 1)
+								var[TYPE_NONSYSVARMAX + 1][n] = v;
+							else if (sys && c == 25)
+								var[7][n] = v;
+						}
+					}
+				}
+			}
+			save = strchr(save, '\n');
+			if (save)
+				save++;
+		} while (save);
+	}
+}
+
 void Flags::Save(string& save) {
 	char buf[1024];
 	save = "\n[Flags]\n";
-	int i,j;
+	int i, j;
 	for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
 		for (j=0; j<2000; j++) {
 			if (var[i][j] != 0) {
@@ -243,61 +315,16 @@ void Flags::Save(string& save) {
 	}
 }
 
-void Flags::Load(const char* save) {
-	int i,j;
-	for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
-		for (j=0; j<2000; j++) {
-			var[i][j] = 0;
-		}
-	}
-	sys = 0;
-	for (j=0; j<2000; j++) {
-		str[j] = "";
-	}
-
-	save = strstr(save, "\n[Flags]\n");
-
-	if (save) {
-		save += strlen("\n[Flags]\n");
-		do {
-			if (save[0] == '[') break; // next section
-			if (strncmp(save, "V<",2) == 0) {
-				if (strncmp(save, "V<C>[",5) == 0) { // string
-					char buf[1024];
-					int n;
-					if (sscanf(save, "V<C>[%04d]=",&n) == 1) {
-						const char* s = strchr(save, '=');
-						s++;
-						const char* send = strchr(s, '\n');
-						int slen = send - s;
-						strncpy(buf, s, slen);
-						buf[slen] = 0;
-						if (n >= 0 && n < 2000) str[n] = buf;
-					}
-				} else if (save[2] >= '0' && save[2] <= '9') {
-					int c,n,v;
-					if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
-						if (c >= 0 && c <= TYPE_NONSYSVARMAX && n >= 0 && n < 2000)
-							var[c][n] = v;
-					}
-				}
-			}
-			save = strchr(save, '\n');
-			if (save) save++;
-		} while (save);
-	}
-}
-
 void Flags::SaveSys(string& save) { //FIXME: see how to factorize with Save
 	char buf[1024];
 	int j;
 	save = "\n[Flags]\n";
-		for (j=0; j<2000; j++) {
+	for (j=0; j<2000; j++) {
 		if (var[6][j] != 0) {
-			sprintf(buf, "V<6>[%04d]=%d\n",j,var[6][j]);
-				save += buf;
-			}
+			sprintf(buf, "V<6>[%04d]=%d\n", j, var[6][j]);
+			save += buf;
 		}
+	}
 	for (j=0; j<2000; j++) {
 		if (var[7][j] != 0) {
 			sprintf(buf, "V<25>[%04d]=%d\n",j,var[7][j]);
@@ -312,53 +339,6 @@ void Flags::SaveSys(string& save) { //FI
 	}
 }
 
-void Flags::LoadSys(const char* save) { //FIXME: Same as Save and SaveSys
-	int i,j;
-	for (i=6; i<=7; i++) {
-		for (j=0; j<2000; j++) {
-			var[i][j] = 0;
-		}
-	}
-	for (j=0; j<2000; j++) {
-		sys_str[j] = "";
-	}
-	sys = 0;
-
-	save = strstr(save, "\n[Flags]\n");
-
-	if (save) {
-		save += strlen("\n[Flags]\n");
-		do {
-			if (save[0] == '[') break; // next section
-			if (strncmp(save, "V<",2) == 0) {
-				if (strncmp(save, "V<M>[",5) == 0) { // string
-					char buf[1024];
-					int n;
-					if (sscanf(save, "V<M>[%04d]=",&n) == 1) {
-						const char* s = strchr(save, '=');
-						s++;
-						const char* send = strchr(s, '\n');
-						int slen = send - s;
-						strncpy(buf, s, slen);
-						buf[slen] = 0;
-						if (n >= 0 && n < 2000) sys_str[n] = buf;
-					}
-				} else if (save[2] >= '0' && save[2] <= '9') {
-					int c,n,v;
-					if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
-						if (c == 6 && n >= 0 && n < 2000)
-							var[6][n] = v;
-						else if (c == 25 && n >= 0 && n < 2000)
-							var[7][n] = v;
-					}
-				}
-			}
-			save = strchr(save, '\n');
-			if (save) save++;
-		} while (save);
-	}
-}
-
 bool Flags::Exec(Cmd& cmd) {
 	if (cmd.cmd_type == CMD_FLAGS) { // ÂåÆþ±é»»
 		if (cmd.args.size() != 2) return false;
--- a/scn2k/scn2k_flags.h
+++ b/scn2k/scn2k_flags.h
@@ -63,11 +63,12 @@ class Flags {
 
 		bool Exec(class Cmd& cmd);
 
-		void Save(std::string& str);
 		void Load(const char* str);
+		void LoadSys(const char* str);
+		void Load(const char* str, bool sys);
 
+		void Save(std::string& str);
 		void SaveSys(std::string& str);
-		void LoadSys(const char* str);
 };
 
 #endif