Repository navigation
Expand file tree
/
Copy pathstrsplit.c
More file actions
579 lines (509 loc) · 18 KB
/
Copy pathstrsplit.c
File metadata and controls
579 lines (509 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <ctype.h>
#include <getopt.h>
#include "config.h"
#define EXIT_SUCCESS_CODE 0
#define EXIT_INVALID_ARGS 1
#define EXIT_RUNTIME_ERROR 2
#define INITIAL_ITEMS_CAPACITY 64
/* Sort modes */
typedef enum {
SORT_NONE = 0,
SORT_IGNORE_CASE,
SORT_NUMERIC,
SORT_GENERAL_NUMERIC,
SORT_HUMAN_NUMERIC,
SORT_DICTIONARY,
SORT_VERSION
} SortMode;
/* Configuration structure */
typedef struct {
char *delim_pattern; /* Delimiter regex pattern */
int linenum; /* Enable line numbering */
int zero_based; /* Use 0-based numbering */
int reset_num; /* Reset numbering between runs */
char *format; /* Output format string */
int insert_nl_count; /* Number of blank lines between runs */
int indent_level; /* Indentation level (number of -i flags) */
int reversed; /* Reverse sort order */
SortMode sort_mode; /* Sort mode */
} Config;
/* Item structure for sorting */
typedef struct {
char *text;
char *original; /* Original text (for case-insensitive sort) */
int index; /* Original index */
} Item;
/* Global config */
static Config config = {
.delim_pattern = "\\s+",
.linenum = 0,
.zero_based = 0,
.reset_num = 0,
.format = NULL,
.insert_nl_count = 0,
.indent_level = 0,
.reversed = 0,
.sort_mode = SORT_NONE
};
/* Function prototypes */
static void print_usage(FILE *out);
static int parse_args(int argc, char *argv[]);
static Item *split_string(const char *input, regex_t *compiled, int *count);
static void free_items(Item *items, int count);
static int compare_items(const void *a, const void *b);
static void sort_items(Item *items, int count);
static void print_items(Item *items, int count, int start_num, int *current_num);
static int process_run(const char *input, int *current_num, int is_first);
static long human_numeric_value(const char *str);
static int version_compare(const char *a, const char *b);
/* Print usage information */
static void print_usage(FILE *out) {
fprintf(out, "Usage: strsplit [OPTIONS] ARGS...\n");
fprintf(out, "\n");
fprintf(out, "Split input string(s) by delimiter and print each item.\n");
fprintf(out, "\n");
fprintf(out, "Options:\n");
fprintf(out, " -d, --delim REGEX Delimiter regex (default: \\\\s+)\n");
fprintf(out, " -n, --linenum Enable line numbering\n");
fprintf(out, " -0, --0-based Use 0-based numbering\n");
fprintf(out, " -z, --reset Reset numbering between runs\n");
fprintf(out, " -c, --format FMT Output format (%%d=index, %%s=item)\n");
fprintf(out, " -N, --insert-nl Insert blank line(s) between runs (repeatable)\n");
fprintf(out, " -i, --indented Indent output by 2 spaces (repeatable)\n");
fprintf(out, " -r, --reversed Reverse sort order\n");
fprintf(out, " -f, --ignore-case Case-insensitive sort\n");
fprintf(out, " -s, --numeric-sort Numeric sort (strtol)\n");
fprintf(out, " -g, --general-numeric-sort General numeric sort (strtod)\n");
fprintf(out, " -H, --human-numeric-sort Human numeric sort (2K, 1M, etc)\n");
fprintf(out, " -D, --dictionary-sort Dictionary sort (ignore non-alnum)\n");
fprintf(out, " -V, --version-sort Version sort (1.9 < 1.10)\n");
fprintf(out, " --version Print version information\n");
fprintf(out, " --help Show this help\n");
}
/* Parse command line arguments */
static int parse_args(int argc, char *argv[]) {
static struct option long_options[] = {
{"delim", required_argument, 0, 'd'},
{"linenum", no_argument, 0, 'n'},
{"0-based", no_argument, 0, '0'},
{"reset", no_argument, 0, 'z'},
{"format", required_argument, 0, 'c'},
{"insert-nl", no_argument, 0, 'N'},
{"indented", no_argument, 0, 'i'},
{"reversed", no_argument, 0, 'r'},
{"ignore-case", no_argument, 0, 'f'},
{"numeric-sort", no_argument, 0, 's'},
{"general-numeric-sort", no_argument, 0, 'g'},
{"human-numeric-sort", no_argument, 0, 'H'},
{"dictionary-sort", no_argument, 0, 'D'},
{"version-sort", no_argument, 0, 'V'},
{"version", no_argument, 0, 1000},
{"help", no_argument, 0, 1001},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "d:n0zc:NirfsgHDVv", long_options, &option_index)) != -1) {
switch (opt) {
case 'd':
config.delim_pattern = optarg;
break;
case 'n':
config.linenum = 1;
break;
case '0':
config.zero_based = 1;
break;
case 'z':
config.reset_num = 1;
break;
case 'c':
config.format = optarg;
break;
case 'N':
config.insert_nl_count++;
break;
case 'i':
config.indent_level++;
break;
case 'r':
config.reversed = 1;
break;
case 'f':
config.sort_mode = SORT_IGNORE_CASE;
break;
case 's':
config.sort_mode = SORT_NUMERIC;
break;
case 'g':
config.sort_mode = SORT_GENERAL_NUMERIC;
break;
case 'H':
config.sort_mode = SORT_HUMAN_NUMERIC;
break;
case 'D':
config.sort_mode = SORT_DICTIONARY;
break;
case 'V':
config.sort_mode = SORT_VERSION;
break;
case 1000:
printf("strsplit %s\n", VERSION);
printf("author: Lenik (strsplit@bodz.net)\n");
printf("strsplit is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License.\n");
exit(EXIT_SUCCESS_CODE);
case 1001:
print_usage(stdout);
exit(EXIT_SUCCESS_CODE);
default:
print_usage(stderr);
return -1;
}
}
/* Set default format if not specified */
if (!config.format) {
if (config.linenum) {
config.format = "%d. %s";
} else {
config.format = "%s";
}
}
return optind;
}
/* Extract numeric value for human numeric sort (e.g., "2K" -> 2048) */
static long human_numeric_value(const char *str) {
char *endptr;
long val = strtol(str, &endptr, 10);
if (endptr && *endptr) {
char suffix = toupper((unsigned char)*endptr);
switch (suffix) {
case 'K': val *= 1024; break;
case 'M': val *= 1024 * 1024; break;
case 'G': val *= 1024 * 1024 * 1024; break;
case 'T': val *= 1024L * 1024L * 1024L * 1024L; break;
}
}
return val;
}
/* Version comparison for version sort */
static int version_compare(const char *a, const char *b) {
while (*a || *b) {
/* Skip non-digits */
while (*a && !isdigit((unsigned char)*a)) a++;
while (*b && !isdigit((unsigned char)*b)) b++;
/* Extract numbers */
char *end_a, *end_b;
long num_a = (*a) ? strtol(a, &end_a, 10) : 0;
long num_b = (*b) ? strtol(b, &end_b, 10) : 0;
if (num_a != num_b) {
return (num_a > num_b) ? 1 : -1;
}
a = end_a;
b = end_b;
}
return 0;
}
/* Comparison function for qsort */
static int compare_items(const void *a, const void *b) {
const Item *ia = (const Item *)a;
const Item *ib = (const Item *)b;
int result = 0;
switch (config.sort_mode) {
case SORT_IGNORE_CASE:
result = strcasecmp(ia->text, ib->text);
break;
case SORT_NUMERIC: {
char *end_a, *end_b;
long val_a = strtol(ia->text, &end_a, 10);
long val_b = strtol(ib->text, &end_b, 10);
if (!*end_a && !*end_b) {
/* Both are purely numeric, compare values */
result = (val_a > val_b) - (val_a < val_b);
} else {
/* Fall back to string comparison */
result = strcmp(ia->text, ib->text);
}
break;
}
case SORT_GENERAL_NUMERIC: {
char *end_a, *end_b;
double val_a = strtod(ia->text, &end_a);
double val_b = strtod(ib->text, &end_b);
result = (val_a > val_b) - (val_a < val_b);
break;
}
case SORT_HUMAN_NUMERIC: {
long val_a = human_numeric_value(ia->text);
long val_b = human_numeric_value(ib->text);
result = (val_a > val_b) - (val_a < val_b);
break;
}
case SORT_DICTIONARY: {
/* Compare ignoring non-alphanumeric characters */
const char *pa = ia->text;
const char *pb = ib->text;
while (*pa || *pb) {
while (*pa && !isalnum((unsigned char)*pa)) pa++;
while (*pb && !isalnum((unsigned char)*pb)) pb++;
if (toupper((unsigned char)*pa) != toupper((unsigned char)*pb)) {
result = toupper((unsigned char)*pa) - toupper((unsigned char)*pb);
break;
}
if (*pa) pa++;
if (*pb) pb++;
}
break;
}
case SORT_VERSION:
result = version_compare(ia->text, ib->text);
break;
case SORT_NONE:
default:
/* Preserve original order */
result = ia->index - ib->index;
break;
}
if (config.reversed) {
result = -result;
}
return result;
}
/* Sort items array */
static void sort_items(Item *items, int count) {
if (count <= 1) return;
if (config.sort_mode == SORT_NONE && !config.reversed) {
/* No sorting needed, preserve order */
return;
}
qsort(items, count, sizeof(Item), compare_items);
}
/* Split input string by delimiter regex */
static Item *split_string(const char *input, regex_t *compiled, int *count) {
*count = 0;
if (!input || !*input) {
return NULL;
}
Item *items = NULL;
int capacity = INITIAL_ITEMS_CAPACITY;
int idx = 0;
items = malloc(capacity * sizeof(Item));
if (!items) {
return NULL;
}
const char *end = input;
regmatch_t pmatch[1];
while (*end) {
int ret = regexec(compiled, end, 1, pmatch, 0);
if (ret == REG_NOMATCH) {
/* No more matches, take rest of string */
size_t len = strlen(end);
if (len > 0 || idx == 0) {
/* Grow array if needed */
if (idx >= capacity) {
capacity *= 2;
Item *new_items = realloc(items, capacity * sizeof(Item));
if (!new_items) {
free_items(items, idx);
return NULL;
}
items = new_items;
}
items[idx].text = strndup(end, len);
items[idx].original = items[idx].text;
items[idx].index = idx;
idx++;
}
break;
} else if (ret == 0) {
/* Match found */
size_t match_start = (size_t)(end - input + pmatch[0].rm_so);
size_t match_len = (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
/* Extract item before delimiter */
size_t item_len = match_start - (size_t)(end - input);
/* Grow array if needed */
if (idx >= capacity) {
capacity *= 2;
Item *new_items = realloc(items, capacity * sizeof(Item));
if (!new_items) {
free_items(items, idx);
return NULL;
}
items = new_items;
}
items[idx].text = strndup(end, item_len);
items[idx].original = items[idx].text;
items[idx].index = idx;
idx++;
/* Move past delimiter */
end = input + match_start + match_len;
/* Handle empty item at end (delimiter at end of string) */
if (*end == '\0' && match_len > 0) {
/* Trailing delimiter produces empty item */
if (idx >= capacity) {
capacity *= 2;
Item *new_items = realloc(items, capacity * sizeof(Item));
if (!new_items) {
free_items(items, idx);
return NULL;
}
items = new_items;
}
items[idx].text = strdup("");
items[idx].original = items[idx].text;
items[idx].index = idx;
idx++;
}
} else {
/* Regex error */
free_items(items, idx);
return NULL;
}
}
*count = idx;
return items;
}
/* Free items array */
static void free_items(Item *items, int count) {
if (!items) return;
for (int i = 0; i < count; i++) {
free(items[i].text);
}
free(items);
}
/* Print items with formatting */
static void print_items(Item *items, int count, int start_num, int *current_num) {
if (!items || count <= 0) return;
for (int i = 0; i < count; i++) {
/* Print indentation */
for (int j = 0; j < config.indent_level; j++) {
printf(" ");
}
/* Print formatted output */
int num = config.zero_based ? (start_num + i) : (start_num + i + 1);
if (config.linenum) {
/* Replace %d and %s in format string */
const char *fmt = config.format;
while (*fmt) {
if (*fmt == '%' && *(fmt + 1) == 'd') {
printf("%d", num);
fmt += 2;
} else if (*fmt == '%' && *(fmt + 1) == 's') {
printf("%s", items[i].text);
fmt += 2;
} else {
putchar(*fmt);
fmt++;
}
}
} else {
/* No line number, just use format with %s */
const char *fmt = config.format;
while (*fmt) {
if (*fmt == '%' && *(fmt + 1) == 's') {
printf("%s", items[i].text);
fmt += 2;
} else if (*fmt == '%') {
/* Pass through to printf for other format specifiers */
putchar(*fmt);
fmt++;
if (*fmt) {
putchar(*fmt);
fmt++;
}
} else {
putchar(*fmt);
fmt++;
}
}
}
putchar('\n');
}
if (current_num) {
*current_num += count;
}
}
/* Process one run (one input string) */
static int process_run(const char *input, int *current_num, int is_first) {
if (!input || !*input) {
return 0;
}
/* Compile delimiter regex */
regex_t compiled;
int ret = regcomp(&compiled, config.delim_pattern, REG_EXTENDED);
if (ret != 0) {
fprintf(stderr, "Error: Invalid regex pattern\n");
return EXIT_RUNTIME_ERROR;
}
/* Split input */
int count = 0;
Item *items = split_string(input, &compiled, &count);
regfree(&compiled);
if (!items || count == 0) {
return 0;
}
/* Sort items */
sort_items(items, count);
/* Print separator before this run (if not first) */
if (!is_first && config.insert_nl_count > 0) {
for (int i = 0; i < config.insert_nl_count; i++) {
putchar('\n');
}
}
/* Determine starting number for this run */
int start_num = 0;
if (config.reset_num) {
start_num = 0;
} else {
start_num = *current_num;
}
/* Print items */
print_items(items, count, start_num, current_num);
/* Free items */
free_items(items, count);
return 0;
}
int main(int argc, char *argv[]) {
/* Parse args - returns negative on error, otherwise returns optind */
int arg_start = parse_args(argc, argv);
if (arg_start < 0) {
return EXIT_INVALID_ARGS;
}
int current_num = 0;
int is_first = 1;
int exit_code = EXIT_SUCCESS_CODE;
if (argc > arg_start) {
/* Process command line arguments */
for (int i = arg_start; i < argc; i++) {
int ret = process_run(argv[i], ¤t_num, is_first);
if (ret != 0) {
exit_code = ret;
break;
}
is_first = 0;
}
} else {
/* Read from stdin */
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
/* Remove trailing newline */
if (read > 0 && line[read - 1] == '\n') {
line[read - 1] = '\0';
}
int ret = process_run(line, ¤t_num, is_first);
if (ret != 0) {
exit_code = ret;
break;
}
is_first = 0;
}
free(line);
}
return exit_code;
}