Added logf function to write to message log. Write warnings of unparsable files into the message log.

Sat, 16 Mar 2013 01:32:47 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 16 Mar 2013 01:32:47 +0200
changeset 13
3955ff2a7d72
parent 12
8f6de46a27e2
child 14
6d9d8efae2f8

Added logf function to write to message log. Write warnings of unparsable files into the message log.

common.h file | annotate | diff | comparison | revisions
gui.h file | annotate | diff | comparison | revisions
io.cpp file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
main.cpp file | annotate | diff | comparison | revisions
misc.cpp file | annotate | diff | comparison | revisions
misc.h file | annotate | diff | comparison | revisions
--- a/common.h	Sat Mar 16 00:35:36 2013 +0200
+++ b/common.h	Sat Mar 16 01:32:47 2013 +0200
@@ -23,6 +23,12 @@
 #include "str.h"
 #include "config.h"
 
+#ifdef __GNUC__
+ #define FORMAT_PRINTF(M,N) __attribute__ ((format (printf, M, N)))
+#else
+ #define FORMAT_PRINTF(M,N)
+#endif // __GNUC__
+
 using std::vector;
 
 class LDForgeWindow;
@@ -95,6 +101,17 @@
 static const double pi = 3.14159265358979323846f;
 
 // main.cpp
+enum logtype_e {
+	LOG_Normal,
+	LOG_Success,
+	LOG_Info,
+	LOG_Warning,
+	LOG_Error,
+};
+
+void logf (const char* fmt, ...) FORMAT_PRINTF (1, 2);
+void logf (logtype_e eType, const char* fmt, ...) FORMAT_PRINTF (2, 3);
+
 extern OpenFile* g_CurrentFile;
 extern bbox g_BBox;
 extern LDForgeWindow* g_qWindow;
--- a/gui.h	Sat Mar 16 00:35:36 2013 +0200
+++ b/gui.h	Sat Mar 16 01:32:47 2013 +0200
@@ -21,6 +21,7 @@
 	
 	// Message log
 	QTextEdit* qMessageLog;
+	str zMessageLogHTML;
 	
 	// Menus
 	QMenu* qFileMenu, *qEditMenu, *qInsertMenu, *qHelpMenu;
--- a/io.cpp	Sat Mar 16 00:35:36 2013 +0200
+++ b/io.cpp	Sat Mar 16 01:32:47 2013 +0200
@@ -30,14 +30,18 @@
 // Opens the given file and parses the LDraw code within.
 // =============================================================================
 OpenFile* IO_OpenLDrawFile (str path) {
+	logf ("Opening %s...\n", path.chars());
+	
 	FILE* fp = fopen (path.chars (), "r");
 	
 	if (!fp) {
-		printf ("Couldn't open %s!\n", path.chars ());
+		logf (LOG_Error, "Couldn't open %s: %s\n", path.chars (), strerror (errno));
 		return NULL;
 	}
 	
 	OpenFile* load = new OpenFile;
+	ulong numWarnings = 0;
+	
 	load->zFileName = path;
 	
 	vector<str> lines;
@@ -56,8 +60,18 @@
 	
 	fclose (fp);
 	
-	for (ulong i = 0; i < lines.size(); ++i)
-		load->objects.push_back (ParseLine (lines[i]));
+	for (ulong i = 0; i < lines.size(); ++i) {
+		LDObject* obj = ParseLine (lines[i]);
+		load->objects.push_back (obj);
+		
+		// Check for warnings
+		if (obj->getType() == OBJ_Gibberish) {
+			logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
+				i, static_cast<LDGibberish*> (obj)->zReason.chars());
+			logf (LOG_Warning, "- Line was: %s\n", lines[i].chars());
+			numWarnings++;
+		}
+	}
 	
 	g_LoadedFiles.push_back (load);
 	g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1];
@@ -69,6 +83,9 @@
 	g_qWindow->buildObjList ();
 	g_qWindow->setTitle ();
 	
+	logf (LOG_Success, "File %s parsed successfully (%lu warnings).\n",
+		path.chars(), numWarnings);
+	
 	return g_CurrentFile;
 }
 
--- a/ldtypes.cpp	Sat Mar 16 00:35:36 2013 +0200
+++ b/ldtypes.cpp	Sat Mar 16 01:32:47 2013 +0200
@@ -1,5 +1,6 @@
 #include "common.h"
 #include "ldtypes.h"
+#include "io.h"
 
 const char* g_saObjTypeNames[] = {
 	"unidentified",
@@ -64,4 +65,17 @@
 
 LDVertex::LDVertex () {
 	
+}
+
+ulong LDObject::getIndex () {
+	if (!g_CurrentFile)
+		return -1u;
+	
+	// TODO: shouldn't rely on g_CurrentFile
+	for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
+		if (g_CurrentFile->objects[i] == this)
+			return i;
+	}
+	
+	return -1u;
 }
\ No newline at end of file
--- a/ldtypes.h	Sat Mar 16 00:35:36 2013 +0200
+++ b/ldtypes.h	Sat Mar 16 01:32:47 2013 +0200
@@ -41,6 +41,10 @@
 public:
 	LDObject ();
 	
+	// Index (i.e. line number) of this object
+	ulong getIndex ();
+	
+	// Type enumerator of this object
 	virtual LDObjectType_e getType () const {
 		return OBJ_Unidentified;
 	};
--- a/main.cpp	Sat Mar 16 00:35:36 2013 +0200
+++ b/main.cpp	Sat Mar 16 01:32:47 2013 +0200
@@ -50,4 +50,81 @@
 		getCoordinateRep (x).chars(),
 		getCoordinateRep (y).chars(),
 		getCoordinateRep (z).chars());
+}
+
+// =============================================================================
+// void logVA (logtype_e, const char*, va_list) [static]
+//
+// Common code for the two logfs
+// =============================================================================
+static void logVA (logtype_e eType, const char* fmt, va_list va) {
+	str zText;
+	char* sBuffer;
+	
+	// Log it to standard output
+	vprintf (fmt, va);
+	
+	sBuffer = vdynformat (fmt, va, 128);
+	zText = sBuffer;
+	delete[] sBuffer;
+	
+	
+	// Replace some things out with HTML entities
+	zText.replace ("<", "&lt;");
+	zText.replace (">", "&gt;");
+	zText.replace ("\n", "<br />");
+	
+	str zHTML;
+	
+	switch (eType) {
+	case LOG_Normal:
+		zHTML = zText;
+		break;
+	
+	case LOG_Error:
+		zHTML.format ("<span style=\"color: #F8F8F8; background-color: #800\"><b>[ERROR]</b> %s</span>",
+			zText.chars());
+		break;
+	
+	case LOG_Info:
+		zHTML.format ("<span style=\"color: #04F\"><b>[INFO]</b> %s</span>",
+			zText.chars());
+		break;
+	
+	case LOG_Success:
+		zHTML.format ("<span style=\"color: #6A0\"><b>[SUCCESS]</b> %s</span>",
+			zText.chars());
+		break;
+	
+	case LOG_Warning:
+		zHTML.format ("<span style=\"color: #C50\"><b>[WARNING]</b> %s</span>",
+			zText.chars());
+		break;
+	}
+	
+	g_qWindow->zMessageLogHTML += zHTML;
+	g_qWindow->qMessageLog->setHtml (g_qWindow->zMessageLogHTML);
+}
+
+
+// =============================================================================
+// logf (const char*, ...)
+// logf (logtype_e eType, const char*, ...)
+//
+// Outputs a message into the message log
+// =============================================================================
+void logf (const char* fmt, ...) {
+	va_list va;
+	
+	va_start (va, fmt);
+	logVA (LOG_Normal, fmt, va);
+	va_end (va);
+}
+
+void logf (logtype_e eType, const char* fmt, ...) {
+	va_list va;
+	
+	va_start (va, fmt);
+	logVA (eType, fmt, va);
+	va_end (va);
 }
\ No newline at end of file
--- a/misc.cpp	Sat Mar 16 00:35:36 2013 +0200
+++ b/misc.cpp	Sat Mar 16 01:32:47 2013 +0200
@@ -1,19 +1,19 @@
 #include "common.h"
 #include <math.h>
 
-double GetWordFloat (str& s, int n) {
+double GetWordFloat (str& s, const ushort n) {
 	return atof ((s / " ")[n]);
 }
 
-long GetWordInt (str& s, int n) {
+long GetWordInt (str& s, const ushort n) {
 	return atol ((s / " ")[n]);
 }
 
-vertex ParseVertex (str& s, int n) {
+vertex ParseVertex (str& s, const ushort n) {
 	vertex v;
 	v.x = GetWordFloat (s, n);
-	v.y = GetWordFloat (s, n+1);
-	v.z = GetWordFloat (s, n+2);
+	v.y = GetWordFloat (s, n + 1);
+	v.z = GetWordFloat (s, n + 2);
 	
 	return v;
 }
--- a/misc.h	Sat Mar 16 00:35:36 2013 +0200
+++ b/misc.h	Sat Mar 16 01:32:47 2013 +0200
@@ -8,9 +8,9 @@
 	return (zString / " ")[ulIndex];
 }
 
-double GetWordFloat (str& s, int n);
-long GetWordInt (str& s, int n);
-vertex ParseVertex (str& s, int n);
+double GetWordFloat (str& s, const ushort n);
+long GetWordInt (str& s, const ushort n);
+vertex ParseVertex (str& s, const ushort n);
 void StripWhitespace (str& s);
 
 #endif // __MISC_H__
\ No newline at end of file

mercurial