home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 40 < prev    next >
Encoding:
Text File  |  1990-12-02  |  3.8 KB  |  221 lines

  1. Path: wuarchive!usc!cs.utexas.edu!uwm.edu!rutgers!aramis.rutgers.edu!paul.rutgers.edu!yoko.rutgers.edu!jac
  2. From: jac@yoko.rutgers.edu (Jonathan A. Chandross)
  3. Newsgroups: comp.sources.apple2
  4. Subject: v001SRC017:  sort -- Sort Text Files In Memory
  5. Message-ID: <Dec.1.16.57.50.1990.24860@yoko.rutgers.edu>
  6. Date: 1 Dec 90 21:57:51 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 210
  9. Approved: jac@paul.rutgers.edu
  10.  
  11.  
  12. Submitted-by: NONE
  13. Posting-number: Volume 1, Source:17
  14. Archive-name: util/sort
  15. Architecture: ANY_2
  16. Version-number: 1.00
  17.  
  18. This C utility sorts text files in memory.  Both normal and reverse
  19. sorting are supported.
  20.  
  21. Enjoy.
  22.  
  23.  
  24. =sort.c
  25. -/*
  26. - * sort.c
  27. - *
  28. - * Sort text files in memory.
  29. - *
  30. - * Usage:
  31. - *     sort [-r] [file_1] [file_2] [file_3] [...]
  32. - *
  33. - *     Options:
  34. - *        -r    sort in reverse order
  35. - *
  36. - *    If no file is specified, sort reads from standard in.
  37. - *
  38. - * Contributed Anonymously.  Written: November 1983
  39. - *
  40. - * Version 1.00
  41. - *
  42. - */
  43. -
  44. -#include "stdio.h"
  45. -
  46. -#define TRUE  1
  47. -#define FALSE 0
  48. -#define NL    '\n'
  49. -#define EOS   '\0'
  50. -#define MAXTEXT 16384
  51. -#define MAXPTR  1024
  52. -
  53. -char linebuf[MAXTEXT] ;
  54. -char *lineptr[MAXPTR] ;
  55. -int reverse ;
  56. -
  57. -main(argc, argv)
  58. -int argc ;
  59. -char *argv[] ;
  60. -{
  61. -    char *s ;
  62. -    FILE *input ;
  63. -
  64. -
  65. -    reverse = FALSE ;
  66. -    while( --argc>0 && **++argv == '-' )
  67. -        for( s=&argv[0][1] ; *s != EOS ; s++ )
  68. -            switch( *s ) {
  69. -                case 'r':
  70. -                    reverse = TRUE ;
  71. -                    break ;
  72. -                default:
  73. -                    fprintf(stderr, "sort: unknown option %c\n", *s ) ;
  74. -                    argc = -1 ;
  75. -                    break ;
  76. -            }
  77. -
  78. -    if( argc < 0 ) {
  79. -        fprintf(stderr, "usage: sort files\n");
  80. -        exit(1) ;
  81. -    }
  82. -
  83. -    if( argc == 0 )
  84. -        sort( stdin, "stdin" ) ;
  85. -    else
  86. -        for( ; argc>0 ; argc--,argv++)
  87. -            if( (input=fopen(*argv,"r")) == NULL ) {
  88. -                fprintf(stderr, "sort: can't open %s\n", *argv) ;
  89. -                exit(1) ;
  90. -            }
  91. -            else {
  92. -                sort( input, *argv ) ;
  93. -                fclose( input ) ;
  94. -            }
  95. -
  96. -    exit(0) ;
  97. -
  98. -} /* end main */
  99. -
  100. -
  101. -/* sort - sort text files in memory */
  102. -
  103. -sort( in, fname )
  104. -FILE *in ;
  105. -char *fname ;
  106. -{
  107. -    int nlines ;
  108. -
  109. -    if( (nlines=readlines(in)) > 0 ) {
  110. -        sortlines( 0, nlines-1 ) ;
  111. -        printlines( stdout, nlines ) ;
  112. -    }
  113. -    else if( nlines < 0 )
  114. -        fprintf(stderr, "sort: file is too large: %s\n", fname ) ;
  115. -
  116. -} /* end sort */
  117. -
  118. -
  119. -/* readlines - store text in linebuf */
  120. -
  121. -readlines( in )
  122. -FILE *in ;
  123. -{
  124. -    int nlines, len ;
  125. -    char *lbptr, *endbuffer ;
  126. -
  127. -    nlines = 0 ;
  128. -    lbptr = &linebuf[0] ;
  129. -    endbuffer = &linebuf[MAXTEXT-1] ;
  130. -
  131. -    do {
  132. -        if( (len = getline(lbptr, in)) == EOF )
  133. -            return nlines ;
  134. -        lineptr[ nlines ] = lbptr ;
  135. -        nlines++ ;
  136. -        lbptr += len + 1 ;    /* 1 for EOS */
  137. -    } while( lbptr < endbuffer && nlines < MAXPTR ) ;
  138. -
  139. -    return EOF ;
  140. -
  141. -} /* end readlines */
  142. -
  143. -
  144. -/* sortlines - quicksort on pointers */
  145. -
  146. -sortlines( left, right )
  147. -int left, right ;
  148. -{
  149. -    int l, r, middle ;
  150. -    char *pivot, *temp ;
  151. -    l = left ; r = right ;
  152. -    middle = (l + r) / 2 ;
  153. -    pivot = lineptr[ middle ] ;
  154. -
  155. -    do {
  156. -
  157. -            while( strcmp(lineptr[l],pivot) < 0 )
  158. -                l++ ;
  159. -            while( strcmp(pivot,lineptr[r]) < 0 )
  160. -                r-- ;
  161. -
  162. -        if( l <= r ) {
  163. -            temp = lineptr[l] ;
  164. -            lineptr[l] = lineptr[r] ;
  165. -            lineptr[r] = temp ;
  166. -            l++ ;
  167. -            r-- ;
  168. -        }
  169. -
  170. -    } while( l <= r ) ;
  171. -
  172. -    if( left < r )
  173. -        sortlines( left, r ) ;
  174. -    if( l < right )
  175. -        sortlines( l, right ) ;
  176. -
  177. -} /* end sortlines */
  178. -
  179. -/* printlines */
  180. -
  181. -printlines( out, nlines )
  182. -FILE *out ;
  183. -int nlines ;
  184. -{
  185. -    int i ;
  186. -
  187. -    if( reverse )
  188. -        for( i=nlines-1 ; i >= 0 ; i-- )
  189. -            fprintf( out, "%s", lineptr[i] ) ;
  190. -    else
  191. -        for( i=0 ; i < nlines ; i++ )
  192. -            fprintf( out, "%s", lineptr[i] ) ;
  193. -
  194. -} /* end printlines */
  195. -
  196. -
  197. -/* getline - read input line */
  198. -
  199. -getline( ptr, input )
  200. -char *ptr ;
  201. -FILE *input ;
  202. -{
  203. -    int c ;
  204. -    char *bufptr ;
  205. -
  206. -    bufptr = ptr ;
  207. -
  208. -    while( (c=agetc(input)) != NL ) {
  209. -        if( c == EOF )
  210. -            return EOF ;
  211. -        *bufptr++ = c ;
  212. -    }
  213. -
  214. -    *bufptr++ = NL ;
  215. -    *bufptr++ = EOS ;
  216. -    return bufptr - ptr ;
  217. -
  218. -} /* end getline */
  219. -
  220. + END OF ARCHIVE
  221.