57 |
|
mp->len = 0; |
58 |
|
} |
59 |
|
|
60 |
+ |
/* load memory from an input stream, starting from current position */ |
61 |
+ |
static int |
62 |
+ |
load_stream(MEMLOAD *mp, FILE *fp) |
63 |
+ |
{ |
64 |
+ |
size_t alloced = 0; |
65 |
+ |
char buf[8192]; |
66 |
+ |
size_t nr; |
67 |
+ |
|
68 |
+ |
if (mp == NULL) |
69 |
+ |
return(-1); |
70 |
+ |
mp->base = NULL; |
71 |
+ |
mp->len = 0; |
72 |
+ |
mp->mapped = 0; |
73 |
+ |
if (fp == NULL) |
74 |
+ |
return(-1); |
75 |
+ |
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
76 |
+ |
if (!alloced) |
77 |
+ |
mp->base = malloc(nr); |
78 |
+ |
else if (mp->len+nr > alloced) |
79 |
+ |
mp->base = realloc(mp->base, |
80 |
+ |
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
81 |
+ |
if (mp->base == NULL) |
82 |
+ |
return(-1); |
83 |
+ |
memcpy((char *)mp->base + mp->len, buf, nr); |
84 |
+ |
mp->len += nr; |
85 |
+ |
} |
86 |
+ |
if (ferror(fp)) { |
87 |
+ |
free_load(mp); |
88 |
+ |
return(-1); |
89 |
+ |
} |
90 |
+ |
if (alloced > mp->len*5/4) /* don't waste too much space */ |
91 |
+ |
mp->base = realloc(mp->base, mp->len); |
92 |
+ |
return(mp->len > 0); |
93 |
+ |
} |
94 |
+ |
|
95 |
|
/* load a file into memory */ |
96 |
|
static int |
97 |
|
load_file(MEMLOAD *mp, FILE *fp) |
99 |
|
int fd; |
100 |
|
off_t skip, flen; |
101 |
|
|
102 |
+ |
#ifdef _WIN32 /* too difficult to fix this */ |
103 |
+ |
return load_stream(mp, fp); |
104 |
+ |
#endif |
105 |
|
if (mp == NULL) |
106 |
|
return(-1); |
107 |
|
mp->base = NULL; |
137 |
|
return(1); |
138 |
|
} |
139 |
|
|
102 |
– |
/* load memory from an input stream, starting from current position */ |
103 |
– |
static int |
104 |
– |
load_stream(MEMLOAD *mp, FILE *fp) |
105 |
– |
{ |
106 |
– |
size_t alloced = 0; |
107 |
– |
char buf[8192]; |
108 |
– |
size_t nr; |
109 |
– |
|
110 |
– |
if (mp == NULL) |
111 |
– |
return(-1); |
112 |
– |
mp->base = NULL; |
113 |
– |
mp->len = 0; |
114 |
– |
mp->mapped = 0; |
115 |
– |
if (fp == NULL) |
116 |
– |
return(-1); |
117 |
– |
while ((nr = fread(buf, 1, sizeof(buf), fp)) > 0) { |
118 |
– |
if (!alloced) |
119 |
– |
mp->base = malloc(nr); |
120 |
– |
else if (mp->len+nr > alloced) |
121 |
– |
mp->base = realloc(mp->base, |
122 |
– |
alloced = alloced*(2+(nr==sizeof(buf)))/2+nr); |
123 |
– |
if (mp->base == NULL) |
124 |
– |
return(-1); |
125 |
– |
memcpy((char *)mp->base + mp->len, buf, nr); |
126 |
– |
mp->len += nr; |
127 |
– |
} |
128 |
– |
if (ferror(fp)) { |
129 |
– |
free_load(mp); |
130 |
– |
return(-1); |
131 |
– |
} |
132 |
– |
if (alloced > mp->len*5/4) /* don't waste too much space */ |
133 |
– |
mp->base = realloc(mp->base, mp->len); |
134 |
– |
return(mp->len > 0); |
135 |
– |
} |
136 |
– |
|
140 |
|
/* free a record index */ |
141 |
|
#define free_records(rp) free(rp) |
142 |
|
|
384 |
|
int columns2go = no_columns; |
385 |
|
char word[256]; |
386 |
|
/* sanity checks */ |
387 |
< |
if (comp_size) |
388 |
< |
return(output_stream(fp)); /* binary data -- just copy */ |
387 |
> |
if (comp_size || (no_columns == ni_columns) & (no_rows == ni_rows)) |
388 |
> |
return(output_stream(fp)); /* no-op -- just copy */ |
389 |
|
if (no_columns <= 0) { |
390 |
|
fprintf(stderr, "Missing -oc specification\n"); |
391 |
|
return(0); |
575 |
|
SET_FILE_BINARY(stdout); |
576 |
|
} |
577 |
|
/* check for no-op */ |
578 |
< |
if (!transpose & !i_header & !o_header && (comp_size || |
579 |
< |
(no_columns == ni_columns) & (no_rows == ni_rows))) { |
578 |
> |
if (!transpose & (i_header == o_header) && |
579 |
> |
(no_columns == ni_columns) & (no_rows == ni_rows)) { |
580 |
|
if (warnings) |
581 |
|
fprintf(stderr, "%s: no-op -- copying input verbatim\n", |
582 |
|
argv[0]); |
606 |
|
fputc('\n', stdout); /* finish new header */ |
607 |
|
} |
608 |
|
if (transpose) { /* transposing rows & columns? */ |
609 |
< |
MEMLOAD myMem; /* need to load into memory */ |
609 |
> |
MEMLOAD myMem; /* need to map into memory */ |
610 |
|
if (a == argc-1) { |
611 |
|
if (load_file(&myMem, stdin) <= 0) { |
612 |
|
fprintf(stderr, "%s: error loading file into memory\n", |
621 |
|
if (!do_transpose(&myMem)) |
622 |
|
return(1); |
623 |
|
/* free_load(&myMem); about to exit, so don't bother */ |
621 |
– |
} else if (comp_size || (no_columns==ni_columns) & (no_rows==ni_rows)) { |
622 |
– |
if (!output_stream(stdin)) /* just changed header */ |
623 |
– |
return(1); |
624 |
|
} else if (!do_resize(stdin)) /* reshaping input */ |
625 |
|
return(1); |
626 |
|
return(0); |