1 /*
2  * Copyright (c) 2016,2017  Daichi GOTO
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #define VERSION "20170916"
29 #define CMDNAME "conv_ssv2ssv"
30 #define ALIAS "ssv2ssv"
31 
32 #include "ttt.h"
33 
34 #define TGT_CHAR_PROCESS(C) \
35 	if (C == ' ') \
36 		++retu; \
37 	else if ((C == '\n') || \
38 	         (C == EOF && (p != '\n' && p != EOF)) || \
39 		 (C == EOF && p == EOF)) { \
40 		if (C == EOF && p == EOF) \
41 			retu = 0; \
42 		else \
43 			++retu; \
44 		if ((C == EOF && p == EOF && -1 == retumax) || \
45 		    retumax < retu) \
46 			retumax = retu; \
47 		retu = 0; \
48 	} \
49 	p = C;
50 
51 #define TGT_GYO_PROCESS(GYO_BUFFER,NF) \
52 	col = 1; \
53 	if (FILEPROCESS_GYO_FILESIZE_IS_ZERO) \
54 		goto tgt_gyo_process_end; \
55 	if (FLAG_w) { \
56 		j = 0; \
57 		for (i = 1; i <= NF; i++) { \
58 			PRINT_COLNUMBER \
59 			printf("%s", GYO_BUFFER[i]); \
60 			++j; \
61 			if (j == wrap) { \
62 				if (i != NF) { \
63 					j = 0; \
64 					printf("\n"); \
65 				} \
66 			} \
67 			else \
68 				printf(" "); \
69 		} \
70 		++j; \
71 		while (j < wrap) { \
72 			PRINT_COLNUMBER \
73 			printf("@ "); \
74 			++j; \
75 		} \
76 		if (j == wrap) { \
77 			PRINT_COLNUMBER \
78 			printf("@"); \
79 		} \
80 		printf("\n"); \
81 	} \
82 	else { \
83 		for (i = 1; i < retumax; i++) { \
84 			PRINT_COLNUMBER \
85 			if (i > NF) \
86 				printf("@ "); \
87 			else \
88 				printf("%s ", GYO_BUFFER[i]); \
89 		} \
90 		if (retumax != 0) { \
91 			PRINT_COLNUMBER \
92 			if (i > NF) \
93 				printf("@\n"); \
94 			else \
95 				printf("%s\n", GYO_BUFFER[retumax]); \
96 		} \
97 	} \
98 tgt_gyo_process_end:
99 
100 #define PRINT_COLNUMBER \
101 	if (FLAG_n) { \
102 		printf("%d:", col++); \
103 	}
104