11
11
(at your option) any later version. See <https://www.gnu.org/licenses/>.
12
12
*/
13
13
14
- #define _STDC_FORMAT_MACROS
15
-
16
- #ifdef __GNUC__
17
- # define strcpy __builtin_strcpy
18
- #else
19
- # include <math.h>
20
- #endif
21
-
22
- /* try to get fdopen, mkstemp declared */
23
- #if defined __STRICT_ANSI__
24
- #undef __STRICT_ANSI__
25
- #endif
26
-
27
- #include <stdio.h>
28
14
#include <stdlib.h>
15
+ #include <string.h>
29
16
#include "thread_support.h"
30
17
#include "fmpz.h"
31
18
#include "fmpz_factor.h"
32
19
#include "fmpz_vec.h"
33
20
#include "qsieve.h"
34
21
35
- /* Use Windows API for temporary files under MSVC and MinGW */
36
- #if (defined(__WIN32 ) && !defined(__CYGWIN__ )) || defined(_MSC_VER )
37
- #include <windows.h>
38
- #endif
39
-
40
22
int compare_facs (const void * a , const void * b )
41
23
{
42
24
fmpz * x = (fmpz * ) a ;
@@ -63,11 +45,6 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
63
45
fmpz_t temp , temp2 , X , Y ;
64
46
slong num_facs ;
65
47
fmpz * facs ;
66
- #if (defined(__WIN32 ) && !defined(__CYGWIN__ )) || defined(_MSC_VER )
67
- char temp_path [MAX_PATH ];
68
- #else
69
- int fd ;
70
- #endif
71
48
72
49
if (fmpz_sgn (n ) < 0 )
73
50
{
@@ -214,41 +191,8 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
214
191
pthread_mutex_init (& qs_inf -> mutex , NULL );
215
192
#endif
216
193
217
- #if (defined(__WIN32 ) && !defined(__CYGWIN__ )) || defined(_MSC_VER )
218
- if (GetTempPathA (MAX_PATH , temp_path ) == 0 )
219
- {
220
- flint_printf ("Exception (qsieve_factor). GetTempPathA() failed.\n" );
221
- flint_abort ();
222
- }
223
- /* uUnique = 0 means the we *do* want a unique filename (obviously!). */
224
- if (GetTempFileNameA (temp_path , "siq" , /*uUnique*/ 0 , qs_inf -> fname ) == 0 )
225
- {
226
- flint_printf ("Exception (qsieve_factor). GetTempFileNameA() failed.\n" );
227
- flint_abort ();
228
- }
229
- qs_inf -> siqs = (FLINT_FILE * ) fopen (qs_inf -> fname , "wb" );
230
- if (qs_inf -> siqs == NULL )
231
- flint_throw (FLINT_ERROR , "fopen failed\n" );
232
- #else
233
- strcpy (qs_inf -> fname , "/tmp/siqsXXXXXX" ); /* must be shorter than fname_alloc_size in init.c */
234
- fd = mkstemp (qs_inf -> fname );
235
- if (fd == -1 )
236
- flint_throw (FLINT_ERROR , "mkstemp failed\n" );
237
-
238
- qs_inf -> siqs = (FLINT_FILE * ) fdopen (fd , "wb" );
239
- if (qs_inf -> siqs == NULL )
240
- flint_throw (FLINT_ERROR , "fdopen failed\n" );
241
- #endif
242
- /*
243
- * The code here and in large_prime_variant.c opens and closes the file
244
- * qs_inf->fname in several different places. On Windows all file handles
245
- * need to be closed before the file can be removed in cleanup at function
246
- * exit. The invariant that needs to be preserved at each open/close is
247
- * that either
248
- * qs_inf->siqs is NULL and there are no open handles to the file,
249
- * or
250
- * qs_inf->siqs is not NULL and is the *only* open handle to the file.
251
- */
194
+ QS_SIQS_INIT (qs_inf );
195
+ QS_SIQS_FOPEN_W (qs_inf );
252
196
253
197
for (j = qs_inf -> small_primes ; j < qs_inf -> num_primes ; j ++ )
254
198
{
@@ -290,9 +234,7 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
290
234
{
291
235
int ok ;
292
236
293
- if (fclose ((FILE * ) qs_inf -> siqs ))
294
- flint_throw (FLINT_ERROR , "fclose fail\n" );
295
- qs_inf -> siqs = NULL ;
237
+ QS_SIQS_FCLOSE (qs_inf );
296
238
297
239
ok = qsieve_process_relation (qs_inf );
298
240
@@ -406,9 +348,7 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
406
348
407
349
_fmpz_vec_clear (facs , 100 );
408
350
409
- qs_inf -> siqs = (FLINT_FILE * ) fopen (qs_inf -> fname , "wb" );
410
- if (qs_inf -> siqs == NULL )
411
- flint_throw (FLINT_ERROR , "fopen fail\n" );
351
+ QS_SIQS_FOPEN_W (qs_inf );
412
352
qs_inf -> num_primes = num_primes ; /* linear algebra adjusts this */
413
353
goto more_primes ; /* factoring failed, may need more primes */
414
354
}
@@ -486,11 +426,8 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
486
426
flint_give_back_threads (qs_inf -> handles , qs_inf -> num_handles );
487
427
488
428
flint_free (sieve );
489
- if (qs_inf -> siqs != NULL && fclose ((FILE * ) qs_inf -> siqs ))
490
- flint_throw (FLINT_ERROR , "fclose fail\n" );
491
- if (remove (qs_inf -> fname )) {
492
- flint_throw (FLINT_ERROR , "remove fail\n" );
493
- }
429
+ QS_SIQS_FCLOSE (qs_inf );
430
+ QS_SIQS_CLEAR (qs_inf );
494
431
qsieve_clear (qs_inf );
495
432
qsieve_linalg_clear (qs_inf );
496
433
qsieve_poly_clear (qs_inf );
0 commit comments