home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2398 / starbox.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.4 KB  |  100 lines

  1. /*
  2. Name          starbox  - puts title in a centered box of stars.
  3.  
  4. Usage         #include "usual.h"
  5.               #include "tools.h"
  6.               char* starbox(char *title, char terminator);
  7.  
  8. Prototype in  tools.h
  9.  
  10. Description   title is a character string.  The string is made up of  
  11.               concatonated lines.  A line is made up of 0-68 characters 
  12.               followed by a slash (default).  Examples are:
  13.               cout << starbox("//line 1/line 2/line 3///");
  14.               cout << starbox("\n\nline 1\nline 2\nline 3\n\n\n",'\n');
  15.  
  16. Return value  Character string.
  17.  
  18. Functions     Library: strcat, strchr, strlen
  19. called        Tools:   (none)
  20.               Sublib:  (none)
  21. */
  22.  
  23. #include "usual.h"
  24. #include "tools.h"
  25.  
  26. char * starbox(char *title, char terminator)
  27. {
  28.   char      border[81],tab[81],work[256],middle[81];
  29.   char      *line, *begin, *end, *t;
  30.   int       i,pad,mpad,linesize;
  31.   long int  length, cum_length;
  32.  
  33.   char      starbox_str_pointer[1024];
  34.   long int  starbox_str_length = 1024;
  35.  
  36.   cum_length = 0;
  37.   *starbox_str_pointer = '\0';
  38.  
  39.   for (i=0; i<70; i++)
  40.     border[i] = '*';
  41.   border[70] = '\n'; border[71] = '\0';
  42.  
  43.   ((LINESIZE<72)||(LINESIZE>133)) ? (linesize=133) : (linesize=LINESIZE);
  44.   
  45.   pad = (linesize-72)/2+1;
  46.   for (i=0; i<pad; i++)
  47.     tab[i] = ' ';
  48.   tab[pad] = '\0';
  49.  
  50.   work[0]='\n'; work[1]='\n'; work[2]='\0';
  51.   line = strcat(work,tab);
  52.   line = strcat(work,border);
  53.  
  54.   cum_length += strlen(line);
  55.  
  56.   if (cum_length < starbox_str_length)
  57.     strcat(starbox_str_pointer,line);
  58.   else 
  59.     return "Error, starbox, title too long.";
  60.  
  61.   begin = title;
  62.   while ( (end=strchr(begin,terminator)) != NULL ) {
  63.     length = end - begin;
  64.     if (length > 68) return "Error, starbox, line too long.";
  65.     middle[0] = '*';
  66.     mpad = (68-length)/2+1;
  67.     for (i=1; i<=mpad; i++) middle[i] = ' ';
  68.     t = &middle[mpad];
  69.     while (begin < end) *t++ = *begin++;
  70.     begin++;
  71.     for (i=mpad+length; i<=68; i++) middle[i]=' ';
  72.     middle[69]='*'; middle[70]='\n'; middle[71]='\0';
  73.     work[0]='\0';
  74.     line = strcat(work,tab);
  75.     line = strcat(work,middle);
  76.  
  77.  
  78.     if (cum_length < starbox_str_length)
  79.       strcat(starbox_str_pointer,line);
  80.     else 
  81.       return "Error, starbox, title too long.";
  82.  
  83.   }
  84.  
  85.   work[0]='\0';
  86.   line = strcat(work,tab);
  87.   line = strcat(work,border);
  88.  
  89.   if (cum_length < starbox_str_length)
  90.     strcat(starbox_str_pointer,line);
  91.   else 
  92.     return "Error, starbox, title too long.";
  93.  
  94.   return starbox_str_pointer;
  95.  
  96. }
  97.  
  98.  
  99.  
  100.