home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / wrowcol.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  2KB  |  81 lines

  1. /*! wstrrowcol
  2.  *
  3.  *     a 2-dimensional strlen() function, this routine computes the
  4.  *    number of rows and columns needed to display a string.
  5.  *
  6.  *    Assumptions:    new lines are flagged with \n
  7.  *                      Lines do not wrap around the window edge.
  8.  *            (routine may be used to compute size of next window)
  9.  *
  10.  *    This routine will FAIL
  11.  *        if the string contains embedded \t tab chars
  12.  *              if the string is longer than the current window width
  13.  *
  14.  *
  15.  *    parameters: text is ptr to a string.
  16.  *        the number of rows and columns needed are placed in
  17.  *        *rows, *cols.
  18.  *    RETURN: void.
  19.  */
  20.  
  21.  
  22.  
  23. #include "wsys.h"
  24.  
  25.  
  26.  
  27.  
  28.  
  29. void wstrrowcol (char *text, int *rows, int*cols )
  30.     {
  31.     int linecnt;          /* #lines in text */
  32.     int longest;        /* longest line in text */
  33.     int len;
  34.  
  35.  
  36.     char *ptr;
  37.     char *last_ptr;
  38.  
  39.     /* 'normalize' the pointers  (if the memory model requires it)
  40.      * so we can add to them without wrapping around
  41.      * segment boundaries
  42.      */
  43.     _NORMALIZE (text);
  44.  
  45.  
  46.  
  47.  
  48.     /* count lines in text  and size of largest line */
  49.  
  50.     ptr = last_ptr = text;
  51.  
  52.     linecnt = longest = 0;
  53.  
  54.     while (NULL != (ptr = strchr (ptr+1, '\n')) )
  55.         {
  56.         ++linecnt;
  57.  
  58.         if ( longest < (int)(ptr - last_ptr) )
  59.             {
  60.             /* longest line so far */
  61.             longest = (int) (ptr - last_ptr);
  62.             }
  63.         last_ptr = ptr;
  64.         }
  65.  
  66.     /*now look over the last line (or only line) for line length.
  67.     */
  68.     len = strlen(last_ptr);
  69.     if ( longest < len )
  70.         {
  71.         longest = len;
  72.         }
  73.  
  74.     *rows = linecnt;
  75.     *cols = longest;
  76.  
  77.     return;
  78.     }  /*end of wstrrowcol */
  79.  
  80.  
  81.