473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1672

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.