- when saving, prepare the data first into a buffer and then write to disk, minimizing the time needed to perform the I/O operation.

Thu, 29 May 2014 17:54:43 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 29 May 2014 17:54:43 +0300
changeset 781
aa823ba3241d
parent 780
b0d84fb70ea5
child 782
70526ee626a0

- when saving, prepare the data first into a buffer and then write to disk, minimizing the time needed to perform the I/O operation.

src/ldDocument.cc file | annotate | diff | comparison | revisions
--- a/src/ldDocument.cc	Thu May 22 18:01:19 2014 +0300
+++ b/src/ldDocument.cc	Thu May 29 17:54:43 2014 +0300
@@ -725,19 +725,16 @@
 //
 bool LDDocument::save (String savepath)
 {
+	if (isImplicit())
+		return false;
+
 	if (not savepath.length())
 		savepath = fullPath();
 
-	QFile f (savepath);
-
-	if (not f.open (QIODevice::WriteOnly))
-		return false;
-
 	// If the second object in the list holds the file name, update that now.
-	// Only do this if the file is explicitly open.
 	LDObjectPtr nameObject = getObject (1);
 
-	if (not isImplicit() && nameObject != null && nameObject->type() == LDObject::EComment)
+	if (nameObject != null && nameObject->type() == LDObject::EComment)
 	{
 		LDCommentPtr nameComment = nameObject.staticCast<LDComment>();
 
@@ -749,12 +746,19 @@
 		}
 	}
 
+	QByteArray data;
+
 	// File is open, now save the model to it. Note that LDraw requires files to
 	// have DOS line endings, so we terminate the lines with \r\n.
 	for (LDObjectPtr obj : objects())
-		f.write ((obj->asText() + "\r\n").toUtf8());
+		data.append ((obj->asText() + "\r\n").toUtf8());
+
+	QFile f (savepath);
 
-	// File is saved, now clean up.
+	if (not f.open (QIODevice::WriteOnly))
+		return false;
+
+	f.write (data);
 	f.close();
 
 	// We have successfully saved, update the save position now.

mercurial