annotate Meerwald/gen_cox_sig.c @ 15:ad1d224896c5

fix scanf() warning
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Mon, 30 Jun 2008 20:59:01 +0200
parents f83ef905a63d
children 4987db85cfae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 fprintf(stderr, "usage: %s [-a n] [-d n] [-m n] [-n n] [-o file] [-s file] [-S n]\n\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 fprintf(stderr, "\t-a n\t\talpha factor (default 0.3)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 fprintf(stderr, "\t-d n\t\tdeviation (default 1.0)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 fprintf(stderr, "\t-m n \t\tmean value (default 0.0)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 fprintf(stderr, "\t-n n\t\twatermark length (default 100)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "\t-o file\t\toutput file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 fprintf(stderr, "\t-S n\t\tseed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 FILE *sig = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 char signature_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 int c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 int n = 100;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 int s = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 double a = 0.3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 double m = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 double d = 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 while ((c = getopt(argc, argv, "a:d:h?m:n:o:s:S:")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 switch (c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 case 'a':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 a = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 if (a <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 case 'd':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 d = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 if (d <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 fprintf(stderr, "%s: deviation %f out of range\n", progname, d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 case 'm':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 m = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 case 'n':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 n = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 if (n < 1 || n > 32000) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 if ((out = fopen(optarg, "w")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 case 's':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 if ((sig = fopen(optarg, "r")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 strcpy(signature_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 case 'S':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 s = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 if (argc > 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 if (sig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 char line[32];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 fgets(line, sizeof(line), sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 if (strspn(line, "CXSG") >= 4) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 if (n == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 fscanf(sig, "%d\n", &n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 if (a == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 fscanf(sig, "%lf\n", &a);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 fscanf(sig, "%*lf\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 if (m == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 fscanf(sig, "%lf\n", &m);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 fscanf(sig, "%*lf\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 if (d == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 fscanf(sig, "%lf\n", &d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 fscanf(sig, "%*lf\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 }
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
117 fclose(sig);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 if (s)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 srandom(s);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 srandom(time(NULL) * getpid());
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 fprintf(out, "CXSG\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 fprintf(out, "%d\n", n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 fprintf(out, "%f\n", a);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 fprintf(out, "%f\n", m);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 fprintf(out, "%f\n", d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 n >>= 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 while (n > 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 double x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 double x1, x2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 /*
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 * Algorithm P (Polar method for normal deviates),
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 do {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 x = x1 * x1 + x2 * x2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 } while (x >= 1.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 x1 *= sqrt((-2.0) * log(x) / x);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 x2 *= sqrt((-2.0) * log(x) / x);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 fprintf(out, "%f\n", m + d * x1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 fprintf(out, "%f\n", m + d * x2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 n--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.