[Pinfo-devel] r192 - pinfo/branches/cxx/src
Nathanael Nerode
neroden-guest at costa.debian.org
Wed Sep 7 03:54:59 UTC 2005
Author: neroden-guest
Date: 2005-09-07 03:54:58 +0000 (Wed, 07 Sep 2005)
New Revision: 192
Modified:
pinfo/branches/cxx/src/filehandling_functions.cxx
pinfo/branches/cxx/src/filehandling_functions.h
pinfo/branches/cxx/src/pinfo.cxx
Log:
Major update; make everything taking **message use the vector<string>, 0-based
version.
Modified: pinfo/branches/cxx/src/filehandling_functions.cxx
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.cxx 2005-09-07 02:55:01 UTC (rev 191)
+++ pinfo/branches/cxx/src/filehandling_functions.cxx 2005-09-07 03:54:58 UTC (rev 192)
@@ -191,12 +191,10 @@
}
FILE *
-dirpage_lookup(char **type, char ***message, long *lines,
- const string wanted_name, string& first_node)
+dirpage_lookup(char **type, vector<string>& message,
+ string wanted_name, string& first_node)
{
#define Type (*type)
-#define Message (*message)
-#define Lines (*lines)
FILE *id = 0;
bool goodHit = false;
@@ -204,21 +202,20 @@
if (!id)
return 0;
- read_item(id, type, message, lines);
+ read_item(id, type, message);
/* search for node-links in every line */
- for (int i = 1; i < Lines; i++) {
+ for (int i = 0; i < message.size(); i++) {
/* we want: `* name:(file)node.' */
- string this_line = Message[i];
string::size_type nameend, filestart, fileend, dot;
- if ( (this_line.length() >= 2)
- && (this_line[0] == '*')
- && (this_line[1] == ' ')
- && ( (nameend = this_line.find(':')) != string::npos )
- && (this_line.length() != nameend + 1)
- && (this_line[nameend + 1] != ':')
- && ( (filestart = this_line.find('(', nameend + 1)) != string::npos )
- && ( (fileend = this_line.find(')', filestart + 1)) != string::npos )
- && ( (dot = this_line.find('.', fileend + 1)) != string::npos )
+ if ( (message[i].length() >= 2)
+ && (message[i][0] == '*')
+ && (message[i][1] == ' ')
+ && ( (nameend = message[i].find(':')) != string::npos )
+ && (message[i].length() != nameend + 1)
+ && (message[i][nameend + 1] != ':')
+ && ( (filestart = message[i].find('(', nameend + 1)) != string::npos )
+ && ( (fileend = message[i].find(')', filestart + 1)) != string::npos )
+ && ( (dot = message[i].find('.', fileend + 1)) != string::npos )
) {
; /* Matches the pattern we want */
} else {
@@ -226,9 +223,9 @@
}
/* It looks like a match. */
- string name(this_line, 2, nameend - 2);
- string file(this_line, filestart + 1, fileend - (filestart + 1) );
- string node(this_line, fileend + 1, dot - (fileend + 1) );
+ string name(message[i], 2, nameend - 2);
+ string file(message[i], filestart + 1, fileend - (filestart + 1) );
+ string node(message[i], fileend + 1, dot - (fileend + 1) );
if ( (name.length() >= wanted_name.length())
&& (strcasecmp(wanted_name.c_str(),
@@ -284,17 +281,13 @@
/* return file we found */
return id;
-#undef Lines
-#undef Message
#undef Type
}
void
-freeitem(char **type, char ***buf, long *lines)
+freeitem(char **type)
{
#define Type (*type)
-#define Buf (*buf)
-#define Lines (*lines)
long i;
if (Type != 0)
@@ -302,32 +295,19 @@
xfree(Type);
Type = 0;
}
- if (Buf != 0)
- {
- for (i = 1; i <= Lines; i++)
- if (Buf[i] != 0)
- {
- xfree(Buf[i]);
- Buf[i] = 0;
- }
- xfree(Buf);
- Buf = 0;
- }
#undef Type
-#undef Buf
-#undef Lines
}
void
-read_item(FILE * id, char **type, char ***buf, long *lines)
+read_item(FILE * id, char **type, vector<string>& buf)
{
#define Type (*type)
-#define Buf (*buf)
-#define Lines (*lines)
+
int i;
- freeitem(type, buf, lines); /* free previously allocated memory */
+ freeitem(type); /* free previously allocated memory */
+ buf.clear(); /* Wipe out old buffer */
/* seek precisely on the INFO_TAG (the seeknode function may be imprecise
* in combination with some weird tag_tables). */
@@ -340,68 +320,57 @@
fgets(Type, 1024, id);
Type = (char*)xrealloc(Type, strlen(Type) + 1);
- /* set number of lines to 0 */
- Lines = 0;
-
- /* initial buffer allocation */
- Buf = (char**)xmalloc(sizeof(char **));
-
- /* now iterate over the lines */
- do
- {
+ /* now iterate over the lines until we hit a new INFO_TAG mark */
+ char* tmpbuf = (char*) xmalloc(1024); /* Note, cleared like calloc */
+ do {
/* don't read after eof in info file */
if (feof(id))
break;
- /* realloc the previous line for it to fit exactly */
- if (Lines)
- {
- Buf[Lines] = (char*)xrealloc(Buf[Lines], strlen(Buf[Lines]) + 1);
- }
+ /* Clear our buffer (needed for algorithm below) */
+ memset(tmpbuf, '\0', 1024);
- /* increase the read lines number */
- Lines++;
-
- /* allocate space for the new line */
- Buf = (char**)xrealloc(Buf, sizeof(char **) *(Lines + 1));
- Buf[Lines] = (char*)xmalloc(1024);
- Buf[Lines][0] = 0;
-
- /* if the line was not found in input file, fill the allocated space
- * with empty line. */
- if (fgets(Buf[Lines], 1024, id) == NULL)
- strcpy(Buf[Lines], "\n");
- else /* we can be sure that at least 1 char was read! */
- {
+ /* Read a line. */
+ if (fgets(tmpbuf, 1024, id) == NULL) {
+ /* If there's a read error, or EOF at the start of the line,
+ * put in an empty line. */
+ /* FIXME */
+ strcpy(tmpbuf, "\n");
+ } else {
+ /* we can be sure that at least 1 char was read! */
/* *sigh* indices contains \0's
* which totally fucks up all strlen()s.
* so replace it by a space */
i = 1023;
/* find the end of the string */
- while (Buf[Lines][i]=='\0' && i>=0) i--;
+ while (tmpbuf[i]=='\0' && i>=0) i--;
/* and replace all \0's in the rest of the string by spaces */
+ /* Also clean out backspaces */
while (i>=0)
{
- if (Buf[Lines][i]=='\0' || Buf[Lines][i]=='\b')
- Buf[Lines][i]=' ';
+ if (tmpbuf[i]=='\0' || tmpbuf[i]=='\b')
+ tmpbuf[i]=' ';
i--;
}
}
- }
- while (Buf[Lines][0] != INFO_TAG); /* repeat until new node mark is found */
+ string tmpstr = tmpbuf;
+ buf.push_back(tmpstr);
+ } while (tmpbuf[0] != INFO_TAG); /* repeat until new node mark is found */
+ xfree(tmpbuf);
-
+ /* Note that we pushed the INFO_TAG line (or the read-zero-characters line) */
+ /* -- but check the feof case, FIXME */
+ /* Also, this should be dropped entirely and ismenu/isnote should be fixed */
+ /* FIXME */
/* added for simplifing two-line ismenu and isnote functs */
- if (Lines)
+ if (buf.size() > 0)
{
- strcpy(Buf[Lines], "\n");
- Buf[Lines] = (char*)xrealloc(Buf[Lines], strlen(Buf[Lines]) + 1);
+ buf[buf.size() - 1] = "\n";
}
+ /* Back up past that last INFO_TAG line */
fseek(id, -2, SEEK_CUR);
#undef Type
-#undef Buf
-#undef Lines
}
void
Modified: pinfo/branches/cxx/src/filehandling_functions.h
===================================================================
--- pinfo/branches/cxx/src/filehandling_functions.h 2005-09-07 02:55:01 UTC (rev 191)
+++ pinfo/branches/cxx/src/filehandling_functions.h 2005-09-07 03:54:58 UTC (rev 192)
@@ -44,14 +44,12 @@
* free allocated memory, hold by buf (node** content, stored line by line),
* and type (a char* pointer, which stores the node header).
*/
-void freeitem (char **type, char ***buf, long *lines);
+void freeitem (char **type);
/*
- * reads a node from 'id' to 'buf', and the header of node to 'type'. It sets
- * the numer of read lines to *lines. Warning! First line of 'buf' is left
- * empty.
+ * Reads a node from 'id' to 'buf', and the header of node to 'type'.
*/
-void read_item (FILE * id, char **type, char ***buf, long *lines);
+void read_item (FILE * id, char **type, std::vector<std::string>& buf);
/* searches for indirect entry of info file */
int seek_indirect (FILE * id);
/* as above, but with tag table entry */
@@ -84,7 +82,7 @@
* lines: pointer to long, which holds the number of lines in dir entry
*/
FILE *
-dirpage_lookup (char **type, char ***message, long *lines,
+dirpage_lookup (char **type, std::vector<std::string>& message,
const std::string filename, std::string& first_node);
/* removes trailing .gz, .bz2, etc. */
Modified: pinfo/branches/cxx/src/pinfo.cxx
===================================================================
--- pinfo/branches/cxx/src/pinfo.cxx 2005-09-07 02:55:01 UTC (rev 191)
+++ pinfo/branches/cxx/src/pinfo.cxx 2005-09-07 03:54:58 UTC (rev 192)
@@ -195,10 +195,8 @@
int userdefinedrc = 0;
FILE *id = NULL;
string filename_string;
- /* line count in message */
- long lines = 0;
/* this will hold node's text */
- char **message = 0;
+ vector<string> message;
/* this will hold the node's header */
char *type = 0;
int tag_table_pos = -1;
@@ -318,7 +316,7 @@
/* try to lookup the name in dir file */
if (id == NULL)
{
- id = dirpage_lookup(&type, &message, &lines, filename_string,
+ id = dirpage_lookup(&type, message, filename_string,
pinfo_start_node);
}
@@ -332,34 +330,16 @@
/* search for indirect entries, if any */
if (seek_indirect(id))
{
- read_item(id, &type, &message, &lines);
-
- /* Quick conversion to vector. Temporary, FIXME. */
- vector<string> my_message;
- for (typeof(my_message.size()) x = 0; x < lines; x++) {
- /* one-based to zero-based conversion, ick */
- string foo = message[x + 1];
- my_message.push_back(foo);
- }
-
- load_indirect(my_message);
+ read_item(id, &type, message);
+ load_indirect(message);
}
/* load tag table if such exists... */
if (seek_tag_table(id,1) != 2) {
if (ForceManualTagTable == 0)
{
- read_item(id, &type, &message, &lines);
-
- /* Quick conversion to vector. Temporary, FIXME. */
- vector<string> my_message;
- for (typeof(my_message.size()) x = 0; x < lines; x++) {
- /* one-based to zero-based conversion, ick */
- string foo = message[x + 1];
- my_message.push_back(foo);
- }
-
- load_tag_table(my_message);
+ read_item(id, &type, message);
+ load_tag_table(message);
}
else
{
@@ -411,7 +391,7 @@
/* set seek offset for given node */
seeknode(tag_table_pos, &id);
/* read the node */
- read_item(id, &type, &message, &lines);
+ read_item(id, &type, message);
/* handle goto/link where no file was found -- see bellow */
if (!filenotfound)
@@ -419,15 +399,7 @@
else
filenotfound = 0;
- /* Quick conversion to vector. Temporary, FIXME. */
- vector<string> my_message;
- for (typeof(my_message.size()) x = 0; x < lines; x++) {
- /* one-based to zero-based conversion, ick */
- string foo = message[x + 1];
- my_message.push_back(foo);
- }
-
- work_return_value = work(my_message, &type, id, tag_table_pos);
+ work_return_value = work(message, &type, id, tag_table_pos);
if (work_return_value.keep_going)
{
/* no cross-file link selected */
@@ -490,18 +462,10 @@
if (seek_indirect(id))
{
/* read it */
- read_item(id, &type, &message, &lines);
+ read_item(id, &type, message);
- /* Quick conversion to vector. Temporary, FIXME. */
- vector<string> my_message;
- for (typeof(my_message.size()) x = 0; x < lines; x++) {
- /* one-based to zero-based conversion, ick */
- string foo = message[x + 1];
- my_message.push_back(foo);
- }
-
/* initialize indirect entries */
- load_indirect(my_message);
+ load_indirect(message);
}
/* free old tag table */
tag_table.clear();
@@ -514,17 +478,8 @@
*/
if (ForceManualTagTable == 0)
{
- read_item(id, &type, &message, &lines);
-
- /* Quick conversion to vector. Temporary, FIXME. */
- vector<string> my_message;
- for (typeof(my_message.size()) x = 0; x < lines; x++) {
- /* one-based to zero-based conversion, ick */
- string foo = message[x + 1];
- my_message.push_back(foo);
- }
-
- load_tag_table(my_message);
+ read_item(id, &type, message);
+ load_tag_table(message);
}
else /* create tag table manually */
{
@@ -573,7 +528,7 @@
fclose(id);
closeprogram();
/* free's at the end are optional, but look nice :) */
- freeitem(&type, &message, &lines);
+ freeitem(&type);
tag_table.clear();
indirect.clear();
return 0;
More information about the Pinfo-devel
mailing list