/
parse.c
901 lines (804 loc) · 27.3 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*
This file used to be part of mfaktc.
Copyright (C) 2009, 2010, 2011 Oliver Weihe (o.weihe@t-online.de)
This file has been written by Luigi Morelli (L.Morelli@mclink.it) *1
mfaktc is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mfaktc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mfaktc. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*1 Luigi initially wrote the two functions get_next_assignment() and
clear_assignment() after we (Luigi and myself (Oliver)) have discussed the
interface. Luigi was so nice to write those functions so I had time to focus
on other parts, this made early (mfaktc 0.07) worktodo parsing posible.
For mfakc 0.15 I've completly rewritten those two functions. The rewritten
functions should be more robust against malformed input. Grab the sources of
mfaktc 0.07-0.14 to see Luigis code.
*/
//Christenson, 2011, needed to expand the functionality, and strongly
// disliked not running everything through the same parse function.
// after a first re-write with a do-everything parse function that was
// too complicated for its own good, a second re-write centers around
// a new function, parse_line, that takes in a line of worktodo and returns
// a structured result.
// the value of this is that it can be re-targeted to some other forms of
// numbers besides Mersenne exponents.....
/* Bill stole it and modified it for CUDALucas' purposes... :) */
/************************************************************************************************************
* Input/output file function library *
* *
* return codes: *
* 0 - OK *
* 1 - get_next_assignment : cannot open file *
* 2 - get_next_assignment : no valid assignment found *
* 3 - clear_assignment : cannot open file <filename> *
* 4 - clear_assignment : cannot open file "__worktodo__.tmp" *
* 5 - clear_assignment : assignment not found *
* 6 - clear_assignment : cannot rename temporary workfile to regular workfile *
************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#define MAX_LINE_LENGTH 131
#include "parse.h"
#ifndef _MSC_VER /* See next comment */
#define _fopen fopen
#define strcopy strncpy
#define _sprintf sprintf
#include <unistd.h>
#include <sched.h>
#define MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
static void Sleep(unsigned int ms) {
struct timespec ts;
ts.tv_sec = (time_t) ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
#else
#include <io.h>
#undef open
#undef close
#define open _open
#define close _close
#define sched_yield SwitchToThread
#define MODE _S_IREAD | _S_IWRITE
#define strncasecmp _strnicmp
/* Everything from here to the next include is to make MSVS happy. */
// #define sscanf sscanf_s /* This only works for scanning numbers, or strings with a defined length (e.g. "%131s") */
void strcopy(char* dest, char* src, size_t n)
{
strncpy_s(dest, MAX_LINE_LENGTH+1, src, n);
}
FILE* _fopen(const char* path, const char* mode)
{
FILE* stream;
errno_t err = fopen_s(&stream, path, mode);
if(err) return NULL;
else return stream;
}
void _sprintf(char* buf, char* frmt, const char* string)
{ // only used in filelocking code
sprintf_s(buf, 251, frmt, string);
}
int gettimeofday(struct timeval *tv, struct timezone *unused)
/*
This is based on a code sniplet from Kevin (kjaget on www.mersenneforum.org)
This doesn't act like a real gettimeofday(). It has a wrong offset but this is
OK since CUDALucas only uses this to measure the time difference between two calls
of gettimeofday().
*/
{
static LARGE_INTEGER frequency;
static int frequency_flag = 0;
if(!frequency_flag)
{
QueryPerformanceFrequency(&frequency);
frequency_flag = 1;
}
if(tv)
{
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
tv->tv_sec = (long) (counter.QuadPart / frequency.QuadPart);
tv->tv_usec = (long)((counter.QuadPart % frequency.QuadPart) / ((double)frequency.QuadPart / 1000000.0));
}
return 0;
}
#endif
/***********************************************************************************************************/
int file_exists(char* name) {
if (name && name[0]) { /* Check for null string */
FILE* stream;
if ((stream = _fopen(name, "r"))) {
fclose(stream);
return 1;
} else
return 0;
} else
return 0;
}
int isprime(unsigned int n)
/*
returns
0 if n is composite
1 if n is prime
*/
{
unsigned int i;
if (n <= 1)
return 0;
if (n > 2 && n % 2 == 0)
return 0;
i = 3;
while (i * i <= n && i < 0x10000) {
if (n % i == 0)
return 0;
i += 2;
}
return 1;
}
int valid_assignment(int exp, int fftlen)
/*
returns 1 if the assignment is within the supported bounds of CUDALucas,
0 otherwise.
*/
{
int ret = 1;
// Perhaps add a largest exponent?
if (exp < 8000) {
ret = 0;
fprintf(stderr, "Warning: exponents < 8000 are not supported!\n");
}
if (!isprime(exp)) {
ret = 0;
fprintf(stderr, "Warning: exponent is not prime!\n");
}
if (fftlen % (1024)) {
ret = 0;
fprintf(stderr,
"Warning: FFT length %d is invalid, it must be a multiple of 1024. See CUDALucas.ini for more details about good lengths.\n",
fftlen);
}
// This doesn't guarantee that it *is* valid, but it will catch horribly bad lengths.
// (To do more checking, we'd need access the "threads" variable from CUDALucas.cu.)
return ret;
}
// Should we include FFT length in this?
// note: parse_worktodo_line() is a function that
// returns the text of the line, the assignment data structure, and a success code.
enum PARSE_WARNINGS parse_worktodo_line(FILE *f_in, struct ASSIGNMENT *assignment, LINE_BUFFER *linecopy, char * *endptr)
/*
input
f_in: an open file from where data is read
output
assignment: structure of line, with any assignment if found
linecopy: a copy of the last read line
endptr: the end of data
*/
{
#ifdef EBUG
printf("Entered p_w_l\n");
#endif
char line[MAX_LINE_LENGTH + 1], *ptr, *ptr_start, *ptr_end, *ptr2;
/* See below about LONG_LINE */
//int c; // extended char pulled from stream;
int scanpos;
int number_of_commas; // Originally this was unsigned, causing lots of problems in my outer for(), and it took me a while to track down :P
// (It didn't help that I was printf'ing '%d' for an unsigned int, thus masking the problem to look correct. :P)
int comment_on_line = 0; // Set to the delimiting char if the line has a comment following the assignment.
long proposed_exponent;
unsigned long proposed_fftlen;
int count_numerical_field = 0;
assignment->fft_length = 0;
assignment->exponent = 0;
assignment->tf_depth = 0;
assignment->ll_saved = 0;
assignment->hex_key[0] = 0;
assignment->type = NONE;
if (NULL == fgets(line, MAX_LINE_LENGTH + 1, f_in)) {
return END_OF_FILE;
}
if (linecopy != NULL) { // maybe it wasn't needed....
strcopy(*linecopy, line, MAX_LINE_LENGTH + 1); // this is what was read...
if (NULL != strchr(*linecopy, '\n'))
*strchr(*linecopy, '\n') = '\0';
}
if ((strlen(line) == MAX_LINE_LENGTH) && (!feof(f_in)) && (line[strlen(line) - 1] != '\n')) // long lines disallowed,
{
return LONG_LINE;
// I see no reason to go to all of the following fuss
/*reason = LONG_LINE;
do
{
c = fgetc(f_in);
if ((EOF == c) ||(iscntrl(c))) // found end of line
break;
}
while(1); */
}
if (linecopy != NULL)
*endptr = *linecopy; // by default, non-significant content is whole line
#ifdef EBUG
printf(" Line: %s", line);
#endif
ptr = line;
while (('\0' != ptr[0]) && isspace(ptr[0])) // skip leading spaces
ptr++;
if ('\0' == ptr[0]) // blank line...
return BLANK_LINE;
if (('\\' == ptr[0]) && ('\\' == ptr[1]))
return NONBLANK_LINE; // it's a comment, so ignore....don't care about long lines either..
if (('/' == ptr[0]) && ('/' == ptr[1]))
return NONBLANK_LINE; // it's a comment, so ignore....don't care about long lines either..
if (('#' == ptr[0]))
return NONBLANK_LINE; // it's a comment, so ignore....don't care about long lines either..
if (strncasecmp("Pfactor=", ptr, 8) == 0)
assignment->type = PFACTOR;
if (strncasecmp("Pminus1=", ptr, 8) == 0)
assignment->type = PMINUS1;
if (assignment->type == NONE) // does the line start with "Pfactor=" or "Pminus1="?
// (case-insensitive)
return NO_TEST_EQUAL;
ptr = 1 + strchr(ptr, '='); // don't rescan..
while (('\0' != ptr[0]) && isspace(ptr[0])) // ignore blanks...
ptr++;
number_of_commas = 0;
for (scanpos = 0; scanpos < strlen(ptr); scanpos++) {
if (ptr[scanpos] == ',')
number_of_commas++; // count the number of ',' in the line
if ((ptr[scanpos] == '\\') && (ptr[scanpos + 1] == '\\')) {
comment_on_line = '\\';
break; // comment delimiter
}
if ((ptr[scanpos] == '/') && (ptr[scanpos + 1] == '/')) {
comment_on_line = '/';
break; // //comment delimiter
}
if (ptr[scanpos] == '#') {
comment_on_line = '#';
break; // comment delimiter
}
}
#ifdef EBUG
printf(" Scanned %d commas\n", number_of_commas);
#endif
// must have less than 8 commas...
// possible fields are:
// Pfactor=key,a,b,exp,c,tf,ll_saved,fft
// Pminus1=key,a,b,exp,c,b1,b2,tf
if (number_of_commas > 7)
return INVALID_FORMAT;
for (; number_of_commas >= 0; number_of_commas--) {
// i is number of commas ahead of ptr (or, there's one more field than commas, so iterate n+1 times)
while (isspace(ptr[0])) // ignore blanks...
ptr++;
if (number_of_commas > 0) // then there's at least one comma left
ptr_end = strchr(ptr, ','); // guaranteed that strchr isn't null because of the if
else if (comment_on_line) // no more commas, but there is a comment
ptr_end = strchr(ptr, comment_on_line); // (see declaration of c_on_l or lines 285,289,293 for explanation)
else { // no commas or comments
ptr_end = strchr(ptr, '\n');
if (ptr_end == NULL)
ptr_end = strchr(ptr, '\0');
}
#ifdef EBUG
printf(" In main for() loop, %d commas left\n", number_of_commas);
#endif
for (ptr_start = ptr; ptr_start < ptr_end; ptr_start++) {
#ifdef EBUG
printf(" Looping on chars, ptr_start = %c\n", *ptr_start);
#endif
if (('A' <= *ptr_start && *ptr_start <= 'F') || ('a' <= *ptr_start && *ptr_start <= 'f')) {
// we have some sort of hex assignment key, or "N/A". Either way, it's an AID of some sort.
#ifdef EBUG
printf(" Branched on AID, trigger is %c\n", *ptr_start);
#endif
strcopy(assignment->hex_key, ptr, (ptr_end - ptr));
assignment->hex_key[ptr_end - ptr] = '\0'; // null-terminate key
#ifdef EBUG
printf(" Key is '%s'\n", assignment->hex_key);
#endif
goto outer_continue;
} else if (*ptr_start == 'k' || *ptr_start == 'K') {
// we've found a fft length field.
#ifdef EBUG
printf(" Branched on FFT-K\n");
#endif
errno = 0;
proposed_fftlen = strtoul(ptr, &ptr2, 10) * 1024; // Don't forget the K ;)
if (ptr == ptr2 || ptr2 < ptr_start) // the second condition disallows space between the num and K, so "1444 K" will fail,
return INVALID_FORMAT; // but it also disallows "1rrrK", so I think it's worth it.
if (errno != 0 || proposed_fftlen > INT_MAX)
return INVALID_DATA;
assignment->fft_length = proposed_fftlen;
goto outer_continue;
} else if (*ptr_start == 'm' || *ptr_start == 'M') {
// we've found a fft length field.
#ifdef EBUG
printf(" Branched on FFT-M\n");
#endif
errno = 0;
proposed_fftlen = strtoul(ptr, &ptr2, 10) * 1024 * 1024; // Don't forget the M ;)
if (ptr == ptr2 || ptr2 < ptr_start) // the second condition disallows space between the num and K, so "1444 M" will fail,
return INVALID_FORMAT; // but it also disallows "1rrrK", so I think it's worth it.
if (errno != 0 || proposed_fftlen > INT_MAX)
return INVALID_DATA;
assignment->fft_length = proposed_fftlen;
goto outer_continue;
} else { // Not special, so we must continue checking the rest of the chars in the field
continue;
}
} // end inner for()
// Now we know there's nothing special about this field, so
// we must assume it's the exponent, except to assume that the largest number
// read in is the exponent (to filter out TF lim or P-1 bool)
#ifdef EBUG
printf(" Branched on default\n");
#endif
ptr_start = ptr; /* Nothing special, so reset ptr_start to the start */
if ('M' == *ptr_start) // M means Mersenne exponent...
ptr_start++;
errno = 0;
proposed_exponent = strtol(ptr_start, &ptr2, 10);
if (ptr_start == ptr2)
return INVALID_FORMAT; // no conversion
if (errno != 0 || proposed_exponent > INT_MAX) {
printf("Input too large. Max exponent/b1/b2 = %d", INT_MAX);
return INVALID_DATA;
}
#ifdef EBUG
printf(" 'Expo' conversion is %ld\n", proposed_exponent);
#endif
count_numerical_field++;
switch (count_numerical_field) {
case 1:
if (proposed_exponent != 1) {
printf("First value should be 1\n");
return INVALID_DATA;
}
break;
case 2:
if (proposed_exponent != 2) {
printf("Second value should be 2\n");
return INVALID_DATA;
}
break;
case 3:
if (proposed_exponent > (unsigned) assignment->exponent)
assignment->exponent = (int) proposed_exponent;
else {
printf("Invalid Exponent\n");
return INVALID_DATA;
}
break;
case 4:
if (proposed_exponent != -1) {
printf("Should be -1\n");
return INVALID_DATA;
}
break;
case 5:
if (proposed_exponent > 0)
if (assignment->type == PFACTOR)
assignment->tf_depth = (int) proposed_exponent;
else if (assignment->type == PMINUS1)
assignment->b1 = (int) proposed_exponent;
else {
return INVALID_DATA;
}
else {
return INVALID_DATA;
}
break;
case 6:
if (proposed_exponent > 0)
if (assignment->type == PFACTOR)
assignment->ll_saved = (int) proposed_exponent;
else if (assignment->type == PMINUS1)
assignment->b2 = (int) proposed_exponent;
else {
return INVALID_DATA;
}
else {
return INVALID_DATA;
}
break;
case 7:
if (proposed_exponent > 0 && assignment->type == PMINUS1)
assignment->tf_depth = (int) proposed_exponent;
else {
return INVALID_DATA;
}
break;
default:
return INVALID_DATA;
}
outer_continue: /* One nice feature Python has is putting "else"s on loops, only to be executed when NOT "break"-ed from.
That's exactly what I'm duplicating here with the inner loop and default "expo" branching. */
ptr = 1 + ptr_end; // Reset for the next field (*ptr == '\n' || *ptr == comment-delimiter when we're done)
} // end outer for()
// now we've looped over all fields in worktodo line
ptr = ptr_end;
#ifdef EBUG
printf(" Left for()\n");
#endif
if (*ptr == '\n' || *ptr == '\0') // no comment (lol)
assignment->comment[0] = '\0';
else if (comment_on_line) {
// Don't include delimters in actual comment
if (*ptr == '/' || *ptr == '\\')
ptr += 2;
else if (*ptr == '#')
ptr++;
else {
fprintf(stderr, "Wow, something's screwed up. Please file as detailed a bug report as possible.\n\n");
exit(7);
}
while (('\0' != ptr[0]) && isspace(ptr[0])) // ignore blanks...
ptr++;
if (*ptr != '\0') {
if (NULL != strchr(ptr, '\n')) // kill off any trailing newlines...
*strchr(ptr, '\n') = '\0';
strcopy(assignment->comment, ptr, 1 + strchr(ptr, '\0') - ptr);
} else
assignment->comment[0] = '\0';
} else /* No comment on line, but no terminating null or newline? */
{
fprintf(stderr, "Wow, something's way screwed up. Please file as detailed a bug report as possible.\n\n");
exit(77);
}
if (linecopy != NULL)
*endptr = *linecopy + (ptr_end - line);
if (assignment->exponent < 100 || (0 < assignment->fft_length && assignment->fft_length < 4096)) // sanity check
return INVALID_DATA;
return NO_WARNING;
}
/************************************************************************************************************
* Function name : get_next_assignment *
* *
* INPUT : char *filename *
* int *exponent *
* int* fft_length *
* char *hex_key[132]; *
* OUTPUT : *
* *
* 0 - OK *
* 1 - get_next_assignment : cannot open file *
* 2 - get_next_assignment : no valid assignment found *
************************************************************************************************************/
enum ASSIGNMENT_ERRORS get_next_assignment(char *filename, int *exponent, int* fft_length, int* tf_depth, int* ll_saved, LINE_BUFFER *key,
int* b1, int* b2) {
struct ASSIGNMENT assignment;
FILE *f_in;
enum PARSE_WARNINGS value;
char *tail;
LINE_BUFFER line;
unsigned int linecount = 0;
#ifdef EBUG
printf("Starting GNA. Called with *fft = %d.\n", *fft_length);
#endif
assignment.fft_length = 0;
f_in = _fopen(filename, "r");
if (NULL == f_in) {
// printf("Can't open workfile %s in %s\n", filename, getcwd(line,sizeof(LINE_BUFFER)) );
fprintf(stderr, "Can't open workfile %s\n\n", filename);
return CANT_OPEN_FILE; // nothing to open...
}
do {
linecount++;
value = parse_worktodo_line(f_in, &assignment, &line, &tail);
if ((BLANK_LINE == value) || (NONBLANK_LINE == value))
continue;
if (NO_WARNING == value) {
if (valid_assignment(assignment.exponent, assignment.fft_length))
break;
value = INVALID_DATA;
}
if (END_OF_FILE == value)
break;
fprintf(stderr, "Warning: ignoring line %u: \"%s\" in \"%s\". Reason: ", linecount, line, filename);
switch (value) {
case LONG_LINE:
fprintf(stderr, "line is too long.\n");
break;
case NO_TEST_EQUAL:
fprintf(stderr, "doesn't begin with Test= or DoubleCheck=.\n");
break;
case INVALID_FORMAT:
fprintf(stderr, "invalid format.\n");
break;
case INVALID_DATA:
fprintf(stderr, "invalid data.\n");
break;
default:
fprintf(stderr, "unknown error.\n");
break;
}
// if (LONG_LINE != value)
// return 2;
} while (1);
fclose(f_in);
if (NO_WARNING == value) {
*exponent = assignment.exponent;
if (assignment.fft_length > 0)
*fft_length = assignment.fft_length;
*tf_depth = assignment.tf_depth;
if (assignment.type == PFACTOR)
*ll_saved = assignment.ll_saved;
if (assignment.type == PMINUS1) {
*b1 = assignment.b1;
*b2 = assignment.b2;
}
#ifdef EBUG
printf("Struct fft is %d, *fft_length is %d\n", assignment.fft_length, *fft_length);
#endif
if (key != NULL)
strcopy(*key, assignment.hex_key, MAX_LINE_LENGTH + 1);
return OK;
} else
fprintf(stderr, "No valid assignment found.\n\n");
return VALID_ASSIGNMENT_NOT_FOUND;
}
/************************************************************************************************************
* Function name : clear_assignment *
* *
* INPUT : char *filename *
* int exponent *
* OUTPUT : *
* *
* 0 - OK *
* 3 - clear_assignment : cannot open file <filename> *
* 4 - clear_assignment : cannot open file "__worktodo__.tmp" *
* 5 - clear_assignment : assignment not found *
* 6 - clear_assignment : cannot rename temporary workfile to regular workfile *
* *
************************************************************************************************************/
enum ASSIGNMENT_ERRORS clear_assignment(char *filename, int exponent) {
int found = 0;
FILE *f_in, *f_out;
LINE_BUFFER line; // line buffer
char *tail = NULL; // points to tail material in line, if non-null
enum PARSE_WARNINGS value;
unsigned int line_to_drop = UINT_MAX;
unsigned int current_line;
struct ASSIGNMENT assignment; // the found assignment....
f_in = _fopen(filename, "r");
if (NULL == f_in) {
fprintf(stderr, "Can't open workfile %s\n\n", filename);
return CANT_OPEN_WORKFILE;
}
f_out = _fopen("__worktodo__.tmp", "w");
if (NULL == f_out) {
fclose(f_in);
fprintf(stderr, "Can't open tmp workfile\n\n");
return CANT_OPEN_TEMPFILE;
}
current_line = 0;
while (END_OF_FILE != (value = parse_worktodo_line(f_in, &assignment, &line, &tail))) {
current_line++;
#ifdef EBUG
printf("Current_line = %d\n", current_line);
#endif
if (NO_WARNING == value) {
if ((exponent == assignment.exponent)) // make final decision
{
if (line_to_drop > current_line)
line_to_drop = current_line;
#ifdef EBUG
printf("Made final decision. linetodrop = %d, current = %d\n", line_to_drop, current_line);
#endif
break;
} else {
line_to_drop = current_line + 1; // found different assignment, can drop no earlier than next line
}
} else if ((BLANK_LINE == value) && (UINT_MAX == line_to_drop))
line_to_drop = current_line + 1;
}
#ifdef EBUG
printf("Broken the first while loop, linetodrop = %d\n", line_to_drop);
#endif
errno = 0;
if (fseek(f_in, 0L, SEEK_SET)) {
fclose(f_in);
f_in = _fopen(filename, "r");
if (NULL == f_in) {
fclose(f_out);
fprintf(stderr, "Can't open workfile %s\n\n", filename);
return CANT_OPEN_WORKFILE;
}
}
found = 0;
current_line = 0;
while (END_OF_FILE != (value = parse_worktodo_line(f_in, &assignment, &line, &tail))) {
current_line++;
if ((NO_WARNING != value) || found) {
if ((found) || (current_line < line_to_drop))
fprintf(f_out, "%s\n", line);
} else // assignment on the line, so we may need to print it..
{
found = (exponent == assignment.exponent);
if (!found) {
fprintf(f_out, "%s\n", line);
} else // we have the assignment...
{
// Do nothing; we don't print this to the temp file, 'cause we're trying to delete it :)
}
}
} // while.....
fclose(f_in);
fclose(f_out);
if (!found) {
fprintf(stderr, "Assignment M%d completed but not found in workfile\n\n", exponent);
return ASSIGNMENT_NOT_FOUND;
}
if (remove(filename) != 0) {
fprintf(stderr, "Cannot remove old workfile\n\n");
return CANT_RENAME;
}
if (rename("__worktodo__.tmp", filename) != 0) {
fprintf(stderr, "Cannot move tmp workfile to regular workfile\n\n");
return CANT_RENAME;
}
return OK;
}
/*----------------------------------------------------------------------------------------------*/
/* These functions are slightly more modified from Oliver's equivalents. */
int IniGetInt(const char *inifile, const char *name, int *value, int dflt) {
FILE *in;
char buf[MAX_LINE_LENGTH + 1];
int found = 0;
in = _fopen(inifile, "r");
if (!in)
goto error;
while (fgets(buf, MAX_LINE_LENGTH, in) && !found) {
if (!strncmp(buf, name, strlen(name)) && buf[strlen(name)] == '=') {
if (sscanf_s(&(buf[strlen(name) + 1]), "%d", value) == 1)
found = 1;
}
}
fclose(in);
if (found)
return 1;
error: *value = dflt;
return 0;
}
int IniGetStr(const char *inifile, const char *name, char *string, const char* dflt) {
FILE *in;
char buf[MAX_LINE_LENGTH + 1];
int found = 0;
in = _fopen(inifile, "r");
if (!in)
goto error;
while (fgets(buf, MAX_LINE_LENGTH, in) && !found) {
if (!strncmp(buf, name, strlen(name)) && buf[strlen(name)] == '=') {
if (sscanf(&(buf[strlen(name) + 1]), "%131s", string) == 1)
found = 1; // CuLu's strs are 132 bytes
}
}
fclose(in);
if (found)
return 1;
error: strcopy(string, dflt, MAX_LINE_LENGTH);
return 0;
}
/*****************************************************************************/
/* mfakto's file locking code */
#define MAX_LOCKED_FILES 3
typedef struct _lockinfo {
int lockfd;
FILE * open_file;
char lock_filename[256];
} lockinfo;
static unsigned int num_locked_files = 0;
static lockinfo locked_files[MAX_LOCKED_FILES];
FILE *fopen_and_lock(const char *path, const char *mode) {
unsigned int i;
int lockfd;
FILE *f;
#ifdef EBUG
printf("\nlock() called on %s\n", path);
#endif
if (strlen(path) > 250) {
fprintf(stderr, "Cannot open %.250s: Name too long.\n", path);
return NULL;
}
if (num_locked_files >= MAX_LOCKED_FILES) {
fprintf(stderr, "Cannot open %.250s: Too many locked files.\n", path);
return NULL;
}
_sprintf(locked_files[num_locked_files].lock_filename, "%.250s.lck", path);
for (i = 0;;) {
if ((lockfd = open(locked_files[num_locked_files].lock_filename, O_EXCL | O_CREAT, MODE)) < 0) {
if (errno == EEXIST) {
if (i == 0)
fprintf(stderr, "%.250s is locked, waiting ...\n", path);
if (i < 1000)
i++; // slowly increase sleep time up to 1 sec
Sleep(i);
continue;
} else {
perror("Cannot open lockfile");
break;
}
}
break;
}
locked_files[num_locked_files].lockfd = lockfd;
if (lockfd > 0 && i > 0) {
printf("Locked %.250s\n", path);
}
f = fopen(path, mode);
if (f) {
locked_files[num_locked_files++].open_file = f;
} else {
if (close(locked_files[num_locked_files].lockfd) != 0)
perror("Failed to close lockfile");
if (remove(locked_files[num_locked_files].lock_filename) != 0)
perror("Failed to delete lockfile");
}
#ifdef EBUG
printf("successfully locked %s\n", path);
#endif
#ifdef TEST
while(1);
#endif
return f;
}
int unlock_and_fclose(FILE *f) {
unsigned int i, j;
int ret;
#ifdef EBUG
printf("unlock() called\n");
#endif
if (f == NULL)
return -1;
for (i = 0; i < num_locked_files; i++) {
if (locked_files[i].open_file == f) {
ret = fclose(f);
f = NULL;
if (close(locked_files[i].lockfd) != 0)
perror("Failed to close lockfile");
if (remove(locked_files[i].lock_filename) != 0)
perror("Failed to delete lockfile");
for (j = i + 1; j < num_locked_files; j++) {
locked_files[j - 1].lockfd = locked_files[j].lockfd;
locked_files[j - 1].open_file = locked_files[j].open_file;
strcpy(locked_files[j - 1].lock_filename, locked_files[j].lock_filename);
}
num_locked_files--;
break;
}
}
if (f) {
fprintf(stderr, "File was not locked!\n");
ret = fclose(f);
}
#ifdef EBUG
printf("successfully unlocked\n");
#endif
return ret;
}
void remove_spaces (char* str_trimmed, const char* str_untrimmed) {
while (*str_untrimmed != '\0') {
if(!isspace(*str_untrimmed)) {
*str_trimmed = *str_untrimmed;
str_trimmed++;
}
str_untrimmed++;
}
*str_trimmed = '\0';
}