1111 (at your option) any later version. See <https://www.gnu.org/licenses/>.
1212*/
1313
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>
2814#include <stdlib.h>
15+ #include <string.h>
2916#include "thread_support.h"
3017#include "fmpz.h"
3118#include "fmpz_factor.h"
3219#include "fmpz_vec.h"
3320#include "qsieve.h"
3421
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-
4022int compare_facs (const void * a , const void * b )
4123{
4224 fmpz * x = (fmpz * ) a ;
@@ -63,11 +45,6 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
6345 fmpz_t temp , temp2 , X , Y ;
6446 slong num_facs ;
6547 fmpz * facs ;
66- #if (defined(__WIN32 ) && !defined(__CYGWIN__ )) || defined(_MSC_VER )
67- char temp_path [MAX_PATH ];
68- #else
69- int fd ;
70- #endif
7148
7249 if (fmpz_sgn (n ) < 0 )
7350 {
@@ -214,41 +191,8 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
214191 pthread_mutex_init (& qs_inf -> mutex , NULL );
215192#endif
216193
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 );
252196
253197 for (j = qs_inf -> small_primes ; j < qs_inf -> num_primes ; j ++ )
254198 {
@@ -290,9 +234,7 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
290234 {
291235 int ok ;
292236
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 );
296238
297239 ok = qsieve_process_relation (qs_inf );
298240
@@ -406,9 +348,7 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
406348
407349 _fmpz_vec_clear (facs , 100 );
408350
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 );
412352 qs_inf -> num_primes = num_primes ; /* linear algebra adjusts this */
413353 goto more_primes ; /* factoring failed, may need more primes */
414354 }
@@ -486,11 +426,8 @@ void qsieve_factor(fmpz_factor_t factors, const fmpz_t n)
486426 flint_give_back_threads (qs_inf -> handles , qs_inf -> num_handles );
487427
488428 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 );
494431 qsieve_clear (qs_inf );
495432 qsieve_linalg_clear (qs_inf );
496433 qsieve_poly_clear (qs_inf );
0 commit comments