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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15d0046 was 134ac5d, checked in by Vojtech Horky <vojtechhorky@…>, 11 years ago

Update PCUT to newest version

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