1 | /* |
2 | * Legal Notice |
3 | * |
4 | * This document and associated source code (the "Work") is a part of a |
5 | * benchmark specification maintained by the TPC. |
6 | * |
7 | * The TPC reserves all right, title, and interest to the Work as provided |
8 | * under U.S. and international laws, including without limitation all patent |
9 | * and trademark rights therein. |
10 | * |
11 | * No Warranty |
12 | * |
13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION |
14 | * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE |
15 | * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER |
16 | * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, |
17 | * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, |
18 | * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR |
19 | * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF |
20 | * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. |
21 | * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, |
22 | * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT |
23 | * WITH REGARD TO THE WORK. |
24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO |
25 | * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE |
26 | * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS |
27 | * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, |
28 | * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, |
29 | * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT |
30 | * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD |
31 | * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. |
32 | * |
33 | * Contributors: |
34 | * Gradient Systems |
35 | */ |
36 | #include "config.h" |
37 | #include "porting.h" |
38 | #include <stdio.h> |
39 | #include <stdlib.h> |
40 | #include <ctype.h> |
41 | #include "decimal.h" |
42 | #include "date.h" |
43 | #include "genrand.h" |
44 | #include "dist.h" |
45 | |
46 | /* |
47 | * Routine: mk_sentence() |
48 | * Purpose: create a sample sentence |
49 | * Algorithm: |
50 | * Data Structures: |
51 | * |
52 | * Params: |
53 | * Returns: |
54 | * Called By: |
55 | * Calls: |
56 | * Assumptions: |
57 | * Side Effects: |
58 | * TODO: None |
59 | */ |
60 | static int used_space = 0; /* current length of the sentence being built */ |
61 | #define SPACE_INCREMENT 100 |
62 | |
63 | static char *mk_sentence(int stream) { |
64 | static char *verbiage = NULL; |
65 | static int allocated_space = 0; |
66 | int word_len; |
67 | char *syntax, *cp, *word = NULL, temp[2]; |
68 | |
69 | temp[1] = '\0'; |
70 | pick_distribution(&syntax, "sentences" , 1, 1, stream); |
71 | |
72 | for (cp = syntax; *cp; cp++) { |
73 | switch (*cp) { |
74 | case 'N': /* pick a noun */ |
75 | pick_distribution(&word, "nouns" , 1, 1, stream); |
76 | break; |
77 | case 'V': /* pick a verb */ |
78 | pick_distribution(&word, "verbs" , 1, 1, stream); |
79 | break; |
80 | case 'J': /* pick a adjective */ |
81 | pick_distribution(&word, "adjectives" , 1, 1, stream); |
82 | break; |
83 | case 'D': /* pick a adverb */ |
84 | pick_distribution(&word, "adverbs" , 1, 1, stream); |
85 | break; |
86 | case 'X': /* pick a auxiliary verb */ |
87 | pick_distribution(&word, "auxiliaries" , 1, 1, stream); |
88 | break; |
89 | case 'P': /* pick a preposition */ |
90 | pick_distribution(&word, "prepositions" , 1, 1, stream); |
91 | break; |
92 | case 'A': /* pick an article */ |
93 | pick_distribution(&word, "articles" , 1, 1, stream); |
94 | break; |
95 | case 'T': /* pick an terminator */ |
96 | pick_distribution(&word, "terminators" , 1, 1, stream); |
97 | break; |
98 | default: |
99 | temp[0] = *cp; |
100 | break; |
101 | } |
102 | |
103 | if (word == NULL) |
104 | word_len = 1; |
105 | else |
106 | word_len = strlen(word); |
107 | |
108 | if (used_space + word_len >= allocated_space) { |
109 | verbiage = (char *)realloc(verbiage, allocated_space + SPACE_INCREMENT); |
110 | MALLOC_CHECK(verbiage); |
111 | allocated_space += SPACE_INCREMENT; |
112 | } |
113 | |
114 | if (word == NULL) |
115 | strcpy(&verbiage[used_space], temp); |
116 | else |
117 | strcpy(&verbiage[used_space], word); |
118 | used_space += word_len; |
119 | word = NULL; |
120 | } |
121 | |
122 | return (verbiage); |
123 | } |
124 | |
125 | /* |
126 | * Routine: gen_text() |
127 | * Purpose: entry point for this module. Generate a truncated sentence in a |
128 | * given length range |
129 | * Algorithm: |
130 | * Data Structures: |
131 | * |
132 | * Params: |
133 | * Returns: |
134 | * Called By: |
135 | * Calls: |
136 | * Assumptions: |
137 | * Side Effects: |
138 | * TODO: None |
139 | */ |
140 | char *gen_text(char *dest, int min, int max, int stream) { |
141 | int target_len, generated_length, capitalize = 1; |
142 | char *s; |
143 | |
144 | used_space = 0; |
145 | genrand_integer(&target_len, DIST_UNIFORM, min, max, 0, stream); |
146 | if (dest) |
147 | *dest = '\0'; |
148 | else { |
149 | dest = (char *)malloc((max + 1) * sizeof(char)); |
150 | MALLOC_CHECK(dest); |
151 | } |
152 | |
153 | while (target_len > 0) { |
154 | used_space = 0; |
155 | s = mk_sentence(stream); |
156 | if (capitalize) |
157 | *s = toupper(*s); |
158 | generated_length = strlen(s); |
159 | capitalize = (s[generated_length - 1] == '.'); |
160 | if (target_len <= generated_length) |
161 | s[target_len] = '\0'; |
162 | strcat(dest, s); |
163 | target_len -= generated_length; |
164 | if (target_len > 0) { |
165 | strcat(dest, " " ); |
166 | target_len -= 1; |
167 | } |
168 | } |
169 | |
170 | return (dest); |
171 | } |
172 | |
173 | #ifdef TEST |
174 | #define DECLARER |
175 | #include "r_driver.h" |
176 | #include "r_params.h" |
177 | |
178 | typedef struct { |
179 | char *name; |
180 | } tdef; |
181 | /* tdef tdefs[] = {NULL}; */ |
182 | |
183 | option_t options[] = { |
184 | |
185 | {"DISTRIBUTIONS" , OPT_STR, 0, NULL, NULL, "tester_dist.idx" }, NULL}; |
186 | |
187 | char *params[2]; |
188 | |
189 | main() { |
190 | char test_dest[201]; |
191 | int i; |
192 | |
193 | init_params(); |
194 | |
195 | for (i = 0; i < 100; i++) { |
196 | gen_text(test_dest, 100, 200, 1); |
197 | printf("%s\n" , test_dest); |
198 | test_dest[0] = '\0'; |
199 | } |
200 | |
201 | return (0); |
202 | } |
203 | #endif |
204 | |