472,981 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

Trouble using string functions

Hi all,

I tried posting this through a free news server, but it
still has not appeared in Google, so if it turns up again
I apologize.

I hope someone can help me with this, or at least help me
find some information that will help me. If I were not at my
wit's end already, I wouldn't even ask. I'm used to doing all
of my programming in Windows, but now I have a task to
accomplish in UNIX/Linux using good old gcc.

Basically, what I have to do is parse a JavaScript file that
will ALWAYS have the following format:

************************************************
<!--
document.writeln('<TABLE BORDER="0" CELLPADDING="2">');
document.writeln(' <TR>');
....(many more lines all starting with "document.writeln('" )
document.writeln(' </TR>');
document.writeln('</TABLE>');
// -->
************************************************
It is auto-generated by another web site, and my job is to
get it to plain HTML format so that it can be linked to in an
email. What we want is for the end result to be:

************************************************

<TABLE BORDER="0" CELLPADDING="2">
<TR>
....(many more lines no longer with the "document.writeln('" )
</TR>
</TABLE>

************************************************

I have a good bit of it done, but I am getting stuck. Here is
the source I have so far in main:
{
int i, nc;
nc = 0;
for(int j = 1; j < 5; j++)
i = getchar();
while (i != EOF)
{
nc = nc + 1;
i = getchar();
if(i == '\n')
{
printf("%c", i);
for(int j = 1; j < 19; j++)
i = getchar();
}
else
{
printf("%c", i);
}
}
}

I am using redirction, i.e.: "prog<infile>outfile" to do this.

This code gets me close to what I need, but I still have the
remaining " '); " on the end of each string, which will not
do for obvious reasons. I tried reading into a string using
strcat(), which might do if I could just get it to work right,
for some reason my program will not run, no matter what I try
with strcat(). It will compile without any errors though.
I'm just not accustomed to using a language that does not
have an inherent "string" type. Everything is done with the
"char" type, and with the way some functions only want
pointers, etc, etc, well it's got me a bit confused and I've
spent WAY too much time on this already.

Can anyone help a fellow out?
Nov 14 '05 #1
6 1658

"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:1b**************************@posting.google.c om...
Hi all,

I tried posting this through a free news server, but it
still has not appeared in Google, so if it turns up again
I apologize.

I hope someone can help me with this, or at least help me
find some information that will help me. If I were not at my
wit's end already, I wouldn't even ask. I'm used to doing all
of my programming in Windows, but now I have a task to
accomplish in UNIX/Linux using good old gcc.

Basically, what I have to do is parse a JavaScript file that
will ALWAYS
Never say "always" :-)

have the following format:

************************************************
<!--
document.writeln('<TABLE BORDER="0" CELLPADDING="2">');
document.writeln(' <TR>');
...(many more lines all starting with "document.writeln('" )
document.writeln(' </TR>');
document.writeln('</TABLE>');
// -->
************************************************
It is auto-generated by another web site, and my job is to
get it to plain HTML format so that it can be linked to in an
email. What we want is for the end result to be:

************************************************

<TABLE BORDER="0" CELLPADDING="2">
<TR>
...(many more lines no longer with the "document.writeln('" )
</TR>
</TABLE>

************************************************
I am using redirction, i.e.: "prog<infile>outfile" to do this.

Can anyone help a fellow out?


Instead of using your approach of depending upon an exact
number and position of characters on each line, the below
extracts all characters between the first occurring pair
of delimiter characters (') on each line. I.e. lines
with less than two delimiters will be skipped, and characters
(if any) past the second delimiter will be skipped.
#include <stdio.h>
#include <string.h>

#define LINE_LEN 128 /* adjust to your needs */

int main()
{
char line[LINE_LEN] = {0};
char delim = '\'';
char *p1 = 0;
char *p2 = 0;

while(fgets(line, sizeof line, stdin))
if(p1 = strchr(line, delim))
if(p2 = strchr(++p1, delim))
{
*p2 = 0;
puts(p1);
}

return 0;
}
Input:

<!--
document.writeln('<TABLE BORDER="0" CELLPADDING="2">');
document.writeln(' <TR>');
....(many more lines all starting with "document.writeln('" )
document.writeln(' </TR>');
document.writeln('</TABLE>');
// -->
Output:

<TABLE BORDER="0" CELLPADDING="2">
<TR>
</TR>
</TABLE>
This code has not been thoroughly tested. I'll let you do that. :-)

HTH,
-Mike
Nov 14 '05 #2
mk******@mkwahler.net says...
Instead of using your approach of depending upon an exact
number and position of characters on each line, the below
extracts all characters between the first occurring pair
of delimiter characters (') on each line. I.e. lines
with less than two delimiters will be skipped, and characters
(if any) past the second delimiter will be skipped.

#include <stdio.h>
#include <string.h>

#define LINE_LEN 128 /* adjust to your needs */


This cannot be "adjusted" to the OP's needs. The OP did not say that the
automatically generated output had any line length limit, and given that it
*is* autogenerated, I rather doubt that it will obey any such trivially short
line length.

Using the better string library (http://bstring.sf.net/), this problem, and the
brittleness of the your solution (only searching for ') is trivially solved:

-------------------------------------------------------------------------------
#include <stdio.h>
#include "bstrlib.h"

int parseLines (bstring src) {
struct tagbstring token0 = bsStatic ("document.writeln('");
struct tagbstring token1 = bsStatic ("');");
struct tagbstring t, u;
int i, j;

/* Reference to where 1st token might match in src string */
blk2tbstr (t, src->data, token0.slen);
for (i=0; i < src->slen - token0.slen; i++) {

/* Does the 1st token match exactly? */
if (biseq (&t, &token0)) {

/* Reference to where 2nd token might match */
blk2tbstr (u, t.data, token1.slen);
for (j = i; j < src->slen - token1.slen; j++) {

/* Does the 2nd token match exactly? */
if (biseq (&u, &token1)) {

/* Construct middle string */
bstring b = blk2bstr (t.data + token0.slen,
j - i - token0.slen);

/* Output the '\0' terminated buffer */
puts (b->data);
bdestroy (b);
break;
}

/* Shift 2nd token scan downward */
u.data++;
}
}

/* Shift 1st token scan downward */
t.data++;
}
return 0;
}

int main (int argc, char * argv[]) {
FILE * fp;

if (argc < 2) {
printf ("%s [inputfile]\n", argv[0]);
return -__LINE__;
}

if (NULL != (fp = fopen (argv[1], "rb"))) {
/* Just read the whole file into a bstring */
bstring src = bread ((bNread) fread, fp);
int ret = parseLines (src);
fclose (fp);
bdestroy (src);
return ret;
}
return -__LINE__;
}
-------------------------------------------------------------------------------

Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<uq*******************@newsread1.news.pas.ear thlink.net>...
"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:1b**************************@posting.google.c om...
Hi all,

I tried posting this through a free news server, but it
still has not appeared in Google, so if it turns up again
I apologize.

I hope someone can help me with this, or at least help me
find some information that will help me. If I were not at my
wit's end already, I wouldn't even ask. I'm used to doing all
of my programming in Windows, but now I have a task to
accomplish in UNIX/Linux using good old gcc.

Basically, what I have to do is parse a JavaScript file that
will ALWAYS
Never say "always" :-)


I only say that because another computer generates the script. Why
they would ever change it I can't imagine!
have the following format:

************************************************
<!--
document.writeln('<TABLE BORDER="0" CELLPADDING="2">');
document.writeln(' <TR>');
...(many more lines all starting with "document.writeln('" )
document.writeln(' </TR>');
document.writeln('</TABLE>');
// -->
************************************************
It is auto-generated by another web site, and my job is to
get it to plain HTML format so that it can be linked to in an
email. What we want is for the end result to be:

************************************************

<TABLE BORDER="0" CELLPADDING="2">
<TR>
...(many more lines no longer with the "document.writeln('" )
</TR>
</TABLE>

************************************************


I am using redirction, i.e.: "prog<infile>outfile" to do this.

Can anyone help a fellow out?


Instead of using your approach of depending upon an exact
number and position of characters on each line, the below
extracts all characters between the first occurring pair
of delimiter characters (') on each line. I.e. lines
with less than two delimiters will be skipped, and characters
(if any) past the second delimiter will be skipped.
#include <stdio.h>
#include <string.h>

#define LINE_LEN 128 /* adjust to your needs */

int main()
{
char line[LINE_LEN] = {0};
char delim = '\'';
char *p1 = 0;
char *p2 = 0;

while(fgets(line, sizeof line, stdin))
if(p1 = strchr(line, delim))
if(p2 = strchr(++p1, delim))
{
*p2 = 0;
puts(p1);
}

return 0;
}
Input:

<!--
document.writeln('<TABLE BORDER="0" CELLPADDING="2">');
document.writeln(' <TR>');
...(many more lines all starting with "document.writeln('" )
document.writeln(' </TR>');
document.writeln('</TABLE>');
// -->
Output:

<TABLE BORDER="0" CELLPADDING="2">
<TR>
</TR>
</TABLE>
This code has not been thoroughly tested. I'll let you do that. :-)

HTH,
-Mike


The code has been thouroghly tested with several of these scripts
and it works perfectly every time so far! Thanks a bunch Mike. I'll
bet this took you all of 5 minutes at the most to cook up, whereas
I had spent quite a few hours trying all manner of different things.
Now I wish I had actually USED that C++ compiler that I got in the
mid 1990's.
Nov 14 '05 #4
Oops, I spoke too soon! I just tried running this on the latest
version, and wouldn't you know that one of the lines had a ' in
it. Being javascript, it is escaped with the backslash like:

another\'s

Given this, it should be a fairly easy matter to check for the
escape character and ignore the next character. Fairly simple
for someone else, that is, but I am certainly going to try now
that I've got something that actually (almost) works like I
need it to.
The code has been thouroghly tested with several of these scripts
and it works perfectly every time so far! Thanks a bunch Mike. I'll
bet this took you all of 5 minutes at the most to cook up, whereas
I had spent quite a few hours trying all manner of different things.
Now I wish I had actually USED that C++ compiler that I got in the
mid 1990's.

Nov 14 '05 #5
"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:1b**************************@posting.google.c om...
Oops, I spoke too soon! I just tried running this on the latest
version, and wouldn't you know that one of the lines had a ' in
it. Being javascript, it is escaped with the backslash like:

another\'s

Given this, it should be a fairly easy matter to check for the
escape character and ignore the next character. Fairly simple
for someone else, that is, but I am certainly going to try now
that I've got something that actually (almost) works like I
need it to.


Hint for a 'quick-n-dirty' fix:

The function 'strchr()' has a counterpart which starts
searching from the end of a string instead of from the
beginning: 'strrchr()'.

-Mike
Nov 14 '05 #6
gm*******@carolina.rr.com (Gary Morris) wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<uq*******************@newsread1.news.pas.ear thlink.net>...
"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:1b**************************@posting.google.c om...
Basically, what I have to do is parse a JavaScript file that
will ALWAYS


Never say "always" :-)


I only say that because another computer generates the script. Why
they would ever change it I can't imagine!
have the following format:


Hohum. Beware the snark. I've said the same thing before. There was this
other computer, which was supposed to generate data, and that was sent
to me to process. So I wrote a program to process it. Should be a simple
job - after all, it was all computer-generated data, and what reason
could they possibly have for changing the format?

No prizes for guessing what happened two months later.

Richard
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: Pjotr Wedersteers | last post by:
I am using a function that does a very basic encryption (rotation based) of data. Data is a string which may contain a..z,A..Z,0..9,,(),|,{}"', length limited to 255 chars. The problem I have...
9
by: Jakle | last post by:
I'm trying to write a program (with my very limited knowledge of python) that will convert text I type into those letters drawn with ascii symbols. I did 2 letters then went to test it. Here's the...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
8
by: Exits Funnel | last post by:
Hello, I've been tasked with porting some C++ code from Windows to Linux. The following excerpt is giving me trouble: //BEGIN CODE #include <string> class TempTestBase_t {
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
5
by: Henaro | last post by:
Hello~ I am having trouble setting environment variables in C++ on win32. The code that is not working is: char prxy; char pf_cmd1 = "set http_proxy="; ....
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.