home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcu16.zip / bin / incl_trunc.c < prev    next >
C/C++ Source or Header  |  1991-10-03  |  4KB  |  155 lines

  1. /*
  2.  * Copyright 1991 Cornell University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Cornell U. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Cornell U. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * CORNELL UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL CORNELL UNIVERSITY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Gene W. Dykes, Program of Computer Graphics
  23.  *          580 Theory Center, Cornell University, Ithaca, NY 14853
  24.  *          (607) 255-6713   gwd@graphics.cornell.edu
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. void wlm_incl_trunc () ;
  30.  
  31. /*
  32.  # The algorithm for this is to strip off the lower case letters,
  33.  # a word at a time, starting with the trailing words, until the basename
  34.  # is 9 characters or fewer.  Example:
  35.  #   VeryLongWidgetName.h (too long)
  36.  #   VeryLongWidgetN.h (still too long)
  37.  #   VeryLongWN.h (still too long)
  38.  #   VeryLWN.h (okay now)
  39.  # But, when down to the last word, only strip just enough letters.
  40.  # Examples:
  41.  #   AtrociouslyLongWidgetName.h -> AtrociLWN.h
  42.  #   Supercallifragilistic.h -> Supercall.h
  43.  */
  44.  
  45. main (argc, argv)
  46.     int argc ;
  47.     char **argv ;
  48. {
  49. if (argc < 2)
  50.     {
  51.     fprintf (stderr, "Usage: %s basename_string\n", argv[0]) ;
  52.     exit (1) ;
  53.     }
  54.  
  55. wlm_incl_trunc (argv[1]) ;
  56. exit (0) ;
  57. }
  58.  
  59. #define MAX_WORDS 9
  60.  
  61. static void get_chunks () ;
  62.  
  63. void
  64. wlm_incl_trunc (in)
  65.     char *in ;
  66. {
  67. char out[10] ;
  68. char *chunks[MAX_WORDS] ;
  69. int letter_count[MAX_WORDS] ;
  70. int i ;
  71. int count ;
  72. int n_chunks ;
  73. int total_letter_count ;
  74.  
  75. /*
  76.  * First, separate the name into word chunks.
  77.  * OneTwoThree -> One Two Three
  78.  */
  79.  
  80. get_chunks (chunks, letter_count, &total_letter_count,
  81.         MAX_WORDS, &n_chunks, in) ;
  82.  
  83. /*
  84.  * Subtract the lowercase letter count until okay
  85.  */
  86.  
  87. for (i=n_chunks-1;  i > 0;  i--)
  88.     {
  89.     if (total_letter_count <= 9)
  90.     break ;
  91.     total_letter_count -= letter_count[i]-1 ;
  92.     letter_count[i] = 1 ;
  93.     }
  94.  
  95. if (total_letter_count > 9)
  96.     letter_count[0] -= total_letter_count-9 ;
  97.  
  98. count = 0 ;
  99. for (i=0;  i < n_chunks;  i++)
  100.     {
  101.     strncpy (&out[count], chunks[i], letter_count[i]) ;
  102.     count += letter_count[i] ;
  103.     }
  104. out[count] = '\0' ;
  105. printf ("%s", out) ;
  106. return ;
  107. }
  108.  
  109. static void
  110. get_chunks (chunks, letter_count, total_letter_count, max_chunks, n_chunks, in)
  111.     char *chunks[] ;
  112.     int letter_count[] ;
  113.     int *total_letter_count ;
  114.     int max_chunks ;
  115.     int *n_chunks ;
  116.     char *in ;
  117. {
  118. char *ptr ;
  119. char *original_in = in ;
  120. *n_chunks = 0 ;
  121. *total_letter_count = 0 ;
  122.  
  123. while (*in != '\0')
  124.     {
  125.     if (*n_chunks == max_chunks)
  126.     {
  127.     fprintf (stderr,
  128.     "Ridiculous number of words (greater than %d) in classname (%s)\n",
  129.     max_chunks, original_in) ;
  130.     exit (1) ;
  131.     }
  132.     letter_count[*n_chunks] = 1 ;
  133.     *total_letter_count += 1 ;
  134.     /* skip initial capital letter */
  135.     ptr = in+1 ;
  136.  
  137.     /* increment until next word or end of text */
  138.     while (!isupper(*ptr) && *ptr != '\0')
  139.     {
  140.     ptr++ ;
  141.     letter_count[*n_chunks]++ ;
  142.     *total_letter_count += 1 ;
  143.     }
  144.  
  145.     /* copy the word found into a chunk */
  146.     chunks[*n_chunks] = (char *)malloc (letter_count[*n_chunks]+1) ;
  147.     strncpy (chunks[*n_chunks], in, letter_count[*n_chunks]) ;
  148.     chunks[*n_chunks][letter_count[*n_chunks]] = '\0' ;
  149.  
  150.     in = ptr ;
  151.     *n_chunks += 1 ;
  152.     }
  153. return ;
  154. }
  155.