How to best do the following string operations...

J

john_pataki

I want to reproduce a string search and replace between to arrays that
I have done in perl using very little code. I am not sure how best do
this without overdoing it... I have the outline of the code written
below - I just need to know what is my next best steps to complete it
-- or a suggestion if what I have so far is not correct. Thanks, john

My perl script basically inserts some postscript code for a watermark
into another postscript file.

Here is basic idea in psuedo perl ocode:

@infile = read_file("c:\\my_drawing.ps");
@watermark_code = read_file("c:\\watermark.ps");

# This is the core filter code
foreach $line (@infile) {
if ($line =~ /showpage/i) { # find line with showpage
$line =~ s/showpage//i; # remove showpage from line
push @outfile,$line; # add line from infile
push @outfile,"@watermark"; #insert all lines of watermark
} else { # all other lines in infile
push @outfile,$line; # add line from infile
} # end if and else
} # end foreach


The help I need is just with the above loop. Here is my current C code
I have written to get ready to do this loop.

I left out the read_ps_file function to make this email smaller...

int merge_watermark (void)
{
int watermark_lines;
int current_ps_lines;
int line;
char **watermark_code;
char **current_ps_code;
char watermark_file[256] = "c:\\temp\\watermark.ps";
char current_ps_file[256] = "c:\\temp\\18.ps";

watermark_lines = read_ps_file(watermark_file,watermark_code);
current_ps_lines = read_ps_file(current_ps_file,current_ps_code);

for (line=0;line<=current_ps_lines;line++) {
// merge code here...

} // end

}
 
R

Robert Harris

I want to reproduce a string search and replace between to arrays that
I have done in perl using very little code. I am not sure how best do
this without overdoing it... I have the outline of the code written
below - I just need to know what is my next best steps to complete it
-- or a suggestion if what I have so far is not correct. Thanks, john

My perl script basically inserts some postscript code for a watermark
into another postscript file.

Here is basic idea in psuedo perl ocode:

@infile = read_file("c:\\my_drawing.ps");
@watermark_code = read_file("c:\\watermark.ps");

# This is the core filter code
foreach $line (@infile) {
if ($line =~ /showpage/i) { # find line with showpage
$line =~ s/showpage//i; # remove showpage from line
push @outfile,$line; # add line from infile
push @outfile,"@watermark"; #insert all lines of watermark
} else { # all other lines in infile
push @outfile,$line; # add line from infile
} # end if and else
} # end foreach


The help I need is just with the above loop. Here is my current C code
I have written to get ready to do this loop.

I left out the read_ps_file function to make this email smaller...

int merge_watermark (void)
{
int watermark_lines;
int current_ps_lines;
int line;
char **watermark_code;
char **current_ps_code;
char watermark_file[256] = "c:\\temp\\watermark.ps";
char current_ps_file[256] = "c:\\temp\\18.ps";

watermark_lines = read_ps_file(watermark_file,watermark_code);
current_ps_lines = read_ps_file(current_ps_file,current_ps_code);

for (line=0;line<=current_ps_lines;line++) {
// merge code here...
char line_buffer[1024];

/* for each line */
if (fgets(line_buffer, sizeof(line_buffer), in_fp)) {
char *p = strstr(line_buffer, "showpage");

if (p) {
fwrite(line_buffer, 1, p - line_buffer, out_fp);
fputs(watermark_lines, out_fp);
fputs(p + sizeof("showpage") - 1, out_fp);
}
else
fputs(line_buffer, out_fp);
}
else {
EOF or error
}

This should work if:
1. There are no lines with containing 1023 characters or more
2. There are no output errors
3. There are no NULs embedded in the file

Robert
 
J

Johnny Google

Thanks.....

Is your solution just reading and writing directly to the file?

Just trying to figure out how you are doing this ....
I assume that you identify the location in the line where showpage
shows up by assigning its position in the line to the pointer p?
Is that the correct way to say what you are doing? Not sure about
terminology....

Are in_fp and out_fp file handles or char string arrays?

Thanks for any help in understanding what you are doing --- so I can do
this again next time :)

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top