8ecfe0dc |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: genprimes.c,v 1.1 1999/11/19 13:19:37 mdw Exp $ |
4 | * |
5 | * Generate prime number table |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: genprimes.c,v $ |
33 | * Revision 1.1 1999/11/19 13:19:37 mdw |
34 | * Generate small primes table. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <ctype.h> |
41 | #include <errno.h> |
42 | #include <stdio.h> |
43 | #include <stdlib.h> |
44 | #include <string.h> |
45 | |
46 | #include <mLib/darray.h> |
47 | #include <mLib/dstr.h> |
48 | #include <mLib/mdwopt.h> |
49 | #include <mLib/quis.h> |
50 | #include <mLib/report.h> |
51 | |
52 | /*----- Data structures ---------------------------------------------------*/ |
53 | |
54 | DA_DECL(intv, int); |
55 | |
56 | /*----- Main code ---------------------------------------------------------*/ |
57 | |
58 | int main(int argc, char *argv[]) |
59 | { |
60 | int p_max = 0, p_n = 0; |
61 | char *type = "unsigned int"; |
62 | char *header = "ptab.h"; |
63 | char *source = "ptab.c"; |
64 | char *name = "ptab"; |
65 | intv p = DA_INIT; |
66 | int i; |
67 | |
68 | ego(argv[0]); |
69 | |
70 | for (;;) { |
71 | int i = getopt(argc, argv, "h:c:i:n:m:t:"); |
72 | if (i < 0) |
73 | break; |
74 | switch (i) { |
75 | case 'h': |
76 | header = optarg; |
77 | break; |
78 | case 'c': |
79 | source = optarg; |
80 | break; |
81 | case 'i': |
82 | name = optarg; |
83 | break; |
84 | case 'n': |
85 | p_max = 0; |
86 | p_n = atoi(optarg); |
87 | break; |
88 | case 'm': |
89 | p_n = 0; |
90 | p_max = atoi(optarg); |
91 | break; |
92 | case 't': |
93 | type = optarg; |
94 | break; |
95 | default: |
96 | pquis(stderr, "Usage: $ [-n nprimes] [-m maxprime] [-t type]\n"); |
97 | exit(EXIT_FAILURE); |
98 | } |
99 | } |
100 | |
101 | if (!p_max && !p_n) |
102 | die(EXIT_FAILURE, "bad arguments to `-n' or `-m'"); |
103 | |
104 | if (p_max >= 2) |
105 | DA_PUSH(&p, 2); |
106 | for (i = 3; (!p_max && !p_n) || |
107 | (p_n && DA_LEN(&p) < p_n) || |
108 | (p_max && i <= p_max); |
109 | i += 2) { |
110 | int j; |
111 | for (j = 0; j < DA_LEN(&p); j++) { |
112 | if (i % DA(&p)[j] == 0) |
113 | goto composite; |
114 | } |
115 | DA_PUSH(&p, i); |
116 | composite:; |
117 | } |
118 | |
119 | { |
120 | FILE *fp = fopen(header, "w"); |
121 | dstr d = DSTR_INIT; |
122 | char *q; |
123 | if (!fp) |
124 | die(EXIT_FAILURE, "couldn't write `%s': %s", header, strerror(errno)); |
125 | for (q = header; *q; q++) { |
126 | int ch = (unsigned char)*q; |
127 | if (isalnum(ch)) |
128 | ch = toupper(ch); |
129 | else |
130 | ch = '_'; |
131 | DPUTC(&d, ch); |
132 | } |
133 | DPUTZ(&d); |
134 | fprintf(fp, "\ |
135 | /* -*-c-*-\n\ |
136 | *\n\ |
137 | * Table of small prime numbers [generated]\n\ |
138 | */\n\ |
139 | \n\ |
140 | #ifndef %s\n\ |
141 | #define %s\n\ |
142 | \n\ |
143 | #define NPRIME %lu\n\ |
144 | #define MAXPRIME %i\n\ |
145 | \n\ |
146 | extern %s %s[];\n\ |
147 | \n\ |
148 | #endif\n\ |
149 | ", |
150 | d.buf, d.buf, |
151 | (unsigned long)DA_LEN(&p), |
152 | DA(&p)[DA_LEN(&p) - 1], |
153 | type, name); |
154 | dstr_destroy(&d); |
155 | if (fclose(fp) == EOF) { |
156 | remove(header); |
157 | die(EXIT_FAILURE, "error writing `%s': %s", header, strerror(errno)); |
158 | } |
159 | } |
160 | |
161 | { |
162 | FILE *fp = fopen(source, "w"); |
163 | int i; |
164 | if (!fp) |
165 | die(EXIT_FAILURE, "couldn't write `%s': %s", source, strerror(errno)); |
166 | fprintf(fp, "\ |
167 | /* -*-c-*-\n\ |
168 | *\n\ |
169 | * Table of small prime numbers [generated]\n\ |
170 | */\n\ |
171 | \n\ |
172 | #include \"%s\"\n\ |
173 | \n\ |
174 | %s %s[] = {", |
175 | header, type, name); |
176 | for (i = 0; i < DA_LEN(&p); i++) { |
177 | if (i % 8 == 0) |
178 | fputs("\n ", fp); |
179 | fprintf(fp, "%5i, ", DA(&p)[i]); |
180 | } |
181 | fputs("\n\ |
182 | };\n\ |
183 | ", fp); |
184 | if (fclose(fp) == EOF) { |
185 | remove(source); |
186 | die(EXIT_FAILURE, "error writing `%s': %s", source, strerror(errno)); |
187 | } |
188 | } |
189 | |
190 | return (0); |
191 | } |
192 | |
193 | /*----- That's all, folks -------------------------------------------------*/ |