Angus Leeming wrote:

> Anand Rangarajan <[EMAIL PROTECTED]> writes:
>> How hard will it be to fix the source to read the bounding box entries
>> and account for the fact that there could be spaces. Anyone know the
>> actual source code that needs to be fixed? Is it in
>> src/graphics/GraphicsParams.C ?

Sorry Anand to beat you to it, but this has bothered me since some time too,
so I did not want to think yet another time about and fixed it.

> Without having the sources to hand, I'd look in
> src/frontends/controllers/ControlGraphics.C. If memory serves me rightly,
> the function you're looking for has some obvious name ;-)

readBB_from_PSFile() in src/support/filetools.C is a better place because
the special bb format is expected elsewhere.

> How hard? Not hard at all. Use Boost.Regex
> (http://www.boost.org/libs/regex/doc/index.html; lots of examples in the
> code base) and a regular expression of the form:
> 
> "^%%BoundingBox: *([[:digit:]]+) +([[:digit:]]+) +([[:digit:]]+)
> +([[:digit:]]+)"
> 
> should do the trick.

Angus, you finally made me to fix this the dirty way :-) I always wanted to
do it right by changing the return type of readBB_from_PSFile(), but now I
simply created a sane string.
The attached works for me. I'll put it in trunk tomorrow if nobody objects.


Log:

fix bug 1235
        * src/support/filetools.C
        (readBB_lyxerrMessage): remove (was strange optimization)
        (readBB_from_PSFile): sanitize return value


Georg
Index: src/support/filetools.C
===================================================================
--- src/support/filetools.C	(Revision 13548)
+++ src/support/filetools.C	(Arbeitskopie)
@@ -1141,19 +1143,6 @@ void removeAutosaveFile(string const & f
 }
 
 
-void readBB_lyxerrMessage(string const & file, bool & zipped,
-	string const & message)
-{
-	lyxerr[Debug::GRAPHICS] << "[readBB_from_PSFile] "
-		<< message << std::endl;
-#ifdef WITH_WARNINGS
-#warning Why is this func deleting a file? (Lgb)
-#endif
-	if (zipped)
-		unlink(file);
-}
-
-
 string const readBB_from_PSFile(string const & file)
 {
 	// in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
@@ -1168,21 +1157,40 @@ string const readBB_from_PSFile(string c
 	string const format = getFormatFromContents(file_);
 
 	if (format != "eps" && format != "ps") {
-		readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
+		lyxerr[Debug::GRAPHICS]
+			<< "[readBB_from_PSFile] no(e)ps-format" << endl;
+		if (zipped)
+			unlink(file_);
 		return string();
 	}
 
+	static boost::regex bbox_re(
+		"^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)(.*)");
 	std::ifstream is(file_.c_str());
 	while (is) {
 		string s;
 		getline(is,s);
-		if (contains(s,"%%BoundingBox:") && !contains(s,"atend")) {
-			string const bb = ltrim(s.substr(14));
-			readBB_lyxerrMessage(file_, zipped, bb);
+		boost::smatch what;
+		regex_match(s, what, bbox_re);
+		if (what[0].matched) {
+			// Our callers expect the tokens in the string
+			// separated by single spaces.
+			// FIXME: change return type from string to something
+			// sensible
+			ostringstream os;
+			os << what.str(1) << ' ' << what.str(2) << ' '
+			   << what.str(3) << ' ' << what.str(4);
+			string const bb = os.str();
+			lyxerr[Debug::GRAPHICS]
+				<< "[readBB_from_PSFile] " << bb << endl;
+			if (zipped)
+				unlink(file_);
 			return bb;
 		}
 	}
-	readBB_lyxerrMessage(file_, zipped, "no bb found");
+	lyxerr[Debug::GRAPHICS] << "[readBB_from_PSFile] no bb found" << endl;
+	if (zipped)
+		unlink(file_);
 	return string();
 }
 

Reply via email to