空格填充器(alignBySpace)

 1 /*******************************************************************
 2  *                     空格填充器(alignBySpace)
 3  * 声明:
 4  *  1. 软件主要是在不改变文本文件内容情况下,自动填充空格一定数量的
 5  *      空格,达到空格右对齐的功能;
 6  *  2. 本软件主要是节省个人的代码跟踪文档打空格的时间;
 7  *
 8  *                             2015-8-3 晴 深圳 南山平山村 曾剑锋
 9  ******************************************************************/
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 int main ( int argc, char ** argv ) {
15 
16     int    ret                      = 0;
17     int    column                   = 0;
18     char  *tempFile                 = "generateFile.txt";
19     char  *readBuffer               = NULL;
20     size_t bufferSize               = 512;
21     int    readBufferIndex          = 0;
22     int    writeBufferIndex         = 0;
23     char   writeBuffer[bufferSize];
24 
25     if ( ( argc < 2 ) || ( argc > 3 ) ) {
26         printf ( "USAGE:\n" );
27         printf ( "    alignBySpace <file> [column].\n" );
28         return -1;
29     }
30 
31     if ( argc == 2 ) {
32         printf ( "\nUse default columns: 80. \n\n" );
33         column = 80;
34     } else 
35         column = atoi ( argv[2] );
36 
37     FILE *readfile  = fopen ( argv[1], "r" );                         // only read
38     FILE *writefile = fopen ( tempFile, "w" );                        // only write
39 
40     /**
41      * man datasheet:
42      *    If *lineptr is NULL, then getline() will allocate a buffer for storing the line, 
43      *    which should be freed by the user program. (In this case, the value in *n is ignored.)
44      */
45     while ( ( ret = getline( &readBuffer, &bufferSize, readfile ) ) != -1 ) {
46 
47         readBufferIndex  = 0;
48         writeBufferIndex = 0;
49 
50         while ( readBufferIndex < ret ) {
51             if ( readBuffer [ readBufferIndex ] != '\t' )             // kick out '\t'
52                 writeBuffer [ writeBufferIndex++ ] = readBuffer [ readBufferIndex++ ];
53             else {
54                 memset ( writeBuffer + writeBufferIndex, ' ', 4 );    // every tab key was 4 space
55                 writeBufferIndex += 4;    
56                 readBufferIndex++;
57             }
58         }
59 
60         writeBufferIndex--;                                           // back to real index
61 
62         if ( ( column - writeBufferIndex ) > 0 ) {                    // may be real column longer than we set
63             // '\n' at end of a line, need to kick it out
64             memset ( writeBuffer + writeBufferIndex, ' ', column - writeBufferIndex );
65             writeBuffer [ column ] = '\n';                            // for end of a line 
66             writeBuffer [ column + 1 ] = '\0';                        // for end of a string
67         } else 
68             writeBuffer [ writeBufferIndex + 1 ] = '\0';              // make sure end of a string
69 
70         fputs ( writeBuffer, writefile );                             // write to file 
71 
72         bzero ( readBuffer,  bufferSize );                            // clean buffer
73         bzero ( writeBuffer, bufferSize );
74 
75     }
76 
77     free ( readBuffer );                                              // referrence getline()
78 
79     fclose ( readfile );
80     fclose ( writefile );
81 
82     remove ( argv[1] );                                               // delete source file
83     rename ( tempFile, argv[1] );                                     // tempfile rename to source file
84 }

 

posted on 2015-08-03 08:28  zengjf  阅读(425)  评论(0编辑  收藏  举报

导航