Mail Archives: djgpp/1997/08/28/17:36:11
Kevin Dickerson wrote:
[...]
> Really, my problem lies within a very narrow position, and I can't debug
> it because it goes so far as to re-boot my system when I call this
> stupid func! Argh. Oh, and sorry if this message seems vague or
> erroneous, but I haven't been to sleep in roughly 28 hours. Bad habits
> are hard to break, I suppose. :-)
>
> (BTW, text = "Holy crap! This thing actually works! Wow! Amazing!")
Seems to be a bug, because it should simply SIGSEGV instead
of re-boot. ;-)
>
> void drawDialogue(char *text)
> {
> int l1, nwords, len;
> char *chr, *words[2000]; // that would be one freakin' huge dialog
> box. :-)
>
> ...
>
> // This is the problem block:
> for (l1=0; l1 < strlen(text); l1++) // processes text
> character-by-character
> {
> sprintf( chr, "%c", text[l1] ); // prints the scanned character
> to string chr
Well, chr is a pointer to a string, pointing probably
at a distant star or wherever. Maybe to your stack,
thus the sprintf() corrupts it, blowing things up.
> strcat( words[nwords], chr ); // and this line *should*, but
At this point, I loose track of what you really want to
do. What you have done so far is declaring an array of
2000 pointers to characters, not allocating any memory,
use an uninitialized variable (nwords) to access
one of them randomly and expect this to work.
> doesn't concatenate the character
> // ...on to the word currently
> being added to.
> if ( text[l1] == ' ' ) // and if the processed
> character is a space, then...
> {
> nwords++; // move the word up. The
> checking-of-the-bounding-box isn't really
> } // imperitive, because I've
> localized the bug to these commands.
> }
>
> ...
>
> }
>
> "nwords" is a variable containing the current word that chr will be
> writing to.
> "words[]" is an array of strings that contain words (fairly obvious,
> yes?).
Obviously not.
Given you want to wrap a line of text to at most 30 characters,
why don't you try something like this, replacing spaces by
\n in text (untested, so probably buggy ;-)}
void wrap(char *text){
char *start; // pointer to the beginning of the last word.
int ll; // column counter
start = text; // Beware: assuming text starts with letter.
ll = 0;
while(*text){ // As long as we have text to process
// look at each character and see if it is
// on a word boundary
while(*text && !isspace(*text) ){
++text;
++ll;
}
// Now text points to a space or ï\0ï, i.e.
// end of string. If so,
// go home.
if(!*text) break;
// line len exceeded?
if(ll> 30){
// replace the character before the current
// word with CR. Beware: This fails if the first
// word is longer than 30 chars, so add a check!
*(start-1) = ï\nï;
ll=0; // reset linelength
text = start; // and restart with current word.
}
else{
++text; // skip space (hopefully there is only one ;-)
start = text; // set pointer to current word.
}
++text;
}
As I said, this is directly hacked into the mailer, so
it won't work, but I hope you get the idea.
--
Ciao
Tom
*************************************************************
* Thomas Demmer *
* Lehrstuhl fuer Stroemungsmechanik *
* Ruhr-Uni-Bochum *
* Universitaetsstr. 150 *
* D-44780 Bochum *
* Tel: +49 234 700 6434 *
* Fax: +49 234 709 4162 *
* http://www.lstm.ruhr-uni-bochum.de/~demmer *
*************************************************************
- Raw text -