writing get_script()

F

Franken Sense

How do I write a get_script() for data that look like these:

44:004:037 Having land, sold it, and brought the money, and laid it at
the apostles' feet.

44:005:001 But a certain man named Ananias, with Sapphira his wife, sold
a possession,

44:005:002 And kept back part of the price, his wife also being privy to
it, and brought a certain part, and laid it at the apostles'
feet.

I don't just want a line, so I can't read until '\n'. If I took it in a
char at a time, how do I write the control so that it stops when I get to a
string of ten digits or colons?

Thanks for your comment.
 
G

Guest

How do I write a get_script() for data that look like these:

44:004:037 Having land, sold it, and brought the money, and laid it at
           the apostles' feet.

44:005:001 But a certain man named Ananias, with Sapphira his wife, sold
           a possession,

44:005:002 And kept back part of the price, his wife also being privy to
           it, and brought a certain part, and laid it at the apostles'
           feet.

I don't just want a line, so I can't read until '\n'.  If I took it in a
char at a time, how do I write the control so that it stops when I get to a
string of ten digits or colons?

1. write it in perl

2. I'd still consider reading a line at a time. If begins with a
scripture
marker (dd:ddd:ddd) return what you have so far otherwise append the
line
to what you have so far and repeat.

3. if you want to do it a char at a time you need a baby parser which
is just a state machine
 
S

s0suk3

How do I write a get_script() for data that look like these:

44:004:037 Having land, sold it, and brought the money, and laid it at
           the apostles' feet.

44:005:001 But a certain man named Ananias, with Sapphira his wife, sold
           a possession,

44:005:002 And kept back part of the price, his wife also being privy to
           it, and brought a certain part, and laid it at the apostles'
           feet.

I don't just want a line, so I can't read until '\n'.  If I took it in a
char at a time, how do I write the control so that it stops when I get to a
string of ten digits or colons?

If you stop at the first string of ten digits or colons, you would
only read "44:004:037" for the above example input. Perhaps you'd
rather want stop when encountering a specific such sequence (e.g., all
zeros)?

Anyway, according to your specification, it could look something like
this (returns true if the script is read completely, false if EOF is
reached or an input error occurs):

bool get_script(FILE* in, char* result, int size)
{
int ch;
int i = 0;
while (i < size - 1 && (ch = getc(in)) != EOF) {
result[i++] = ch;
int j = 0;
for (; i + j < size - 1 &&
(isdigit(ch) || ch == ':'); ++j) {
if (j + 1 == 10) {
result[i + j] = '\0';
return true;
}
if ((ch = getc(in)) == EOF) {
result[i + j] = '\0';
return false;
}
result[i + j] = ch;
}
i += j;
}
result = '\0';
return false;
}

Sebastian
 
F

Franken Sense

In Dread Ink, the Grave Hand of Han from China Did Inscribe:
I agree with Keighley here. It's not that you can't accomplish
what you want to do in C; it's that Perl is a lot better for
the task at hand. Since Franken Sense (whom I believe to be
George, Larry Gates, etc.) has been studying Perl, the
advice is not as useless as it may first appear.

Yours,
Han from China

The aim here is to populate binary trees in interesting ways. This is
exactly the type of thing that perl does well, but I'd be really sketchy on
how to suck something in using perl with main in C.
--
Frank

And by the way, a few months ago, I trademarked the word 'funny.' So when
Fox calls me 'unfunny,' they're violating my trademark. I am seriously
considering a countersuit.
~~ Al Franken, in response to Fox's copyright infringement lawsuit
 
J

jameskuyper

Franken Sense wrote:
....
Thanks, sebastian, I can't quite remember what the status is of bool in C.
Am I missing necessary headers?

In C99, _Bool is always available. If you #include <stdbool.h>, it
will set up macros named bool, true, false, and
__bool_true_false_are_defined.

If you need your code to compile with C90, you'll have to set up your
own boolean type. The purpose of the way bool is implemented in C99 is
to allow you to continue using C90 code that uses bool with a
different meaning.
 
C

CBFalconer

Franken said:
How do I write a get_script() for data that look like these:

44:004:037 Having land, sold it, and brought the money, and laid it at
the apostles' feet.

44:005:001 But a certain man named Ananias, with Sapphira his wife, sold
a possession,

44:005:002 And kept back part of the price, his wife also being privy to
it, and brought a certain part, and laid it at the apostles'
feet.

I don't just want a line, so I can't read until '\n'. If I took it in a
char at a time, how do I write the control so that it stops when I get to a
string of ten digits or colons?

It looks as if you need to separate out 'modules' by the presence
of a blank line. After that, modules begin with a system of three
integers, separated by ':' and terminated by a blank. After that,
the content is words (alpha/numeric? chars) terminated by
punctuation or a blank. You can then represent modules by a
structure that contains the three integers, and a pointer to the
first word or punctuation, contained in a list. You can do all
this on the fly.
 
K

Keith Thompson

jameskuyper said:
Franken Sense wrote:
...

In C99, _Bool is always available. If you #include <stdbool.h>, it
will set up macros named bool, true, false, and
__bool_true_false_are_defined.

If you need your code to compile with C90, you'll have to set up your
own boolean type. The purpose of the way bool is implemented in C99 is
to allow you to continue using C90 code that uses bool with a
different meaning.

For guidance on that, see section 9 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

My favorite way to do this is:

typedef enum { false, true } bool;

For compatibility, you might use:

#if __STDC_VERSION__ 199901L
#include <stdbool.h>
#else
typedef enum { false, true } bool;
#endif

The two ways of defining false, true, and bool are not 100%
compatible, but it's not too difficult to write code that can use
either without running into the incompatibilities.
 
E

Eric Sosman

Keith said:
[... concerning pre-C99 "bool" ...]

My favorite way to do this is:

typedef enum { false, true } bool;

... which brings to mind a horror story. Reading
someone else's code, I once encountered

typedef enum { TRUE, FALSE } Bool;

Is it any wonder he'd asked for some debugging help?
 
K

Keith Thompson

Eric Sosman said:
Keith said:
[... concerning pre-C99 "bool" ...]

My favorite way to do this is:

typedef enum { false, true } bool;

... which brings to mind a horror story. Reading
someone else's code, I once encountered

typedef enum { TRUE, FALSE } Bool;

Is it any wonder he'd asked for some debugging help?

*chuckle*

I think I'll change my recommendation to:

typedef enum { false=0, true=1 } bool;

Not because the "=0" and "=1" are necessary (they're completely
superfluous as far as the compiler is concerned), but because they
document the fact that the values matter. Even if someone later
decides, for some unfathomable reason, to reverse the order, that
won't break it:

typedef enum { true=1, false=0 } bool;

(One might argue that something that's just for documentation should
be written as a comment. But in this case, there's something to be
said for documentation that can't get out of synch with the code.)
 
F

Franken Sense

In Dread Ink, the Grave Hand of CBFalconer Did Inscribe:
It looks as if you need to separate out 'modules' by the presence
of a blank line. After that, modules begin with a system of three
integers, separated by ':' and terminated by a blank. After that,
the content is words (alpha/numeric? chars) terminated by
punctuation or a blank. You can then represent modules by a
structure that contains the three integers, and a pointer to the
first word or punctuation, contained in a list. You can do all
this on the fly.

You mean the flies that I may have slapped on my arms today as I waited for
the city bus to transport me to the ballgame?

Almost proper Perl is so:

#!/usr/bin/perl
# perl m6.pl
use warnings;
use strict;

use Tree::Binary;

local $/="";
while ( <DATA> ) {
my @s = split /\s+/, $_;

# print fields
print $s[0];

my $outline = join(' ', (1..$#s));
print "$outline\n";
}

__DATA__
44:005:017 Then the high priest rose up, and all they that were with him,
(which is the sect of the Sadducees,) and were filled with
indignation,

44:005:018 And laid their hands on the apostles, and put them in the
common prison.

44:005:019 But the angel of the Lord by night opened the prison doors,
and brought them forth, and said,

44:005:020 Go, stand and speak in the temple to the people all the words
of this life.
 
F

Franken Sense

In Dread Ink, the Grave Hand of jameskuyper Did Inscribe:
Franken Sense wrote:
...

In C99, _Bool is always available. If you #include <stdbool.h>, it
will set up macros named bool, true, false, and
__bool_true_false_are_defined.

If you need your code to compile with C90, you'll have to set up your
own boolean type. The purpose of the way bool is implemented in C99 is
to allow you to continue using C90 code that uses bool with a
different meaning.

#include <stdio.h>
#include <stdbool.h>

bool get_script(FILE* in, char* result, int size)
{
int ch;
int i = 0;
while (i < size - 1 && (ch = getc(in)) != EOF) {
result[i++] = ch;
int j = 0;
for (; i + j < size - 1 &&
(isdigit(ch) || ch == ':'); ++j) {
if (j + 1 == 10) {
result[i + j] = '\0';
return true;
}
if ((ch = getc(in)) == EOF) {
result[i + j] = '\0';
return false;
}
result[i + j] = ch;
}
i += j;
}
result = '\0';
return false;
}

int main(void)
{

return 0;
}

// gcc s2.c -Wall -o out


E:\gfortran\dan>gcc s2.c -Wall -o out
s2.c: In function `get_script':
s2.c:12: warning: implicit declaration of function `isdigit'
s2.c:35:25: warning: no newline at end of file

E:\gfortran\dan>out

E:\gfortran\dan>

These are things I can address.
--
Frank

Most of us here in the media are what I call infotainers...Rush Limbaugh is
what I call a disinfotainer. He entertains by spreading disinformation.
~~ Al Franken
 
P

Phil Carmody

Keith Thompson said:
Eric Sosman said:
Keith said:
[... concerning pre-C99 "bool" ...]

My favorite way to do this is:

typedef enum { false, true } bool;

... which brings to mind a horror story. Reading
someone else's code, I once encountered

typedef enum { TRUE, FALSE } Bool;

Is it any wonder he'd asked for some debugging help?

*chuckle*

I think I'll change my recommendation to:

typedef enum { false=0, true=1 } bool;

Not because the "=0" and "=1" are necessary (they're completely
superfluous as far as the compiler is concerned), but because they
document the fact that the values matter.


I've seen:

typedef enum { FALSE=(0==1), TRUE=(0==0) } bool_e;

for similar reasons. Not just the values, but what those values
represent being documented.
Even if someone later
decides, for some unfathomable reason, to reverse the order, that
won't break it:

typedef enum { true=1, false=0 } bool;

(One might argue that something that's just for documentation should
be written as a comment. But in this case, there's something to be
said for documentation that can't get out of synch with the code.)

There certainly is.

Phil
 
K

Keith Thompson

Phil Carmody said:
I've seen:

typedef enum { FALSE=(0==1), TRUE=(0==0) } bool_e;

for similar reasons. Not just the values, but what those values
represent being documented.
[...]

Sure, you can do that, but IMHO writing (0==1) rather than 0 and
(0==0) rather than 1 is just silly. 0 is false, 1 is true, and anyone
who doesn't already know that isn't going to benefit from the extra
verbosity anyway.
 
W

Willem

Malcolm McLean wrote:
)
)> Sure, you can do that, but IMHO writing (0==1) rather than 0 and
)> (0==0) rather than 1 is just silly. 0 is false, 1 is true, and anyone
)> who doesn't already know that isn't going to benefit from the extra
)> verbosity anyway.
)>
) 0 == 1 is necessarily false. 0 is false by convention.

Yes, and by writing 'FALSE = 0' and 'TRUE = 1' you document this convention.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
L

Lew Pitcher

0 == 1 is necessarily false. 0 is false by convention.

Or,
0 is false, by design
true is 'not false'
and 'not' is ~
so, true is ~0

Or any of an uncountable number of ways to say that the C language evaluates
non-zero values as "true" conditions, and zero values as "false"
conditions.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
J

James Kuyper

Malcolm said:
0 == 1 is necessarily false. 0 is false by convention.

That 0 is treated as false is a convention established by the standard,
in it's definition of the behavior of the if() statement and the ?:
operator, and in it's description of the value of the relational
operators, and it it's description of the "true" and "false" macros
defined by <stdbool.h>. That strikes me as a pretty well-established
convention.
 
K

Keith Thompson

Malcolm McLean said:
0 == 1 is necessarily false. 0 is false by convention.

0 is necessarily false in C. It's no more or less of a "convention"
than the fact that 0 == 1 yields a false result (specifically 0).

Are you really arguing that
typedef enum { false = (0==1), true = (0==0) } bool;
is better than
typedef enum { false = 0, true = 1 } bool;
? If nothing else, the second form seems obviously correct to me; the
first takes me a few seconds to verify.
 
R

Richard Bos

Malcolm McLean said:
0 == 1 is necessarily false. 0 is false by convention.

That's like saying that driving on one side of the road is necessary,
but driving on the right is convention. Well, yes, it _is_ - but someone
who doesn't know that convention should not be messing about with motor
vehicles, casu quo C programs.

Richard
 
P

Phil Carmody

Willem said:
Malcolm McLean wrote:
)
)> Sure, you can do that, but IMHO writing (0==1) rather than 0 and
)> (0==0) rather than 1 is just silly. 0 is false, 1 is true, and anyone
)> who doesn't already know that isn't going to benefit from the extra
)> verbosity anyway.
)>
) 0 == 1 is necessarily false. 0 is false by convention.

Yes, and by writing 'FALSE = 0' and 'TRUE = 1' you document this convention.

But by writing 'TRUE = 0' and 'FALSE = 1', what are you documenting?

Phil
 
P

Phil Carmody

Lew Pitcher said:
Or,
0 is false, by design

The design is a convention.
true is 'not false'
and 'not' is ~
so, true is ~0

Or any of an uncountable number of ways to say that the C language evaluates
non-zero values as "true" conditions, and zero values as "false"
conditions.

Look at success/failure exit codes in unix shells if you want
an example of a design which follows a different convention.

Phil
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top