source: mainline/uspace/lib/pcut/src/preproc.c@ a05ec66

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a05ec66 was 76d0981d, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Use proper boolean constant in while loops.

  • Property mode set to 100644
File size: 4.1 KB
Line 
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#include <stdbool.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <ctype.h>
33#include <string.h>
34
35#define MAX_IDENTIFIER_LENGTH 256
36
37static int counter = 0;
38
39static void print_numbered_identifier(int value, FILE *output)
40{
41 fprintf(output, "pcut_item_%d", value);
42}
43
44static void print_numbered_identifier2(int value, FILE *output)
45{
46 fprintf(output, "pcut_item2_%d", value);
47}
48
49static void print_numbered_identifier3(int value, FILE *output)
50{
51 fprintf(output, "pcut_item3_%d", value);
52}
53
54typedef struct {
55 char name[MAX_IDENTIFIER_LENGTH];
56 size_t length;
57} identifier_t;
58
59static void identifier_init(identifier_t *identifier)
60{
61 identifier->name[0] = 0;
62 identifier->length = 0;
63}
64
65static void identifier_add_char(identifier_t *identifier, char c)
66{
67 if (identifier->length + 1 >= MAX_IDENTIFIER_LENGTH) {
68 fprintf(stderr, "Identifier %s is too long, aborting!\n", identifier->name);
69 exit(1);
70 }
71
72 identifier->name[identifier->length] = c;
73 identifier->length++;
74 identifier->name[identifier->length] = 0;
75}
76
77static void identifier_print_or_expand(identifier_t *identifier, FILE *output)
78{
79 const char *name = identifier->name;
80 if (strcmp(name, "PCUT_ITEM_NAME") == 0) {
81 print_numbered_identifier(counter, output);
82 } else if (strcmp(name, "PCUT_ITEM_NAME_PREV") == 0) {
83 print_numbered_identifier(counter - 1, output);
84 } else if (strcmp(name, "PCUT_ITEM_COUNTER_INCREMENT") == 0) {
85 counter++;
86 } else if (strcmp(name, "PCUT_ITEM2_NAME") == 0) {
87 print_numbered_identifier2(counter, output);
88 } else if (strcmp(name, "PCUT_ITEM3_NAME") == 0) {
89 print_numbered_identifier3(counter, output);
90 } else {
91 fprintf(output, "%s", name);
92 }
93}
94
95static int is_identifier_char(int c, int inside_identifier)
96{
97 return isalpha(c) || (c == '_') || (inside_identifier && isdigit(c));
98}
99
100int main(int argc, char *argv[])
101{
102 FILE *input = stdin;
103 FILE *output = stdout;
104
105 int last_char_was_identifier = 0;
106 identifier_t last_identifier;
107
108 /* Unused parameters. */
109 (void) argc;
110 (void) argv;
111
112 while (true) {
113 int current_char_denotes_identifier;
114
115 int current_char = fgetc(input);
116 if (current_char == EOF) {
117 break;
118 }
119
120 current_char_denotes_identifier = is_identifier_char(current_char, last_char_was_identifier);
121 if (current_char_denotes_identifier) {
122 if (!last_char_was_identifier) {
123 identifier_init(&last_identifier);
124 }
125 identifier_add_char(&last_identifier, current_char);
126 last_char_was_identifier = 1;
127 } else {
128 if (last_char_was_identifier) {
129 identifier_print_or_expand(&last_identifier, output);
130 }
131 fprintf(stdout, "%c", current_char);
132 last_char_was_identifier = 0;
133 }
134 }
135
136 if (last_char_was_identifier) {
137 identifier_print_or_expand(&last_identifier, output);
138 }
139 fprintf(output, "\n");
140
141 return 0;
142}
Note: See TracBrowser for help on using the repository browser.