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

Nathanael Nerode neroden-guest at costa.debian.org
Sun Aug 28 21:28:31 UTC 2005


Author: neroden-guest
Date: 2005-08-28 21:28:30 +0000 (Sun, 28 Aug 2005)
New Revision: 54

Modified:
   pinfo/branches/cxx/src/filehandling_functions.cxx
   pinfo/branches/cxx/src/filehandling_functions.h
   pinfo/branches/cxx/src/pinfo.cxx
Log:
Convert more of pinfo.cxx to std::string, with lots of fallout.


Modified: pinfo/branches/cxx/src/filehandling_functions.cxx
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.cxx	2005-08-28 20:57:22 UTC (rev 53)
+++ pinfo/branches/cxx/src/filehandling_functions.cxx	2005-08-28 21:28:30 UTC (rev 54)
@@ -112,9 +112,11 @@
 	{
 		if (strncmp(dp->d_name, bname, namelen) == 0)
 		{
-			char *tmp = strdup(dp->d_name);
+			string tmp_string = dp->d_name;
+			strip_compression_suffix(tmp_string);
+
+			char *tmp = strdup(tmp_string.c_str());
 			int dl;
-			strip_compression_suffix(tmp);
 			dl = strlen(tmp);
 			if ((!isdigit(tmp[dl - 1])) &&(!isalpha(tmp[namelen])))
 				/* if it's not eg. info-2.gz, but info.gz, the primary page
@@ -676,7 +678,7 @@
  * *filenameprefix and then in the rest of userdefined paths.
  */
 FILE *
-openinfo(char *filename, int number)
+openinfo(const char *filename, int number)
 {
 	FILE *id = NULL;
 	char *buf = (char*) xmalloc(1024);	/* holds local copy of filename */
@@ -735,7 +737,9 @@
 		else
 		{
 			strcpy(buf, infopaths[i]);	/* build a filename */
-			if (matchfile(&buf, filename) == 1)	/* no match found in this directory */
+			char* filename_fixme = NULL;
+			filename_fixme = strdup(filename); /* big memory leak */
+			if (matchfile(&buf, filename_fixme) == 1)	/* no match found in this directory */
 				continue;
 		}
 		bufend = buf;
@@ -1153,20 +1157,23 @@
 #undef id
 }
 
-	void
-strip_compression_suffix(char *file)	/* removes trailing .gz, .bz2, etc. */
+/*
+ * Strip one trailing .gz, .bz2, etc.
+ * Operates in place.
+ */
+void
+strip_compression_suffix(string& filename)
 {
-	char *found = 0;
-	int j;
-	for (j = 0; j < SuffixesNumber; j++)
+	for (int j = 0; j < SuffixesNumber; j++)
 	{
-		if (found = strstr(file, suffixes[j].suffix))
-		{
-			if (*(found + strlen(suffixes[j].suffix)) == 0)
-			{
-				*found = 0;
-				break;
-			}
+		string::size_type suffix_len =  strlen(suffixes[j].suffix);
+		if (    (filename.length() >= suffix_len)
+		     && (filename.substr(filename.length() - suffix_len)
+		         == suffixes[j].suffix)
+		   ) {
+			/* Truncate string. */
+			filename.resize(filename.length() - suffix_len);
+			break;
 		}
 	}
 }

Modified: pinfo/branches/cxx/src/filehandling_functions.h
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.h	2005-08-28 20:57:22 UTC (rev 53)
+++ pinfo/branches/cxx/src/filehandling_functions.h	2005-08-28 21:28:30 UTC (rev 54)
@@ -63,7 +63,7 @@
 /* loads tag table (as above) */
 void load_tag_table (char **message, long lines);
 /* opens info file */
-FILE *openinfo (char *filename, int number);
+FILE *openinfo (const char *filename, int number);
 /* opens dir info file */
 FILE *opendirfile (int number);
 
@@ -87,6 +87,9 @@
 		char *filename, char **first_node);
 
 /* removes trailing .gz, .bz2, etc. */
-void strip_compression_suffix (char *file);
+void strip_compression_suffix (std::string& filename);
 
+/* Gets the base file name from a filename-with-directory */
+void basename (std::string filename, std::string& basename_str);
+
 #endif

Modified: pinfo/branches/cxx/src/pinfo.cxx
===================================================================
--- pinfo/branches/cxx/src/pinfo.cxx	2005-08-28 20:57:22 UTC (rev 53)
+++ pinfo/branches/cxx/src/pinfo.cxx	2005-08-28 21:28:30 UTC (rev 54)
@@ -27,6 +27,8 @@
 #include <string>
 using std::string;
 
+// #include "filehandling_functions.h"
+
 RCSID(PKG_VER "$Id$")
 
 #ifdef HAVE_GETOPT_LONG
@@ -139,37 +141,29 @@
 					checksu();
 					if (verbose)
 						printf(_("Looking for man page...\n"));
-					filename_string = "";
+					string man_filename_string = "";
 					for (int i = optind; i < argc; i++)
 					{
-						filename_string.append(argv[i]);
-						filename_string.append(" ");
+						man_filename_string.append(argv[i]);
+						man_filename_string.append(" ");
 					}
-					exit(handlemanual(filename_string));
+					exit(handlemanual(man_filename_string));
 				}
 			case 'f':
 			case 'r':
 				{
-					char filename[256];
-					char *tmp;
-					strncpy(filename, argv[argc - 1], 200);
+					filename_string = argv[argc - 1];
 					/* Check for unsafe filenames */
-					string filename_string = filename;
 					checkfilename(filename_string);
 					/* add the raw path to searchpath */
 					addrawpath(filename_string);
 
-					tmp = filename + strlen(filename) - 1;
-					/* later, openinfo automaticaly adds them */
-					strip_compression_suffix(filename);
-					/* get basename */
-					while ((tmp > filename) &&(*tmp != '/'))
-						tmp--;
-					if (*tmp == '/')
-						tmp++;
-					/* and try it without '.info' suffix */
-					(*id) = openinfo(tmp, 0);
-					filename_string = filename;
+					/* Strip suffix in place.  Later, openinfo tries adding all of them */
+					strip_compression_suffix(filename_string);
+					/* Get basename, and pass to openinfo */
+					string basename_string;
+					basename(filename_string, basename_string);
+					(*id) = openinfo(basename_string.c_str(), 0);
 				}
 				break;
 			case 'a':
@@ -179,8 +173,8 @@
 				{
 					use_apropos = 1;
 					plain_apropos = 1;
-					string filename_string = argv[argc - 1];
-					exit(handlemanual(filename_string));
+					string man_filename_string = argv[argc - 1];
+					exit(handlemanual(man_filename_string));
 					/* Again, really really weird.  FIXME. */
 				}
 				break;




More information about the Pinfo-devel mailing list