From 66093f029b4dd94e109c8cc7d9d4bfbc6f0f4e90 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 5 Nov 2009 11:53:01 +0100 Subject: copyLinesFromFileToFile instead of a single Line in the rred method by saving the length of the data we need to copy into the out file --- methods/rred.cc | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'methods') diff --git a/methods/rred.cc b/methods/rred.cc index 2d4dd768b..9abb1b89c 100644 --- a/methods/rred.cc +++ b/methods/rred.cc @@ -34,7 +34,7 @@ class RredMethod : public pkgAcqMethod { State applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file, unsigned long &line, char *buffer, Hashes *hash) const; void ignoreLineInFile(FILE *fin, char *buffer) const; - void copyLineFromFileToFile(FILE *fin, FILE *fout, + void copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines, Hashes *hash, char *buffer) const; State patchFile(FILE *ed_cmds, FILE *in_file, FILE *out_file, Hashes *hash) const; @@ -123,10 +123,14 @@ RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_ unsigned const long pos = ftell(ed_cmds); // if this is add or change then go to the next full stop + unsigned int data_length = 0; if (mode == MODE_CHANGED || mode == MODE_ADDED) { - do + do { ignoreLineInFile(ed_cmds, buffer); + data_length++; + } while (strncmp(buffer, ".", 1) != 0); + data_length--; // the dot should not be copied } // do the recursive call - the last command is the one we need to execute at first @@ -140,10 +144,9 @@ RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_ line++; // first wind to the current position and copy over all unchanged lines - while (line < startline) { - fgets(buffer, BUF_SIZE, in_file); - copyLineFromFileToFile(in_file, out_file, hash, buffer); - line++; + if (line < startline) { + copyLinesFromFileToFile(in_file, out_file, (startline - line), hash, buffer); + line = startline; } if (mode != MODE_ADDED) @@ -152,12 +155,7 @@ RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_ // include data from ed script if (mode == MODE_CHANGED || mode == MODE_ADDED) { fseek(ed_cmds, pos, SEEK_SET); - while(fgets(buffer, BUF_SIZE, ed_cmds) != NULL) { - if (strncmp(buffer, ".", 1) != 0) - copyLineFromFileToFile(ed_cmds, out_file, hash, buffer); - else - break; - } + copyLinesFromFileToFile(ed_cmds, out_file, data_length, hash, buffer); } // ignore the corresponding number of lines from input @@ -170,15 +168,15 @@ RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_ return ED_OK; } /*}}}*/ -void RredMethod::copyLineFromFileToFile(FILE *fin, FILE *fout, /*{{{*/ +void RredMethod::copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,/*{{{*/ Hashes *hash, char *buffer) const { - size_t written = fwrite(buffer, 1, strlen(buffer), fout); - hash->Add((unsigned char*)buffer, written); - while (strlen(buffer) == (BUF_SIZE - 1) && - buffer[BUF_SIZE - 2] != '\n') { - fgets(buffer, BUF_SIZE, fin); - written = fwrite(buffer, 1, strlen(buffer), fout); - hash->Add((unsigned char*)buffer, written); + while (0 < lines--) { + do { + fgets(buffer, BUF_SIZE, fin); + size_t const written = fwrite(buffer, 1, strlen(buffer), fout); + hash->Add((unsigned char*)buffer, written); + } while (strlen(buffer) == (BUF_SIZE - 1) && + buffer[BUF_SIZE - 2] != '\n'); } } /*}}}*/ -- cgit v1.2.3