home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / word2x0a.zip / source / col-align.cc < prev    next >
C/C++ Source or Header  |  1997-04-10  |  2KB  |  100 lines

  1. /* $Id: col-align.cc,v 1.3 1997/03/22 20:20:17 dps Exp $ */
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define __EXCLUDE_READER_CLASSES
  5. #include "lib.h"
  6.  
  7. /* Calculate the widths and guess the alignment a column wants */
  8. struct wd_info find_width(int rows, const char *const *cdata)
  9. {
  10.     int max_wd[3], i, not_num, is_num;
  11.     int lt_sp, rt_sp, align_set;
  12.     wd_info res;
  13.     const char *cdp, *sc;
  14.     num_info nd;
  15.  
  16.     max_wd[0]=0;
  17.     max_wd[1]=0;
  18.     max_wd[2]=0;
  19.     is_num=not_num=align_set=0;
  20.     res.align=ALIGN_LEFT;
  21.     res.has_sign=0;
  22.  
  23.     for (i=0; i<rows; i++)
  24.     {
  25.     cdp=cdata[i];
  26.     if (cdp==NULL)
  27.         continue;        // Handle blank entries
  28.  
  29.     nd=scan_num(cdp);
  30.     if (nd.dot_pos==-1)
  31.         not_num++;
  32.     else
  33.     {
  34.         is_num++;
  35.         res.has_sign |= nd.has_sign;
  36.         if (nd.wd[0]>max_wd[0])
  37.         max_wd[0]=nd.wd[0];
  38.         if (nd.wd[1]>max_wd[1])
  39.         max_wd[1]=nd.wd[1];
  40.     }
  41.     if (strlen(cdp)>(unsigned) max_wd[2])
  42.         max_wd[2]=strlen(cdp);
  43.     for (lt_sp=0, sc=cdp; isspace(*sc); sc++, lt_sp++)
  44.     {
  45.         if (*sc==CH_SUSPECT)
  46.         align_set=1;
  47.     }
  48.     if (*sc=='\0')
  49.         continue;        // Blank entry gives no information
  50.  
  51.     while (*sc!='\0')
  52.     {
  53.         if (*sc==CH_SUSPECT)
  54.         align_set=1;
  55.         if (isspace(*sc))
  56.         rt_sp++;
  57.         else
  58.         rt_sp=0;
  59.         sc++;
  60.     }
  61.  
  62.     if (align_set)
  63.         continue;
  64.  
  65.     if (lt_sp==0 && rt_sp<=1)
  66.         continue;
  67.     if (lt_sp-rt_sp<-1)
  68.     {
  69.         res.align=ALIGN_LEFT;
  70.         continue;
  71.     }
  72.     if (lt_sp-rt_sp>1)
  73.     {
  74.         res.align=ALIGN_RIGHT;
  75.         continue;
  76.     }
  77.     res.align=ALIGN_CENTER;
  78.     }
  79.  
  80.     if (is_num>not_num)
  81.     {
  82.     /*
  83.      * Decimal alignment, set dp_col and leave the alignment
  84.      * alone to handle non-number entries nicely.
  85.      */
  86.     res.width=max_wd[0]+(max_wd[1]>0) ? 0 : 1;
  87.     res.dp_col=max_wd[0]+1;
  88.     }
  89.     else
  90.     {
  91.     res.width=0;
  92.     res.dp_col=-1;
  93.     }
  94.  
  95.     if (res.width<max_wd[2])
  96.     res.width=max_wd[2];    // dp_col irrelant
  97.  
  98.     return res;
  99. }
  100.