delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/28/17:36:11

From: Thomas Demmer <demmer AT LSTM DOT Ruhr-UNI-Bochum DOT De>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: String formatting problems
Date: Thu, 28 Aug 1997 14:19:06 +0200
Organization: Lehrstuhl fuer Stroemungsmechanik
Lines: 138
Message-ID: <34056CBA.152A8249@LSTM.Ruhr-UNI-Bochum.De>
References: <34042ABC DOT 892782F5 AT phs DOT mat-su DOT k12 DOT ak DOT us>
NNTP-Posting-Host: c64.lstm.ruhr-uni-bochum.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019