src/primitives.cc

changeset 609
a8dc74a809c6
parent 606
3dd6f343ec06
child 610
f8244301110d
--- a/src/primitives.cc	Wed Jan 08 13:57:10 2014 +0200
+++ b/src/primitives.cc	Wed Jan 08 21:43:46 2014 +0200
@@ -59,9 +59,9 @@
 	PrimitiveCategory::loadCategories();
 
 	// Try to load prims.cfg
-	File conf (Config::filepath ("prims.cfg"), File::Read);
+	QFile conf (Config::filepath ("prims.cfg"));
 
-	if (!conf)
+	if (!conf.open (QIODevice::ReadOnly))
 	{
 		// No prims.cfg, build it
 		PrimitiveLister::start();
@@ -69,8 +69,9 @@
 	else
 	{
 		// Read primitives from prims.cfg
-		for (QString line : conf)
+		while (conf.atEnd() == false)
 		{
+			QString line = conf.readLine();
 			int space = line.indexOf (" ");
 
 			if (space == -1)
@@ -129,20 +130,24 @@
 // -----------------------------------------------------------------------------
 void PrimitiveLister::work()
 {
-	int j = min (m_i + 300, m_files.size());
-	log ("PrimitiveLister::work: %1 -> %2\n", m_i, j);
+	int j = min (m_i + 100, m_files.size());
 
 	for (; m_i < j; ++m_i)
 	{
 		QString fname = m_files[m_i];
-		File f (fname, File::Read);
+		QFile f (fname);
+
+		if (f.open (QIODevice::ReadOnly))
+			continue;
+
 		Primitive info;
 		info.name = fname.mid (m_baselen + 1);  // make full path relative
 		info.name.replace ('/', '\\');  // use DOS backslashes, they're expected
 		info.cat = null;
+		QByteArray titledata = f.readLine();
 
-		if (!f.readLine (info.title))
-			info.title = "";
+		if (titledata != QByteArray())
+			info.title = QString::fromUtf8 (titledata);
 
 		info.title = info.title.simplified();
 
@@ -158,12 +163,18 @@
 	if (m_i == m_files.size())
 	{
 		// Done with primitives, now save to a config file
-		File conf (Config::filepath ("prims.cfg"), File::Write);
+		QString path = Config::filepath ("prims.cfg");
+		QFile conf (path);
 
-		for (Primitive& info : m_prims)
-			fprint (conf, "%1 %2\n", info.name, info.title);
+		if (!conf.open (QIODevice::WriteOnly | QIODevice::Text))
+			critical (fmt ("Couldn't write %1: %2", path, conf.errorString()));
+		else
+		{
+			for (Primitive& info : m_prims)
+				fprint (conf, "%1 %2\n", info.name, info.title);
 
-		conf.close();
+			conf.close();
+		}
 
 		g_primitives = m_prims;
 		PrimitiveCategory::populateCategories();
@@ -265,67 +276,71 @@
 		delete cat;
 
 	g_PrimitiveCategories.clear();
-	File f (Config::dirpath() + "primregexps.cfg", File::Read);
+	QString path = Config::dirpath() + "primregexps.cfg";
+
+	if (!QFile::exists (path))
+		path = ":/data/primitive-categories.cfg";
 
-	if (!f)
-		f.open (":/data/primitive-categories.cfg", File::Read);
+	QFile f (path);
 
-	if (!f)
+	if (!f.open (QIODevice::ReadOnly))
 	{
-		critical (QObject::tr ("Failed to open primitive categories!"));
+		critical (fmt (QObject::tr ("Failed to open primitive categories: %1"), f.errorString()));
 		return;
 	}
 
-	if (f)
-	{
-		PrimitiveCategory* cat = null;
+	PrimitiveCategory* cat = null;
 
-		for (QString line : f)
-		{
-			int colon;
+	while (f.atEnd() == false)
+	{
+		QString line = f.readLine();
+		int colon;
+
+		if (line.endsWith ("\n"))
+			line.chop (1);
+
+		if (line.length() == 0 || line[0] == '#')
+			continue;
 
-			if (line.length() == 0 || line[0] == '#')
-				continue;
+		if ((colon = line.indexOf (":")) == -1)
+		{
+			if (cat && cat->isValidToInclude())
+				g_PrimitiveCategories << cat;
 
-			if ((colon = line.indexOf (":")) == -1)
-			{
-				if (cat && cat->isValidToInclude())
-					g_PrimitiveCategories << cat;
+			cat = new PrimitiveCategory (line);
+		}
+		elif (cat != null)
+		{
+			QString cmd = line.left (colon);
+			ERegexType type = EFilenameRegex;
 
-				cat = new PrimitiveCategory (line);
-			}
-			elif (cat != null)
+			if (cmd == "f")
+				type = EFilenameRegex;
+			elif (cmd == "t")
+				type = ETitleRegex;
+			else
 			{
-				QString cmd = line.left (colon);
-				ERegexType type = EFilenameRegex;
+				log (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
+				continue;
+			}
 
-				if (cmd == "f")
-					type = EFilenameRegex;
-				elif (cmd == "t")
-					type = ETitleRegex;
-				else
-				{
-					log (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
-					continue;
-				}
+			QRegExp regex (line.mid (colon + 1));
+			RegexEntry entry = { regex, type };
+			cat->regexes << entry;
+		}
+		else
+			log ("Warning: Rules given before the first category name");
+	}
 
-				QRegExp regex (line.mid (colon + 1));
-				RegexEntry entry = { regex, type };
-				cat->regexes << entry;
-			}
-			else
-				log ("Warning: Rules given before the first category name");
-		}
-
-		if (cat->isValidToInclude())
-			g_PrimitiveCategories << cat;
-	}
+	if (cat->isValidToInclude())
+		g_PrimitiveCategories << cat;
 
 	// Add a category for unmatched primitives.
 	// Note: if this function is called the second time, g_unmatched has been
 	// deleted at the beginning of the function and is dangling at this point.
 	g_unmatched = new PrimitiveCategory (tr ("Other"));
 	g_PrimitiveCategories << g_unmatched;
+	f.close();
 }
 
 // =============================================================================

mercurial