1 | /*
|
---|
2 | * Copyright (c) 2014 Vojtech Horky
|
---|
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
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #pragma warning(push, 0)
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <ctype.h>
|
---|
33 | #include <string.h>
|
---|
34 | #pragma warning(pop)
|
---|
35 |
|
---|
36 |
|
---|
37 | #define MAX_IDENTIFIER_LENGTH 256
|
---|
38 |
|
---|
39 | static int counter = 0;
|
---|
40 |
|
---|
41 | static void print_numbered_identifier(int value, FILE *output) {
|
---|
42 | fprintf(output, "pcut_item_%d", value);
|
---|
43 | }
|
---|
44 |
|
---|
45 | static void print_numbered_identifier2(int value, FILE *output) {
|
---|
46 | fprintf(output, "pcut_item2_%d", value);
|
---|
47 | }
|
---|
48 |
|
---|
49 | static void print_numbered_identifier3(int value, FILE *output) {
|
---|
50 | fprintf(output, "pcut_item3_%d", value);
|
---|
51 | }
|
---|
52 |
|
---|
53 | typedef struct {
|
---|
54 | char name[MAX_IDENTIFIER_LENGTH];
|
---|
55 | size_t length;
|
---|
56 | } identifier_t;
|
---|
57 |
|
---|
58 | static void identifier_init(identifier_t *identifier) {
|
---|
59 | identifier->name[0] = 0;
|
---|
60 | identifier->length = 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static void identifier_add_char(identifier_t *identifier, char c) {
|
---|
64 | if (identifier->length + 1 >= MAX_IDENTIFIER_LENGTH) {
|
---|
65 | fprintf(stderr, "Identifier %s is too long, aborting!\n", identifier->name);
|
---|
66 | exit(1);
|
---|
67 | }
|
---|
68 |
|
---|
69 | identifier->name[identifier->length] = c;
|
---|
70 | identifier->length++;
|
---|
71 | identifier->name[identifier->length] = 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void identifier_print_or_expand(identifier_t *identifier, FILE *output) {
|
---|
75 | const char *name = identifier->name;
|
---|
76 | if (strcmp(name, "PCUT_ITEM_NAME") == 0) {
|
---|
77 | print_numbered_identifier(counter, output);
|
---|
78 | } else if (strcmp(name, "PCUT_ITEM_NAME_PREV") == 0) {
|
---|
79 | print_numbered_identifier(counter - 1, output);
|
---|
80 | } else if (strcmp(name, "PCUT_ITEM_COUNTER_INCREMENT") == 0) {
|
---|
81 | counter++;
|
---|
82 | } else if (strcmp(name, "PCUT_ITEM2_NAME") == 0) {
|
---|
83 | print_numbered_identifier2(counter, output);
|
---|
84 | } else if (strcmp(name, "PCUT_ITEM3_NAME") == 0) {
|
---|
85 | print_numbered_identifier3(counter, output);
|
---|
86 | } else {
|
---|
87 | fprintf(output, "%s", name);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | static int is_identifier_char(int c, int inside_identifier) {
|
---|
92 | return isalpha(c) || (c == '_')
|
---|
93 | || (inside_identifier && isdigit(c));
|
---|
94 | }
|
---|
95 |
|
---|
96 | int main(int argc, char *argv[]) {
|
---|
97 | FILE *input = stdin;
|
---|
98 | FILE *output = stdout;
|
---|
99 |
|
---|
100 | int last_char_was_identifier = 0;
|
---|
101 | identifier_t last_identifier;
|
---|
102 |
|
---|
103 | /* Unused parameters. */
|
---|
104 | (void) argc;
|
---|
105 | (void) argv;
|
---|
106 |
|
---|
107 | while (1) {
|
---|
108 | int current_char_denotes_identifier;
|
---|
109 |
|
---|
110 | int current_char = fgetc(input);
|
---|
111 | if (current_char == EOF) {
|
---|
112 | break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | current_char_denotes_identifier = is_identifier_char(current_char, last_char_was_identifier);
|
---|
116 | if (current_char_denotes_identifier) {
|
---|
117 | if (!last_char_was_identifier) {
|
---|
118 | identifier_init(&last_identifier);
|
---|
119 | }
|
---|
120 | identifier_add_char(&last_identifier, current_char);
|
---|
121 | last_char_was_identifier = 1;
|
---|
122 | } else {
|
---|
123 | if (last_char_was_identifier) {
|
---|
124 | identifier_print_or_expand(&last_identifier, output);
|
---|
125 | }
|
---|
126 | fprintf(stdout, "%c", current_char);
|
---|
127 | last_char_was_identifier = 0;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (last_char_was_identifier) {
|
---|
132 | identifier_print_or_expand(&last_identifier, output);
|
---|
133 | }
|
---|
134 | fprintf(output, "\n");
|
---|
135 |
|
---|
136 | return 0;
|
---|
137 | }
|
---|