[Pinfo-devel] r51 - pinfo/branches/cxx/src

Nathanael Nerode neroden-guest at costa.debian.org
Sun Aug 28 20:40:26 UTC 2005


Author: neroden-guest
Date: 2005-08-28 20:40:25 +0000 (Sun, 28 Aug 2005)
New Revision: 51

Modified:
   pinfo/branches/cxx/src/filehandling_functions.cxx
Log:
Fix off-by-one error in basename_and_dirname.
Convert more low-hanging fruit.


Modified: pinfo/branches/cxx/src/filehandling_functions.cxx
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.cxx	2005-08-28 20:29:22 UTC (rev 50)
+++ pinfo/branches/cxx/src/filehandling_functions.cxx	2005-08-28 20:40:25 UTC (rev 51)
@@ -42,7 +42,7 @@
 		dirname = "";
 	} else {
 		basename = filename.substr(index + 1);
-		dirname = filename.substr(0, index);
+		dirname = filename.substr(0, index + 1);
 	}
 }
 
@@ -787,34 +787,22 @@
 void
 addrawpath(char *filename)
 {
-	int len = strlen(filename);
-	int i, pos;
-	char tmp;
-	for (i = len; i >= 0; i--)
-	{
-		if (filename[i] == '/')
-		{
-			tmp = filename[i+1];
-			filename[i+1] = 0;
-			pos = i+1;
-			break;
-		}
-	}
-	if (i < 0)
-		pos = -1;
+	string filename_string;
+	filename_string = filename;
+	/* Cut the filename after the last slash. */
+	string::size_type index = filename_string.rfind('/');
+	string dirstring;
+	if (index != string::npos)
+		dirstring = filename_string.substr(0, index + 1);
+	else
+		dirstring = "./"; /* If no directory part, use current directory */
 
 	infopaths = (char**)xrealloc(infopaths,(infopathcount + 3) *(sizeof(char *)));
-	for (i = infopathcount; i > 0; i--)	/* move entries to the right */
+	for (int i = infopathcount; i > 0; i--)	/* move entries to the right */
 		infopaths[i] = infopaths[i - 1];
 
-	if (pos > 0)
-		infopaths[0]=strdup(filename);	/* add new(raw) entry */
-	else
-		infopaths[0]=strdup("./");
+	infopaths[0]=strdup(dirstring.c_str());	/* add new(raw) entry */
 	infopathcount++;
-
-	if (pos > 0)			/* recreate original filename */
-		filename[pos] = tmp;
 }
 
 int




More information about the Pinfo-devel mailing list