home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_09 / 1109058a < prev    next >
Text File  |  1993-07-08  |  2KB  |  52 lines

  1. //  strdetab.cpp
  2.  
  3. #include    "stdhdr.h"
  4. #include    "strdetab.h"
  5.  
  6. const char * strdetab (char * out_buf,
  7.                        const int max_line_length,
  8.                        const char * in_buf,
  9.                        const int no_of_tabs,
  10.                        const int * tab_stops,
  11.                        int * char_line_length)
  12. {
  13. char * in = (char *) in_buf, * out = out_buf;
  14. int out_count = 0;
  15. while (*in && out_count < max_line_length)
  16.     if (*in == '\t')
  17.         {   //  tab found
  18.         if (no_of_tabs == 1)
  19.             //  The tab interval is specified
  20.             {
  21.             int tab_interval = *tab_stops;
  22.             int next_tab_stop = (out_count
  23.                                 / tab_interval + 1)
  24.                                 * tab_interval;
  25.             while (out_count < max_line_length
  26.                    && out_count < next_tab_stop)
  27.                 //  insert spaces in place of the tab
  28.                 {*out++ = ' '; out_count++;}
  29.             }
  30.         else
  31.             //  The positions of the tabs are given
  32.             {
  33.             for (int i = 0;
  34.                  i < no_of_tabs
  35.                     && tab_stops [i] <= out_count;
  36.                  i++);
  37.                 //  all the work is in the iteration!
  38.                 //  find the required tab stop
  39.             while (out_count < max_line_length
  40.                    && out_count < tab_stops [i])
  41.                 //  insert spaces in place of the tab
  42.                 {*out++ = ' '; out_count++;}
  43.             }
  44.         in++;   //  move on to next char
  45.         }
  46.     else    //  No tab, so copy the character
  47.         {*out++ = *in++; out_count++;}
  48. *out = '\0';    // Null-terminate the string
  49. if (char_line_length != NULL) * char_line_length = strlen (out_buf);
  50. return out_buf;
  51. }
  52.