1 /****************************************************************************/
2 /*                                                                          */
3 /*  This file is part of CONCORDE                                           */
4 /*                                                                          */
5 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
6 /*  Vasek Chvatal, and William Cook                                         */
7 /*                                                                          */
8 /*  Permission is granted for academic research use.  For other uses,       */
9 /*  contact the authors for licensing options.                              */
10 /*                                                                          */
11 /*  Use at your own risk.  We make no guarantees about the                  */
12 /*  correctness or usefulness of this code.                                 */
13 /*                                                                          */
14 /****************************************************************************/
15 
16 /****************************************************************************/
17 /*                                                                          */
18 /* Date: April 19, 1996  (dave)                                             */
19 /*       August 28, 1996 (bico)                                             */
20 /*       September 30, 1997 (dave)                                          */
21 /*                                                                          */
22 /*    EXPORTED FUNCTIONS:                                                   */
23 /*                                                                          */
24 /*  CCtsp_PROB_FILE *CCtsp_prob_read (char *f, int n)                       */
25 /*    NONE                                                                  */
26 /*                                                                          */
27 /*  CCtsp_PROB_FILE *CCtsp_prob_read_name (char *f)                         */
28 /*    NONE                                                                  */
29 /*                                                                          */
30 /*  CCtsp_PROB_FILE *CCtsp_prob_write (char *f, int n)                      */
31 /*    NONE                                                                  */
32 /*                                                                          */
33 /*  CCtsp_PROB_FILE *CCtsp_prob_write_name (char *fname)                    */
34 /*    NONE                                                                  */
35 /*                                                                          */
36 /*  int CCtsp_prob_file_delete (char *f, int n)                             */
37 /*    NONE                                                                  */
38 /*                                                                          */
39 /*  int CCtsp_prob_getname (CCtsp_PROB_FILE *p, char *name)                 */
40 /*    NONE                                                                  */
41 /*                                                                          */
42 /*  int CCtsp_prob_getid (CCtsp_PROB_FILE *p, int *id)                      */
43 /*    NONE                                                                  */
44 /*                                                                          */
45 /*  int CCtsp_prob_getparent (CCtsp_PROB_FILE *p, int *parent)              */
46 /*    NONE                                                                  */
47 /*                                                                          */
48 /*  int CCtsp_prob_getub (CCtsp_PROB_FILE *p, double *ub)                   */
49 /*    NONE                                                                  */
50 /*                                                                          */
51 /*  int CCtsp_prob_getlb (CCtsp_PROB_FILE *p, double *lb)                   */
52 /*    NONE                                                                  */
53 /*                                                                          */
54 /*  int CCtsp_prob_getexactlb (CCtsp_PROB_FILE *p, CCbigguy *lb)            */
55 /*    NONE                                                                  */
56 /*                                                                          */
57 /*  int CCtsp_prob_getnnodes (CCtsp_PROB_FILE *p, int *nnodes)              */
58 /*    NONE                                                                  */
59 /*                                                                          */
60 /*  int CCtsp_prob_getchildren (CCtsp_PROB_FILE *p, int *child0,            */
61 /*      int *child1)                                                        */
62 /*    NONE                                                                  */
63 /*                                                                          */
64 /*  int CCtsp_prob_getreal (CCtsp_PROB_FILE *p, int *real)                  */
65 /*    NONE                                                                  */
66 /*                                                                          */
67 /*  int CCtsp_prob_getprocessed (CCtsp_PROB_FILE *p, int *processed)        */
68 /*    NONE                                                                  */
69 /*                                                                          */
70 /*  int CCtsp_prob_getinfeasible (CCtsp_PROB_FILE *p, int *infeasible)      */
71 /*    NONE                                                                  */
72 /*                                                                          */
73 /*  int CCtsp_prob_gettour (CCtsp_PROB_FILE *p, int ncount, int **tour,     */
74 /*      int silent)                                                         */
75 /*    NONE                                                                  */
76 /*                                                                          */
77 /*  int CCtsp_prob_getedges (CCtsp_PROB_FILE *p, int ncount, int *nedges,   */
78 /*      int **elist, int **elen, int silent)                                */
79 /*    NONE                                                                  */
80 /*                                                                          */
81 /*  int CCtsp_prob_getcuts (CCtsp_PROB_FILE *p, int *ncount,                */
82 /*      CCtsp_lpcuts *cuts, int silent)                                     */
83 /*    NONE                                                                  */
84 /*                                                                          */
85 /*  int CCtsp_prob_getwarmstart (CCtsp_PROB_FILE *p, CClp_warmstart **w,    */
86 /*      int silent)                                                         */
87 /*    NONE                                                                  */
88 /*                                                                          */
89 /*  int CCtsp_prob_getfulladj (CCtsp_PROB_FILE *p, int ncount,              */
90 /*      int *fullcount, CCtsp_genadj **adj,                                 */
91 /*      CCtsp_genadjobj **adjspace, int silent)                             */
92 /*    NONE                                                                  */
93 /*                                                                          */
94 /*  int CCtsp_prob_getfixed (CCtsp_PROB_FILE *p, int ncount, int *ecount,   */
95 /*      int **elist, int silent)                                            */
96 /*    NONE                                                                  */
97 /*                                                                          */
98 /*  int CCtsp_prob_getexactdual (CCtsp_PROB_FILE *p, ncount,                */
99 /*      CCtsp_bigdual **d, int silent)                                      */
100 /*    NONE                                                                  */
101 /*                                                                          */
102 /*  int CCtsp_prob_gethistory (CCtsp_PROB_FILE *p, int *depth,              */
103 /*      CCtsp_branchobj **history, int silent)                              */
104 /*    NONE                                                                  */
105 /*                                                                          */
106 /*  int CCtsp_prob_rclose (CCtsp_PROB_FILE *p)                              */
107 /*    NONE                                                                  */
108 /*                                                                          */
109 /*  int CCtsp_prob_putname (CCtsp_PROB_FILE *p, char *name)                 */
110 /*    NONE                                                                  */
111 /*                                                                          */
112 /*  int CCtsp_prob_putid (CCtsp_PROB_FILE *p, int id)                       */
113 /*    NONE                                                                  */
114 /*                                                                          */
115 /*  int CCtsp_prob_putparent (CCtsp_PROB_FILE *p, int parent)               */
116 /*    NONE                                                                  */
117 /*                                                                          */
118 /*  int CCtsp_prob_putub (CCtsp_PROB_FILE *p, double ub)                    */
119 /*    NONE                                                                  */
120 /*                                                                          */
121 /*  int CCtsp_prob_putlb (CCtsp_PROB_FILE *p, double lb)                    */
122 /*    NONE                                                                  */
123 /*                                                                          */
124 /*  int CCtsp_prob_putexactlb (CCtsp_PROB_FILE *p, CCbigguy lb)             */
125 /*    NONE                                                                  */
126 /*                                                                          */
127 /*  int CCtsp_prob_putnnodes (CCtsp_PROB_FILE *p, int nnodes)               */
128 /*    NONE                                                                  */
129 /*                                                                          */
130 /*  int CCtsp_prob_putchildren (CCtsp_PROB_FILE *p, int child0,             */
131 /*      int child1)                                                         */
132 /*    NONE                                                                  */
133 /*                                                                          */
134 /*  int CCtsp_prob_putreal (CCtsp_PROB_FILE *p, int real)                   */
135 /*    NONE                                                                  */
136 /*                                                                          */
137 /*  int CCtsp_prob_putprocessed (CCtsp_PROB_FILE *p, int processed)         */
138 /*    NONE                                                                  */
139 /*                                                                          */
140 /*  int CCtsp_prob_putinfeasible (CCtsp_PROB_FILE *p, int infeasible)       */
141 /*    NONE                                                                  */
142 /*                                                                          */
143 /*  int CCtsp_prob_puttour (CCtsp_PROB_FILE *p, int ncount, int *tour)      */
144 /*    NONE                                                                  */
145 /*                                                                          */
146 /*  int CCtsp_prob_putedges (CCtsp_PROB_FILE *p, int ncount, int nedges,    */
147 /*      int *elist, int *elen)                                              */
148 /*    NONE                                                                  */
149 /*                                                                          */
150 /*  int CCtsp_prob_putcuts (CCtsp_PROB_FILE *p, int ncount,                 */
151 /*      CCtsp_lpcuts *cuts)                                                 */
152 /*    NONE                                                                  */
153 /*                                                                          */
154 /*  int CCtsp_prob_putwarmstart (CCtsp_PROB_FILE *p, CClp_warmstart *w)     */
155 /*    NONE                                                                  */
156 /*                                                                          */
157 /*  int CCtsp_prob_putfulladj (CCtsp_PROB_FILE *p, int ncount,              */
158 /*      int fullcount, CCtsp_genadj *adj)                                   */
159 /*    MISSING                                                               */
160 /*                                                                          */
161 /*  int CCtsp_prob_putfixed (CCtsp_PROB_FILE *p, int ncount,                */
162 /*      int ecount, int *elist)                                             */
163 /*    MISSING                                                               */
164 /*                                                                          */
165 /*  int CCtsp_prob_putexact_dual (CCtsp_PROB_FILE *p,                       */
166 /*      CCtsp_bigdual *exact_dual, int ncount)                              */
167 /*    NONE                                                                  */
168 /*                                                                          */
169 /*  int CCtsp_prob_puthistory (CCtsp_PROB_FILE *p, int depth,               */
170 /*      CCtsp_branchobj *history)                                           */
171 /*    NONE                                                                  */
172 /*                                                                          */
173 /*  int CCtsp_prob_wclose (CCtsp_PROB_FILE *p)                              */
174 /*    NONE                                                                  */
175 /*                                                                          */
176 /*  int CCtsp_prob_copy_section (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t,    */
177 /*      char section, int silent)                                           */
178 /*    NONE                                                                  */
179 /*                                                                          */
180 /*  char *CCtsp_problabel (const char *probloc)                             */
181 /*    -RETURNS a copy of the probname portion of probfile, or NULL if       */
182 /*     unable to allocate space for the copy.                               */
183 /*                                                                          */
184 /*  CCtsp_PROB_FILE *CCtsp_prob_read_remote (char *hname, char *pname,      */
185 /*      int n)                                                              */
186 /*    Only exists if CC_NETREADY is defined                                 */
187 /*                                                                          */
188 /*  CCtsp_PROB_FILE *CCtsp_prob_write_remote (char *hname, char *pname,     */
189 /*      int n)                                                              */
190 /*    Only exists if CC_NETREADY is defined                                 */
191 /*                                                                          */
192 /*  CCtsp_PROB_FILE *CCtsp_prob_server (CC_SFILE *s)                        */
193 /*    Only exists if CC_NETREADY is defined                                 */
194 /*                                                                          */
195 /*  int CCtsp_prob_delete_remote (char *hname, char *pname, int n)          */
196 /*    Only exists if CC_NETREADY is defined                                 */
197 /*                                                                          */
198 /****************************************************************************/
199 
200 #include "machdefs.h"
201 #include "util.h"
202 #include "tsp.h"
203 #include "bigguy.h"
204 
205 #define PROB_LOCAL  1
206 #define PROB_REMOTE 2
207 #define PROB_SERVER 3
208 
209 #define PROB_HEADER_VERSION    (1)
210 #define PROB_TOUR_VERSION      (1)
211 #define PROB_EDGES_VERSION     (1)
212 #define PROB_CUTS_VERSION      (1)
213 #define PROB_FULLADJ_VERSION   (1)
214 #define PROB_FIXED_VERSION     (1)
215 #define PROB_EXACTDUAL_VERSION (1)
216 #define PROB_HISTORY_VERSION   (1)
217 
218 
219 static int
220     prob_getheader (CCtsp_PROB_FILE *p, CCtsp_PROB_FILE *h),
221     prob_putheader (CCtsp_PROB_FILE *p, CCtsp_PROB_FILE *h),
222     prob_name (char *buf, size_t buflen, char *f, int n),
223     prob_copyheader (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t),
224     begin_put (CCtsp_PROB_FILE *p, int *offset, char section),
225     begin_get (CCtsp_PROB_FILE *p, int offset, char section, int silent),
226     begin_copy (CCtsp_PROB_FILE *f, int foffset, CCtsp_PROB_FILE *t,
227         int *toffset, char section, int silent),
228     prob_copytour (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
229     prob_copyedges (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
230     prob_copycuts (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
231     prob_copywarmstart (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
232     prob_copyfulladj (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
233     prob_copyfixed (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
234     prob_copyexactdual (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
235     prob_copyhistory (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent),
236     remote_name (const char *f),
237     split_name (const char *f, char *hostname, size_t hlen, char *probname,
238         size_t plen),
239     copy_ints (CC_SFILE *f, CC_SFILE *t, int n),
240     copy_int_val (CC_SFILE *f, CC_SFILE *t, int *n),
241     copy_bits (CC_SFILE *f, CC_SFILE *t, int n, int nbits),
242     copy_bits_val (CC_SFILE *f, CC_SFILE *t, int *n, int nbits),
243     copy_chars (CC_SFILE *f, CC_SFILE *t, int n),
244     copy_char_val (CC_SFILE *f, CC_SFILE *t, char *c),
245     copy_bigguys (CC_SFILE *f, CC_SFILE *t, int n);
246 
247 static void
248     prob_init (CCtsp_PROB_FILE *p);
249 
250 
251 
CCtsp_prob_file_delete(char * f,int n)252 int CCtsp_prob_file_delete (char *f, int n)
253 {
254     char nambuf[1024];
255     int sval;
256 
257     if (remote_name (f)) {
258 #ifdef CC_NETREADY
259         char hostbuf[1024];
260         if (split_name (f, hostbuf, sizeof (hostbuf),
261                         nambuf, sizeof (nambuf))) {
262             fprintf (stderr, "Cannot split remote name\n");
263             return -1;
264         }
265         return CCtsp_prob_delete_remote (hostbuf, nambuf, n);
266 #else /* CC_NETREADY */
267         fprintf (stderr, "Remote problem deleting not enabled\n");
268         return -1;
269 #endif
270     }
271 
272     if (prob_name (nambuf, sizeof (nambuf), f, n)) return 1;
273     /*
274     printf ("Delete File %s at time %.0f\n", nambuf, CCutil_real_zeit());
275     fflush (stdout);
276     */
277     sval = CCutil_sdelete_file (nambuf);
278     if (sval) {
279         fprintf (stderr, "Prob file %s could not be deleted\n", nambuf);
280     }
281     sval = CCutil_sdelete_file_backup (nambuf);
282     /*
283     if (!sval) {
284         printf ("Deleted backup to file: %s\n", nambuf);
285         fflush (stdout);
286     }
287     */
288 
289     return 0;
290 }
291 
CCtsp_prob_read(char * f,int n)292 CCtsp_PROB_FILE *CCtsp_prob_read (char *f, int n)
293 {
294     char nambuf[1024];
295 
296     if (remote_name (f)) {
297 #ifdef CC_NETREADY
298         char hostbuf[1024];
299         if (split_name (f, hostbuf, sizeof (hostbuf),
300                         nambuf, sizeof (nambuf))) {
301             fprintf (stderr, "Cannot split remote name\n");
302             return (CCtsp_PROB_FILE *) NULL;
303         }
304         return CCtsp_prob_read_remote (hostbuf, nambuf, n);
305 #else /* CC_NETREADY */
306         fprintf (stderr, "Remote problem reading not enabled\n");
307         return (CCtsp_PROB_FILE *) NULL;
308 #endif
309     }
310 
311     if (prob_name (nambuf, sizeof (nambuf), f, n))
312         return (CCtsp_PROB_FILE *) NULL;
313 
314     return CCtsp_prob_read_name (nambuf);
315 }
316 
CCtsp_prob_read_name(char * f)317 CCtsp_PROB_FILE *CCtsp_prob_read_name (char *f)
318 {
319     CCtsp_PROB_FILE *p = (CCtsp_PROB_FILE *) NULL;
320     int rval;
321 
322 #if 0
323     if (remote_name (f)) {
324         fprintf (stderr, "Cannot read remote problems by name\n");
325         return (CCtsp_PROB_FILE *) NULL;
326     }
327 #endif
328 
329     /*
330     printf ("Read File %s at time %.0f\n", f, CCutil_real_zeit());
331     fflush (stdout);
332     */
333 
334     p = CC_SAFE_MALLOC (1, CCtsp_PROB_FILE);
335     if (p == (CCtsp_PROB_FILE *) NULL) goto FAILURE;
336     prob_init (p);
337 
338     p->f = CCutil_sopen (f, "r");
339     if (!p->f) goto FAILURE;
340 
341     p->type = PROB_LOCAL;
342 
343     rval = prob_getheader (p, p);
344     if (rval) {
345         fprintf (stderr, "prob_getheader failed\n");
346         goto FAILURE;
347     }
348 
349     return p;
350 
351   FAILURE:
352     if (p) {
353         if (p->f) {
354             CCutil_sclose (p->f);
355         }
356         CC_FREE (p, CCtsp_PROB_FILE);
357     }
358     return (CCtsp_PROB_FILE *) NULL;
359 }
360 
361 #ifdef CC_NETREADY
362 
CCtsp_prob_read_remote(char * hname,char * pname,int n)363 CCtsp_PROB_FILE *CCtsp_prob_read_remote (char *hname, char *pname, int n)
364 {
365     CCtsp_PROB_FILE *p = (CCtsp_PROB_FILE *) NULL;
366     int rval;
367 
368     /* Hack for run at Princeton without .caam.rice.edu */
369 
370 /*
371     START HACK
372     char tname[4048];
373     sprintf (tname, "%s.caam.rice.edu", hname);
374     END HACK - also change hname to tname
375 */
376 
377     /*
378     printf ("Read Remote Host %s name %s id %d\n", hname, pname, n);
379     fflush (stdout);
380     */
381 
382     p = CC_SAFE_MALLOC (1, CCtsp_PROB_FILE);
383     if (p == (CCtsp_PROB_FILE *) NULL) goto FAILURE;
384     prob_init (p);
385 
386     p->f = CCutil_snet_open (hname, CCtsp_PROB_PORT);
387     if (p->f == (CC_SFILE *) NULL) {
388         fprintf (stderr, "Unable to contact server\n");
389         goto FAILURE;
390     }
391     p->type = PROB_REMOTE;
392 
393     rval = CCutil_swrite_char (p->f, CCtsp_Pread);
394     if (rval) {
395         fprintf (stderr, "CCutil_swrite_char failed\n"); goto FAILURE;
396     }
397     rval = CCutil_swrite_string (p->f, pname);
398     if (rval) {
399         fprintf (stderr, "CCutil_swrite_string failed\n"); goto FAILURE;
400     }
401     rval = CCutil_swrite_int (p->f, n);
402     if (rval) {
403         fprintf (stderr, "CCutil_swrite_int failed\n"); goto FAILURE;
404     }
405 
406     rval = prob_getheader (p, p);
407     if (rval) {
408         fprintf (stderr, "prob_getheader failed\n");
409         goto FAILURE;
410     }
411 
412     return p;
413 
414   FAILURE:
415     if (p) {
416         if (p->f) {
417             CCutil_sclose (p->f);
418         }
419         CC_FREE (p, CCtsp_PROB_FILE);
420     }
421     return (CCtsp_PROB_FILE *) NULL;
422 }
423 
424 #endif /* CC_NETREADY */
425 
426 /* reads header data from p into the struct h (p == h except from copyheader */
prob_getheader(CCtsp_PROB_FILE * p,CCtsp_PROB_FILE * h)427 static int prob_getheader (CCtsp_PROB_FILE *p, CCtsp_PROB_FILE *h)
428 {
429     char version;
430     int i;
431 
432     if (p->type == PROB_REMOTE) {
433         if (CCutil_swrite_char (p->f, CCtsp_Pheader)) return -1;
434     }
435 
436     if (CCutil_sread_char (p->f, &version)) return 1;
437 
438     switch (version) {
439     case 1:
440         for (i = 0; i < CCtsp_PROB_FILE_NAME_LEN; i++) {
441             if (CCutil_sread_char (p->f, &h->name[i]))           return 1;
442         }
443         if (CCutil_sread_int      (p->f, &h->parent))            return 1;
444         if (CCutil_sread_int      (p->f, &h->id))                return 1;
445         if (CCutil_sread_double   (p->f, &h->ub))                return 1;
446         if (CCutil_sread_double   (p->f, &h->lb))                return 1;
447         if (CCbigguy_sread        (p->f, &h->exactlb))           return 1;
448         if (CCutil_sread_int      (p->f, &h->nnodes))            return 1;
449         if (CCutil_sread_int      (p->f, &h->child0))            return 1;
450         if (CCutil_sread_int      (p->f, &h->child1))            return 1;
451         if (CCutil_sread_int      (p->f, &h->real))              return 1;
452         if (CCutil_sread_int      (p->f, &h->processed))         return 1;
453         if (CCutil_sread_int      (p->f, &h->infeasible))        return 1;
454         if (p->type == PROB_LOCAL) {
455             if (CCutil_sread_int  (p->f, &h->offsets.dat))       return 1;
456             if (CCutil_sread_int  (p->f, &h->offsets.edge))      return 1;
457             if (CCutil_sread_int  (p->f, &h->offsets.fulladj))   return 1;
458             if (CCutil_sread_int  (p->f, &h->offsets.cut))       return 1;
459             if (CCutil_sread_int  (p->f, &h->offsets.tour))      return 1;
460             if (CCutil_sread_int  (p->f, &h->offsets.fix))       return 1;
461             if (CCutil_sread_int  (p->f, &h->offsets.exactdual)) return 1;
462             if (CCutil_sread_int  (p->f, &h->offsets.history))   return 1;
463             if (CCutil_sread_int  (p->f, &h->offsets.warmstart)) return 1;
464         }
465         break;
466     default:
467         fprintf (stderr, "Unknown problem version %ud\n", (unsigned) version);
468         return 1;
469     }
470     return 0;
471 }
472 
CCtsp_prob_write(char * f,int n)473 CCtsp_PROB_FILE *CCtsp_prob_write (char *f, int n)
474 {
475     char nambuf[1024];
476 
477     if (remote_name (f)) {
478 #ifdef CC_NETREADY
479         char hostbuf[1024];
480         if (split_name (f, hostbuf, sizeof (hostbuf),
481                         nambuf, sizeof (nambuf))) {
482             fprintf (stderr, "Cannot split remote name\n");
483             return (CCtsp_PROB_FILE *) NULL;
484         }
485         return CCtsp_prob_write_remote (hostbuf, nambuf, n);
486 #else /* CC_NETREADY */
487         fprintf (stderr, "Remote problem writing not enabled\n");
488         return (CCtsp_PROB_FILE *) NULL;
489 #endif
490     }
491 
492     if (prob_name (nambuf, sizeof (nambuf), f, n))
493         return (CCtsp_PROB_FILE *) NULL;
494 
495     return CCtsp_prob_write_name (nambuf);
496 }
497 
CCtsp_prob_write_name(char * fname)498 CCtsp_PROB_FILE *CCtsp_prob_write_name (char *fname)
499 {
500     CCtsp_PROB_FILE *p = (CCtsp_PROB_FILE *) NULL;
501 
502 #if 0
503     if (remote_name (fname)) {
504         fprintf (stderr, "Cannot write remote problems by name\n");
505         return (CCtsp_PROB_FILE *) NULL;
506     }
507 #endif
508 
509     /*
510     printf ("Write File %s at time %.0f\n", fname, CCutil_real_zeit());
511     fflush (stdout);
512     */
513 
514     p = CC_SAFE_MALLOC (1, CCtsp_PROB_FILE);
515     if (p == (CCtsp_PROB_FILE *) NULL) goto FAILURE;
516     prob_init (p);
517 
518     p->f = CCutil_sopen (fname, "w");
519     if (!p->f) goto FAILURE;
520 
521     p->type = PROB_LOCAL;
522 
523     if (prob_putheader (p, p)) {
524         fprintf (stderr, "prob_putheader failed\n");
525         goto FAILURE;
526     }
527 
528     return p;
529 
530 FAILURE:
531     if (p) {
532         if (p->f) {
533             CCutil_sclose (p->f);
534         }
535         CC_FREE (p, CCtsp_PROB_FILE);
536     }
537     return (CCtsp_PROB_FILE *) NULL;
538 }
539 
540 #ifdef CC_NETREADY
541 
CCtsp_prob_write_remote(char * hname,char * pname,int n)542 CCtsp_PROB_FILE *CCtsp_prob_write_remote (char *hname, char *pname, int n)
543 {
544     CCtsp_PROB_FILE *p = (CCtsp_PROB_FILE *) NULL;
545     int i;
546     int rval;
547 
548     /* Hack for run at Princeton without .caam.rice.edu */
549 
550 /*
551     START HACK
552     char tname[4048];
553     sprintf (tname, "%s.caam.rice.edu", hname);
554     END HACK - also change hname to tname
555 */
556 
557     /*
558     printf ("Write Remote Host %s name %s id %d\n", hname, pname, n);
559     fflush (stdout);
560     */
561 
562     p = CC_SAFE_MALLOC (1, CCtsp_PROB_FILE);
563     if (p == (CCtsp_PROB_FILE *) NULL) goto FAILURE;
564     prob_init (p);
565 
566     for (i = 0; pname[i] && i < CCtsp_PROB_FILE_NAME_LEN - 1; i++)
567         p->name[i] = pname[i];
568     p->name[i] = '\0';
569 
570     p->f = CCutil_snet_open (hname, CCtsp_PROB_PORT);
571     if (p->f == (CC_SFILE *) NULL) {
572         fprintf (stderr, "Unable to contact server\n");
573         goto FAILURE;
574     }
575     p->type = PROB_REMOTE;
576 
577     rval = CCutil_swrite_char (p->f, CCtsp_Pwrite);
578     if (rval) {
579         fprintf (stderr, "CCutil_swrite_char failed\n"); goto FAILURE;
580     }
581     rval = CCutil_swrite_string (p->f, pname);
582     if (rval) {
583         fprintf (stderr, "CCutil_swrite_string failed\n"); goto FAILURE;
584     }
585     rval = CCutil_swrite_int (p->f, n);
586     if (rval) {
587         fprintf (stderr, "CCutil_swrite_int failed\n"); goto FAILURE;
588     }
589 
590     return p;
591 
592 FAILURE:
593     if (p) {
594         if (p->f) {
595             CCutil_sclose (p->f);
596         }
597         CC_FREE (p, CCtsp_PROB_FILE);
598     }
599     return (CCtsp_PROB_FILE *) NULL;
600 }
601 
CCtsp_prob_delete_remote(char * hname,char * pname,int n)602 int CCtsp_prob_delete_remote (char *hname, char *pname, int n)
603 {
604     CC_SFILE *f = (CC_SFILE *) NULL;
605     int rval;
606 
607     printf ("Delete Remote Host %s name %s id %d\n", hname, pname, n);
608     fflush (stdout);
609 
610     f = CCutil_snet_open (hname, CCtsp_PROB_PORT);
611     if (f == (CC_SFILE *) NULL) {
612         fprintf (stderr, "Unable to contact server\n");
613         rval = 1; goto CLEANUP;
614     }
615 
616     rval = CCutil_swrite_char (f, CCtsp_Pdelete);
617     if (rval) {
618         fprintf (stderr, "CCutil_swrite_char failed\n"); goto CLEANUP;
619     }
620     rval = CCutil_swrite_string (f, pname);
621     if (rval) {
622         fprintf (stderr, "CCutil_swrite_string failed\n"); goto CLEANUP;
623     }
624     rval = CCutil_swrite_int (f, n);
625     if (rval) {
626         fprintf (stderr, "CCutil_swrite_int failed\n"); goto CLEANUP;
627     }
628 
629     rval = CCutil_sclose (f);
630     if (rval) {
631         fprintf (stderr, "CCutil_sclose failed\n");
632     }
633     f = (CC_SFILE *) NULL;
634 
635     rval = 0;
636   CLEANUP:
637     if (f != (CC_SFILE *) NULL) {
638         CCutil_sclose (f);
639     }
640     return rval;
641 }
642 
CCtsp_prob_server(CC_SFILE * s)643 CCtsp_PROB_FILE *CCtsp_prob_server (CC_SFILE *s)
644 {
645     CCtsp_PROB_FILE *p = (CCtsp_PROB_FILE *) NULL;
646 
647     p = CC_SAFE_MALLOC (1, CCtsp_PROB_FILE);
648     if (p == (CCtsp_PROB_FILE *) NULL) {
649         return (CCtsp_PROB_FILE *) NULL;
650     }
651     prob_init (p);
652 
653     p->f = s;
654     p->type = PROB_SERVER;
655 
656     return p;
657 }
658 
659 #endif /* CC_NETREADY */
660 
661 /* writes header data from struct h to p (p == h except from copyheader */
prob_putheader(CCtsp_PROB_FILE * p,CCtsp_PROB_FILE * h)662 static int prob_putheader (CCtsp_PROB_FILE *p, CCtsp_PROB_FILE *h)
663 {
664     int  i;
665 
666     if (p->type == PROB_LOCAL) {
667         if (CCutil_srewind (p->f))                           return 1;
668     } else if (p->type == PROB_REMOTE) {
669         if (CCutil_swrite_char (p->f, CCtsp_Pheader))        return 1;
670     }
671 
672     if (CCutil_swrite_char (p->f, PROB_HEADER_VERSION))
673         return 1;
674     for (i = 0; i < CCtsp_PROB_FILE_NAME_LEN; i++) {
675         if (CCutil_swrite_char (p->f, h->name[i]))           return 1;
676     }
677     if (CCutil_swrite_int      (p->f, h->parent))            return 1;
678     if (CCutil_swrite_int      (p->f, h->id))                return 1;
679     if (CCutil_swrite_double   (p->f, h->ub))                return 1;
680     if (CCutil_swrite_double   (p->f, h->lb))                return 1;
681     if (CCbigguy_swrite        (p->f, h->exactlb))           return 1;
682     if (CCutil_swrite_int      (p->f, h->nnodes))            return 1;
683     if (CCutil_swrite_int      (p->f, h->child0))            return 1;
684     if (CCutil_swrite_int      (p->f, h->child1))            return 1;
685     if (CCutil_swrite_int      (p->f, h->real))              return 1;
686     if (CCutil_swrite_int      (p->f, h->processed))         return 1;
687     if (CCutil_swrite_int      (p->f, h->infeasible))        return 1;
688     if (p->type == PROB_LOCAL) {
689         if (CCutil_swrite_int  (p->f, h->offsets.dat))       return 1;
690         if (CCutil_swrite_int  (p->f, h->offsets.edge))      return 1;
691         if (CCutil_swrite_int  (p->f, h->offsets.fulladj))   return 1;
692         if (CCutil_swrite_int  (p->f, h->offsets.cut))       return 1;
693         if (CCutil_swrite_int  (p->f, h->offsets.tour))      return 1;
694         if (CCutil_swrite_int  (p->f, h->offsets.fix))       return 1;
695         if (CCutil_swrite_int  (p->f, h->offsets.exactdual)) return 1;
696         if (CCutil_swrite_int  (p->f, h->offsets.history))   return 1;
697         if (CCutil_swrite_int  (p->f, h->offsets.warmstart)) return 1;
698     }
699 
700     return 0;
701 }
702 
prob_name(char * buf,size_t buflen,char * f,int n)703 static int prob_name (char *buf, size_t buflen, char *f, int n)
704 {
705     int l = (int) strlen(f);
706     int lastslash;
707     int i;
708     int d;
709 
710     if (l + 5 > (int) buflen || n < 0) {
711         fprintf (stderr, "Cannot generate filename for %s node %d\n",
712                  f, n);
713         return -1;
714     }
715 
716     for (i = 0, lastslash = -1; i < l; i++) {
717         if (f[i] == '/') lastslash = i;
718         buf[i] = f[i];
719     }
720     if (l > lastslash + 9) l = lastslash + 9;
721     for (i = lastslash+1; i < l; i++) {
722         if (buf[i] == '.') buf[i] = '_';
723     }
724     if (n < 1000) {
725         buf[l++] = '.';
726         d = n/100;
727         buf[l++] = '0' + ((unsigned int) d);
728         n -= d*100;
729         d = n/10;
730         buf[l++] = '0' + d;
731         n -= d*10;
732         d = n;
733         buf[l++] = '0' + ((unsigned int) d);
734     } else if (n < 1000 + (26*36*36 - 5)) {
735         buf[l++] = '.';
736 #define NAMESTRNUM(xa,xb,xc) (((xa)-'a') * 1296 + ((xb)-'a'+10) * 36 + \
737                               ((xc)-'a'+10))
738         n -= 1000;
739         if (n >= NAMESTRNUM('m','a','s')) n++;
740         if (n >= NAMESTRNUM('p','u','l')) n++;
741         if (n >= NAMESTRNUM('r','e','s')) n++;
742         if (n >= NAMESTRNUM('s','a','v')) n++;
743         if (n >= NAMESTRNUM('s','o','l')) n++;
744         if (n >= NAMESTRNUM('x','x','x')) n++;
745         d = n/1296;
746         buf[l++] = 'a' + ((unsigned int) d);
747         n -= d*1296;
748         d = n/36;
749         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
750                             : 'a' + ((unsigned int) (d-10));
751         n -= d*36;
752         d = n;
753         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
754                             : 'a' + ((unsigned int) (d-10));
755     } else if (n < 1000 + (26*36*36 - 5) + 36*36*36*36) {
756         n -= 1000;
757         n -= 26*36*36 - 5;
758         d = n/(36*36*36);
759         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
760                             : 'a' + ((unsigned int) (d-10));
761         n -= d*36*36*36;
762         d = n/(36*36);
763         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
764                             : 'a' + ((unsigned int) (d-10));
765         n -= d*(36*36);
766         d = n/36;
767         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
768                             : 'a' + ((unsigned int) (d-10));
769         n -= d*36;
770         d = n;
771         buf[l++] = (d < 10) ? '0' + ((unsigned int) d)
772                             : 'a' + ((unsigned int) (d-10));
773     } else {
774         fprintf (stderr, "Node number %d too large\n", n);
775         return -1;
776     }
777 
778     buf[l] = '\0';
779     return 0;
780 }
781 
prob_init(CCtsp_PROB_FILE * p)782 static void prob_init (CCtsp_PROB_FILE *p)
783 {
784     int i;
785 
786     p->f = (CC_SFILE *) NULL;
787     for (i = 0; i < CCtsp_PROB_FILE_NAME_LEN; i++)
788         p->name[i] = '\0';
789     p->type = 0;
790     p->id = -1;
791     p->parent = -1;
792     p->lb = -1.0;
793     p->ub = -1.0;
794     p->exactlb = CCbigguy_ZERO;
795     p->nnodes = -1;
796     p->child0 = -1;
797     p->child1 = -1;
798     p->real = -1;
799     p->processed = -1;
800     p->infeasible = -1;
801     p->offsets.dat = -1;
802     p->offsets.edge = -1;
803     p->offsets.fulladj = -1;
804     p->offsets.cut = -1;
805     p->offsets.tour = -1;
806     p->offsets.basis = -1;
807     p->offsets.norms = -1;
808     p->offsets.fix = -1;
809     p->offsets.exactdual = -1;
810     p->offsets.history = -1;
811     p->offsets.warmstart = -1;
812 }
813 
CCtsp_prob_rclose(CCtsp_PROB_FILE * p)814 int CCtsp_prob_rclose (CCtsp_PROB_FILE *p)
815 {
816     int rval = 0;
817 
818     if (p->type == PROB_REMOTE) {
819         rval |= CCutil_swrite_char (p->f, CCtsp_Pexit);
820     }
821 
822     if (p->type != PROB_SERVER) {
823         rval |= CCutil_sclose (p->f);
824     }
825 
826     CC_FREE (p, CCtsp_PROB_FILE);
827 
828     if (rval) return 1;
829     else return 0;
830 }
831 
CCtsp_prob_wclose(CCtsp_PROB_FILE * p)832 int CCtsp_prob_wclose (CCtsp_PROB_FILE *p)
833 {
834     int rval = 0;
835 
836     rval |= prob_putheader (p, p);
837     if (p->type == PROB_REMOTE) {
838         rval |= CCutil_swrite_char (p->f, CCtsp_Pexit);
839     }
840 
841     if (p->type != PROB_SERVER) {
842         rval |= CCutil_sclose (p->f);
843     }
844 
845     CC_FREE (p, CCtsp_PROB_FILE);
846 
847     return rval;
848 }
849 
CCtsp_prob_getname(CCtsp_PROB_FILE * p,char * name)850 int CCtsp_prob_getname (CCtsp_PROB_FILE *p, char *name)
851 {
852     int i;
853 
854     if (!p) return -1;
855 
856     for (i = 0; i < CCtsp_PROB_FILE_NAME_LEN; i++)
857         name[i] = p->name[i];
858 
859     return 0;
860 }
861 
CCtsp_prob_putname(CCtsp_PROB_FILE * p,char * name)862 int CCtsp_prob_putname (CCtsp_PROB_FILE *p, char *name)
863 {
864     int i;
865 
866     if (!p) return 1;
867 
868     for (i = 0; name[i] && i < CCtsp_PROB_FILE_NAME_LEN - 1; i++)
869         p->name[i] = name[i];
870     for (; i < CCtsp_PROB_FILE_NAME_LEN; i++)
871         p->name[i] = '\0';
872 
873     return 0;
874 }
875 
876 
CCtsp_prob_getid(CCtsp_PROB_FILE * p,int * id)877 int CCtsp_prob_getid (CCtsp_PROB_FILE *p, int *id)
878 {
879     if (!p) return -1;
880 
881     *id = p->id;
882     if (*id == -1) {
883         printf ("Setting -1 ID to 0\n"); fflush (stdout);
884         *id = 0;
885     }
886     return 0;
887 }
888 
CCtsp_prob_putid(CCtsp_PROB_FILE * p,int id)889 int CCtsp_prob_putid (CCtsp_PROB_FILE *p, int id)
890 {
891     if (!p) return 1;
892 
893     p->id = id;
894     return 0;
895 }
896 
CCtsp_prob_getparent(CCtsp_PROB_FILE * p,int * parent)897 int CCtsp_prob_getparent (CCtsp_PROB_FILE *p, int *parent)
898 {
899     if (!p) return -1;
900 
901     *parent = p->parent;
902     return 0;
903 }
904 
CCtsp_prob_putparent(CCtsp_PROB_FILE * p,int parent)905 int CCtsp_prob_putparent (CCtsp_PROB_FILE *p, int parent)
906 {
907     if (!p) return 1;
908 
909     p->parent = parent;
910     return 0;
911 }
912 
CCtsp_prob_getub(CCtsp_PROB_FILE * p,double * ub)913 int CCtsp_prob_getub (CCtsp_PROB_FILE *p, double *ub)
914 {
915     if (!p) return -1;
916 
917     *ub = p->ub;
918     return 0;
919 }
920 
CCtsp_prob_putub(CCtsp_PROB_FILE * p,double ub)921 int CCtsp_prob_putub (CCtsp_PROB_FILE *p, double ub)
922 {
923     if (!p) return 1;
924 
925     p->ub = ub;
926     return 0;
927 }
928 
CCtsp_prob_getlb(CCtsp_PROB_FILE * p,double * lb)929 int CCtsp_prob_getlb (CCtsp_PROB_FILE *p, double *lb)
930 {
931     if (!p) return -1;
932 
933     *lb = p->lb;
934     return 0;
935 }
936 
CCtsp_prob_putlb(CCtsp_PROB_FILE * p,double lb)937 int CCtsp_prob_putlb (CCtsp_PROB_FILE *p, double lb)
938 {
939     if (!p) return 1;
940 
941     p->lb = lb;
942     return 0;
943 }
944 
CCtsp_prob_getexactlb(CCtsp_PROB_FILE * p,CCbigguy * lb)945 int CCtsp_prob_getexactlb (CCtsp_PROB_FILE *p, CCbigguy *lb)
946 {
947     if (!p) return -1;
948 
949     *lb = p->exactlb;
950     return 0;
951 }
952 
CCtsp_prob_putexactlb(CCtsp_PROB_FILE * p,CCbigguy lb)953 int CCtsp_prob_putexactlb (CCtsp_PROB_FILE *p, CCbigguy lb)
954 {
955     if (!p) return 1;
956 
957     p->exactlb = lb;
958     return 0;
959 }
960 
CCtsp_prob_getnnodes(CCtsp_PROB_FILE * p,int * nnodes)961 int CCtsp_prob_getnnodes (CCtsp_PROB_FILE *p, int *nnodes)
962 {
963     if (!p) return -1;
964 
965     *nnodes = p->nnodes;
966     return 0;
967 }
968 
CCtsp_prob_putnnodes(CCtsp_PROB_FILE * p,int nnodes)969 int CCtsp_prob_putnnodes (CCtsp_PROB_FILE *p, int nnodes)
970 {
971     if (!p) return 1;
972 
973     p->nnodes = nnodes;
974     return 0;
975 }
976 
CCtsp_prob_getchildren(CCtsp_PROB_FILE * p,int * child0,int * child1)977 int CCtsp_prob_getchildren (CCtsp_PROB_FILE *p, int *child0, int *child1)
978 {
979     if (!p) return -1;
980 
981     *child0 = p->child0;
982     *child1 = p->child1;
983     return 0;
984 }
985 
CCtsp_prob_putchildren(CCtsp_PROB_FILE * p,int child0,int child1)986 int CCtsp_prob_putchildren (CCtsp_PROB_FILE *p, int child0, int child1)
987 {
988     if (!p) return 1;
989 
990     p->child0 = child0;
991     p->child1 = child1;
992     return 0;
993 }
994 
CCtsp_prob_getreal(CCtsp_PROB_FILE * p,int * real)995 int CCtsp_prob_getreal (CCtsp_PROB_FILE *p, int *real)
996 {
997     if (!p) return -1;
998 
999     *real = p->real;
1000     return 0;
1001 }
1002 
CCtsp_prob_putreal(CCtsp_PROB_FILE * p,int real)1003 int CCtsp_prob_putreal (CCtsp_PROB_FILE *p, int real)
1004 {
1005     if (!p) return 1;
1006 
1007     p->real = real;
1008     return 0;
1009 }
1010 
CCtsp_prob_getprocessed(CCtsp_PROB_FILE * p,int * processed)1011 int CCtsp_prob_getprocessed (CCtsp_PROB_FILE *p, int *processed)
1012 {
1013     if (!p) return -1;
1014 
1015     *processed = p->processed;
1016     return 0;
1017 }
1018 
CCtsp_prob_putprocessed(CCtsp_PROB_FILE * p,int processed)1019 int CCtsp_prob_putprocessed (CCtsp_PROB_FILE *p, int processed)
1020 {
1021     if (!p) return 1;
1022 
1023     p->processed = processed;
1024     return 0;
1025 }
1026 
CCtsp_prob_getinfeasible(CCtsp_PROB_FILE * p,int * infeasible)1027 int CCtsp_prob_getinfeasible (CCtsp_PROB_FILE *p, int *infeasible)
1028 {
1029     if (!p) return -1;
1030 
1031     *infeasible = p->infeasible;
1032     return 0;
1033 }
1034 
CCtsp_prob_putinfeasible(CCtsp_PROB_FILE * p,int infeasible)1035 int CCtsp_prob_putinfeasible (CCtsp_PROB_FILE *p, int infeasible)
1036 {
1037     if (!p) return 1;
1038 
1039     p->infeasible = infeasible;
1040     return 0;
1041 }
1042 
prob_copyheader(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t)1043 static int prob_copyheader (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t)
1044 {
1045     int rval;
1046 
1047     if (f->type == PROB_SERVER && t->type == PROB_SERVER) {
1048         fprintf (stderr, "Cannot copy header from server to server\n");
1049         return -1;
1050     } else if (f->type == PROB_SERVER) {
1051         rval = prob_getheader (f, t);
1052         if (rval) fprintf (stderr, "prob_getheader failed\n");
1053         return rval;
1054     } else if (t->type == PROB_SERVER) {
1055         rval = prob_putheader (t, f);
1056         if (rval) fprintf (stderr, "prob_putheader failed\n");
1057         return rval;
1058     } else {
1059         strcpy (t->name, f->name);
1060         t->id = f->id;
1061         t->parent = f->parent;
1062         t->ub = f->ub;
1063         t->lb = f->lb;
1064         t->exactlb = f->exactlb;
1065         t->nnodes = f->nnodes;
1066         t->child0 = f->child0;
1067         t->child1 = f->child1;
1068         t->real = f->real;
1069         t->processed = f->processed;
1070         t->infeasible = f->infeasible;
1071         return 1;
1072     }
1073 }
1074 
begin_put(CCtsp_PROB_FILE * p,int * offset,char section)1075 static int begin_put (CCtsp_PROB_FILE *p, int *offset, char section)
1076 {
1077     if (p->type == PROB_LOCAL) {
1078         *offset = CCutil_stell (p->f);
1079     } else if (p->type == PROB_REMOTE) {
1080         if (CCutil_swrite_char (p->f, section)) return 1;
1081     }
1082 
1083     return 0;
1084 }
1085 
begin_get(CCtsp_PROB_FILE * p,int offset,char section,int silent)1086 static int begin_get (CCtsp_PROB_FILE *p, int offset, char section, int silent)
1087 {
1088     if (p->type == PROB_LOCAL) {
1089         if (offset == -1) {
1090             if (!silent) {
1091                 printf ("No section %c in file.\n", section);
1092                 fflush (stdout);
1093             }
1094             return 1;
1095         }
1096         if (CCutil_sseek (p->f, offset)) {
1097             fprintf (stderr, "CCutil_sseek failed in begin_get\n");
1098             fflush (stdout);
1099             return -1;
1100         }
1101     } else if (p->type == PROB_REMOTE) {
1102         char exists;
1103 
1104         if (CCutil_swrite_char (p->f, section)) return -1;
1105         if (CCutil_sread_char (p->f, &exists)) return -1;
1106         if (exists == 0) {
1107             if (!silent) {
1108                 printf ("No section %c in remote file\n", section);
1109                 fflush (stdout);
1110             }
1111             return 1;
1112         }
1113     }
1114     return 0;
1115 }
1116 
begin_copy(CCtsp_PROB_FILE * f,int foffset,CCtsp_PROB_FILE * t,int * toffset,char section,int silent)1117 static int begin_copy (CCtsp_PROB_FILE *f, int foffset, CCtsp_PROB_FILE *t,
1118         int *toffset, char section, int silent)
1119 {
1120     char exists;
1121 
1122     if (f->type == PROB_LOCAL) {
1123         if (foffset == -1) {
1124             if (!silent) {
1125                 printf ("No section %c in file.\n", section);
1126                 fflush (stdout);
1127             }
1128             exists = 0;
1129         } else {
1130             if (CCutil_sseek (f->f, foffset)) {
1131                 fprintf (stderr, "CCutil_sseek failed in begin_copy\n");
1132                 return -1;
1133             }
1134             exists = 1;
1135         }
1136     } else if (f->type == PROB_REMOTE) {
1137         if (CCutil_swrite_char (f->f, section)) return -1;
1138         if (CCutil_sread_char (f->f, &exists)) return -1;
1139         if (exists == 0) {
1140             if (!silent) {
1141                 printf ("No section %c in remote file\n", section);
1142                 fflush (stdout);
1143             }
1144         }
1145     } else if (f->type == PROB_SERVER) {
1146         exists = 1;
1147     }
1148 
1149     if (t->type == PROB_SERVER) {
1150         if (CCutil_swrite_char (t->f, exists)) return -1;
1151     }
1152     if (exists) {
1153         if (t->type == PROB_LOCAL) {
1154             *toffset = CCutil_stell (t->f);
1155         } else if (t->type == PROB_REMOTE) {
1156             if (CCutil_swrite_char (t->f, section)) return -1;
1157         }
1158         return 0;
1159     } else {
1160         return 1;
1161     }
1162 }
1163 
CCtsp_prob_puttour(CCtsp_PROB_FILE * p,int ncount,int * tour)1164 int CCtsp_prob_puttour (CCtsp_PROB_FILE *p, int ncount, int *tour)
1165 {
1166     int i;
1167     int rval;
1168     int nbits;
1169 
1170     if (!p) return 1;
1171 
1172     rval = begin_put (p, &p->offsets.tour, CCtsp_Ptour);
1173     if (rval) return rval;
1174 
1175     if (CCutil_swrite_char     (p->f, PROB_TOUR_VERSION)) return 1;
1176     if (CCutil_swrite_int      (p->f, ncount))            return 1;
1177 
1178     nbits = CCutil_sbits (ncount);
1179 
1180     for (i = 0; i < ncount; i++) {
1181         if (CCutil_swrite_bits (p->f, tour[i], nbits))    return 1;
1182     }
1183 
1184     return 0;
1185 }
1186 
CCtsp_prob_gettour(CCtsp_PROB_FILE * p,int ncount,int ** tour,int silent)1187 int CCtsp_prob_gettour (CCtsp_PROB_FILE *p, int ncount, int **tour, int silent)
1188 {
1189     int i;
1190     int rval;
1191     int nbits;
1192     char version;
1193     int ncount2;
1194 
1195     if (!p) return -1;
1196 
1197     rval = begin_get (p, p->offsets.tour, CCtsp_Ptour, silent);
1198     if (rval) return rval;
1199 
1200     *tour = (int *) NULL;
1201 
1202     if (CCutil_sread_char (p->f, &version)) goto FAILURE;
1203     switch (version) {
1204     case 1:
1205         if (CCutil_sread_int (p->f, &ncount2)) goto FAILURE;
1206         if (ncount != ncount) {
1207             fprintf (stderr, "Wrong ncount in tour\n");
1208             goto FAILURE;
1209         }
1210         nbits = CCutil_sbits (ncount2);
1211         *tour = CC_SAFE_MALLOC (ncount2, int);
1212         if (!(*tour)) {
1213             fprintf (stderr, "out of memory in CCtsp_prob_gettour\n");
1214             goto FAILURE;
1215         }
1216 
1217         for (i = 0; i < ncount2; i++) {
1218             if (CCutil_sread_bits (p->f, &((*tour)[i]), nbits)) {
1219                 goto FAILURE;
1220             }
1221         }
1222         break;
1223     default:
1224         fprintf (stderr, "Unknown tour version %ud\n", (unsigned) version);
1225         goto FAILURE;
1226     }
1227     return 0;
1228 
1229  FAILURE:
1230     CC_IFFREE (*tour, int);
1231     return -1;
1232 }
1233 
prob_copytour(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1234 static int prob_copytour (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1235 {
1236     int ncount;
1237     int rval;
1238     char version;
1239     int nbits;
1240 
1241     rval = begin_copy (f, f->offsets.tour, t, &t->offsets.tour, CCtsp_Ptour,
1242                        silent);
1243     if (rval == 1) return 0;
1244     else if (rval) return rval;
1245 
1246     rval = copy_char_val (f->f, t->f, (char *) &version);
1247     if (rval) return -1;
1248 
1249     switch (version) {
1250     case 1:
1251         rval = copy_int_val (f->f, t->f, &ncount);
1252         if (rval) return -1;
1253         nbits = CCutil_sbits (ncount);
1254         rval = copy_bits (f->f, t->f, ncount, nbits);
1255         if (rval) return rval;
1256         break;
1257     default:
1258         fprintf (stderr, "Unknown tour version %ud\n", (unsigned) version);
1259         return 1;
1260     }
1261     return 0;
1262 }
1263 
CCtsp_prob_putedges(CCtsp_PROB_FILE * p,int ncount,int ecount,int * elist,int * elen)1264 int CCtsp_prob_putedges (CCtsp_PROB_FILE *p, int ncount, int ecount,
1265         int *elist, int *elen)
1266 {
1267     int i;
1268     int rval;
1269     int nbits;
1270 
1271     if (!p) return 1;
1272 
1273     rval = begin_put (p, &p->offsets.edge, CCtsp_Pedges);
1274     if (rval) return rval;
1275 
1276     if (CCutil_swrite_char (p->f, PROB_EDGES_VERSION)) return 1;
1277 
1278     if (CCutil_swrite_int (p->f, ncount)) return 1;
1279     if (CCutil_swrite_int (p->f, ecount)) return 1;
1280     nbits = CCutil_sbits (ncount);
1281 
1282     for (i = 0; i < ecount; i++) {
1283         if (CCutil_swrite_bits (p->f, elist[2*i], nbits)) return 1;
1284         if (CCutil_swrite_bits (p->f, elist[2*i+1], nbits)) return 1;
1285         if (CCutil_swrite_int (p->f, elen[i])) return 1;
1286     }
1287 
1288     return 0;
1289 }
1290 
CCtsp_prob_getedges(CCtsp_PROB_FILE * p,int ncount,int * ecount,int ** elist,int ** elen,int silent)1291 int CCtsp_prob_getedges (CCtsp_PROB_FILE *p, int ncount, int *ecount,
1292         int **elist, int **elen, int silent)
1293 {
1294     int i;
1295     int rval;
1296     char version;
1297     int nbits;
1298     int ncount2;
1299 
1300     if (!p) return -1;
1301 
1302     *elist = (int *) NULL;
1303     *elen = (int *) NULL;
1304 
1305     rval = begin_get (p, p->offsets.edge, CCtsp_Pedges, silent);
1306     if (rval) return rval;
1307 
1308     if (CCutil_sread_char (p->f, &version)) goto FAILURE;
1309     switch (version) {
1310     case 1:
1311         if (CCutil_sread_int (p->f, &ncount2)) goto FAILURE;
1312         if (ncount2 != ncount) {
1313             fprintf (stderr, "Wrong ncount in edges\n");
1314             goto FAILURE;
1315         }
1316         if (CCutil_sread_int (p->f, ecount)) goto FAILURE;
1317         nbits = CCutil_sbits (ncount2);
1318         *elist = CC_SAFE_MALLOC (2 * (*ecount), int);
1319         *elen = CC_SAFE_MALLOC (*ecount, int);
1320         if (!(*elist) || !(*elen)) {
1321             fprintf (stderr, "out of memory in CCtsp_prob_getedges\n");
1322             goto FAILURE;
1323         }
1324 
1325         for (i = 0; i < (*ecount); i++) {
1326             if (CCutil_sread_bits (p->f, &((*elist)[2*i]), nbits)) {
1327                 goto FAILURE;
1328             }
1329             if (CCutil_sread_bits (p->f, &((*elist)[2*i+1]), nbits)) {
1330                 goto FAILURE;
1331             }
1332             if (CCutil_sread_int (p->f, &((*elen)[i]))) {
1333                 goto FAILURE;
1334             }
1335         }
1336         break;
1337     default:
1338         fprintf (stderr, "Unknown edges version %ud\n", (unsigned) version);
1339         goto FAILURE;
1340     }
1341     return 0;
1342 
1343  FAILURE:
1344     CC_IFFREE (*elist, int);
1345     CC_IFFREE (*elen, int);
1346     return -1;
1347 
1348 }
1349 
prob_copyedges(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1350 static int prob_copyedges (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1351 {
1352     int ecount;
1353     int ncount;
1354     int rval;
1355     char version;
1356     int nbits;
1357     int i;
1358 
1359     rval = begin_copy (f, f->offsets.edge, t, &t->offsets.edge, CCtsp_Pedges,
1360                        silent);
1361     if (rval == 1) return 0;
1362     else if (rval) return rval;
1363 
1364     rval = copy_char_val (f->f, t->f, (char *) &version);
1365     if (rval) return rval;
1366     switch (version) {
1367     case 1:
1368         rval = copy_int_val (f->f, t->f, &ncount);
1369         if (rval) return rval;
1370         rval = copy_int_val (f->f, t->f, &ecount);
1371         if (rval) return rval;
1372         nbits = CCutil_sbits (ncount);
1373         for (i=0; i<ecount; i++) {
1374             rval = copy_bits (f->f, t->f, 2, nbits);
1375             if (rval) return rval;
1376             rval = copy_ints (f->f, t->f, 1);
1377             if (rval) return rval;
1378         }
1379         break;
1380     default:
1381         fprintf (stderr, "Unknown edges version %ud\n", (unsigned) version);
1382         return 1;
1383     }
1384     return 0;
1385 }
1386 
CCtsp_prob_putcuts(CCtsp_PROB_FILE * p,int ncount,CCtsp_lpcuts * cuts)1387 int CCtsp_prob_putcuts (CCtsp_PROB_FILE *p, int ncount, CCtsp_lpcuts *cuts)
1388 {
1389     int rval;
1390 
1391     if (!p) return 1;
1392 
1393     rval = begin_put (p, &p->offsets.cut, CCtsp_Pcuts);
1394     if (rval) return rval;
1395 
1396     rval = CCtsp_write_cuts (p->f, ncount, cuts, 1);
1397     if (rval) return rval;
1398 
1399     return 0;
1400 }
1401 
CCtsp_prob_getcuts(CCtsp_PROB_FILE * p,int * ncount,CCtsp_lpcuts * cuts,int silent)1402 int CCtsp_prob_getcuts (CCtsp_PROB_FILE *p, int *ncount, CCtsp_lpcuts *cuts,
1403         int silent)
1404 {
1405     int rval;
1406 
1407     if (!p) return -1;
1408 
1409     rval = begin_get (p, p->offsets.cut, CCtsp_Pcuts, silent);
1410     if (rval) return rval;
1411 
1412     rval = CCtsp_read_cuts (p->f, ncount, cuts, 1, 0);
1413     if (rval) return rval;
1414 
1415     return 0;
1416 }
1417 
prob_copycuts(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1418 static int prob_copycuts (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1419 {
1420     int rval;
1421 
1422     rval = begin_copy (f, f->offsets.cut, t, &t->offsets.cut, CCtsp_Pcuts,
1423                        silent);
1424     if (rval == 1) return 0;
1425     else if (rval) return rval;
1426 
1427     rval = CCtsp_copy_cuts (f->f, t->f, 1);
1428     if (rval) return rval;
1429 
1430     return 0;
1431 }
1432 
CCtsp_prob_putwarmstart(CCtsp_PROB_FILE * p,CClp_warmstart * w)1433 int CCtsp_prob_putwarmstart (CCtsp_PROB_FILE *p, CClp_warmstart *w)
1434 {
1435     int rval;
1436 
1437     if (!p) return 1;
1438 
1439     rval = begin_put (p, &p->offsets.warmstart, CCtsp_Pwarmstart);
1440     if (rval) return rval;
1441 
1442     if (CClp_swrite_warmstart (p->f, w)) {
1443         fprintf (stderr, "CClp_swrite_warmstart failed\n");
1444         return 1;
1445     }
1446 
1447     return 0;
1448 }
1449 
CCtsp_prob_getwarmstart(CCtsp_PROB_FILE * p,CClp_warmstart ** w,int silent)1450 int CCtsp_prob_getwarmstart (CCtsp_PROB_FILE *p, CClp_warmstart **w, int silent)
1451 {
1452     int rval;
1453 
1454     if (!p) return -1;
1455 
1456     rval = begin_get (p, p->offsets.warmstart, CCtsp_Pwarmstart, silent);
1457     if (rval) return rval;
1458 
1459 
1460     if (CClp_sread_warmstart (p->f, w)) {
1461         fprintf (stderr, "CClp_sread_warmstart failed\n");;
1462         return -1;
1463     }
1464     return 0;
1465 }
1466 
prob_copywarmstart(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1467 static int prob_copywarmstart (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t,
1468         int silent)
1469 {
1470     CClp_warmstart *w = (CClp_warmstart *) NULL;
1471     int rval;
1472 
1473     rval = begin_copy (f, f->offsets.warmstart, t, &t->offsets.warmstart,
1474                        CCtsp_Pwarmstart, silent);
1475     if (rval == 1) return 0;
1476     else if (rval) return rval;
1477 
1478     rval = CClp_sread_warmstart (f->f, &w);
1479     if (rval) return rval;
1480     rval = CClp_swrite_warmstart (t->f, w);
1481     if (rval) return rval;
1482 
1483     CClp_free_warmstart (&w);
1484 
1485     return 0;
1486 }
1487 
CCtsp_prob_putfulladj(CCtsp_PROB_FILE * p,int ncount,int fullcount,CCtsp_genadj * adj)1488 int CCtsp_prob_putfulladj (CCtsp_PROB_FILE *p, int ncount, int fullcount,
1489             CCtsp_genadj *adj)
1490 {
1491     int i, j;
1492     int rval;
1493     int nbits;
1494 
1495     if (!p) return 1;
1496 
1497     rval = begin_put (p, &p->offsets.fulladj, CCtsp_Pfull);
1498     if (rval) return rval;
1499 
1500     if (CCutil_swrite_char (p->f, PROB_FULLADJ_VERSION)) return 1;
1501 
1502     if (CCutil_swrite_int (p->f, ncount)) return 1;
1503     if (CCutil_swrite_int (p->f, fullcount)) return 1;
1504     nbits = CCutil_sbits (ncount);
1505     for (i = 0; i < ncount; i++) {
1506         if (CCutil_swrite_bits (p->f, adj[i].deg, nbits)) return 1;
1507         for (j = 0; j < adj[i].deg; j++) {
1508             if (CCutil_swrite_bits (p->f, adj[i].list[j].end, nbits)) return 1;
1509             if (CCutil_swrite_int (p->f, adj[i].list[j].len)) return 1;
1510         }
1511     }
1512     return 0;
1513 }
1514 
CCtsp_prob_getfulladj(CCtsp_PROB_FILE * p,int ncount,int * fullcount,CCtsp_genadj ** adj,CCtsp_genadjobj ** adjspace,int silent)1515 int CCtsp_prob_getfulladj (CCtsp_PROB_FILE *p, int ncount, int *fullcount,
1516             CCtsp_genadj **adj, CCtsp_genadjobj **adjspace, int silent)
1517 {
1518     int i, j;
1519     CCtsp_genadj *a;
1520     CCtsp_genadjobj *s;
1521     int rval;
1522     int ncount2;
1523     char version;
1524     int nbits;
1525 
1526     *fullcount = 0;
1527     *adj = (CCtsp_genadj *) NULL;
1528     *adjspace = (CCtsp_genadjobj *) NULL;
1529 
1530     if (!p) return -1;
1531 
1532     rval = begin_get (p, p->offsets.fulladj, CCtsp_Pfull, silent);
1533     if (rval) return rval;
1534 
1535     if (CCutil_sread_char (p->f, &version)) goto FAILURE;
1536     switch (version) {
1537     case 1:
1538         if (CCutil_sread_int (p->f, &ncount2)) goto FAILURE;
1539         if (ncount != ncount2) {
1540             fprintf (stderr, "ncount incorrect in fulladj\n"); goto FAILURE;
1541         }
1542         nbits = CCutil_sbits (ncount2);
1543         if (CCutil_sread_int (p->f, fullcount)) goto FAILURE;
1544         *adjspace = CC_SAFE_MALLOC (*fullcount, CCtsp_genadjobj);
1545         *adj = CC_SAFE_MALLOC (ncount, CCtsp_genadj);
1546         if (!adjspace || !adj) {
1547             fprintf (stderr, "out of memory in CCtsp_prob_getfulladj\n");
1548             goto FAILURE;
1549         }
1550 
1551         s = *adjspace;
1552         a = *adj;
1553         for (i = 0; i < ncount; i++) {
1554             a[i].list = s;
1555             if (CCutil_sread_bits (p->f, &(a[i].deg), nbits)) goto FAILURE;
1556             for (j = 0; j < a[i].deg; j++) {
1557                 if (CCutil_sread_bits (p->f, &(a[i].list[j].end), nbits))
1558                     goto FAILURE;
1559                 if (CCutil_sread_int (p->f, &(a[i].list[j].len)))
1560                     goto FAILURE;
1561             }
1562             s += a[i].deg;
1563         }
1564         break;
1565     default:
1566         fprintf (stderr, "Unknown fulladj version %ud\n", (unsigned) version);
1567         goto FAILURE;
1568     }
1569     return 0;
1570 
1571 FAILURE:
1572     CC_IFFREE (*adjspace, CCtsp_genadjobj);
1573     CC_IFFREE (*adj, CCtsp_genadj);
1574     *fullcount = 0;
1575     return -1;
1576 }
1577 
prob_copyfulladj(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1578 static int prob_copyfulladj (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1579 {
1580     int rval;
1581     int deg;
1582     int ncount;
1583     char version;
1584     int nbits;
1585     int i;
1586     int j;
1587 
1588     rval = begin_copy (f, f->offsets.fulladj, t, &t->offsets.fulladj,
1589                        CCtsp_Pfull, silent);
1590     if (rval == 1) return 0;
1591     else if (rval) return rval;
1592 
1593     rval = copy_char_val (f->f, t->f, (char *) &version);
1594     if (rval) return rval;
1595     switch (version) {
1596     case 1:
1597         rval = copy_int_val (f->f, t->f, &ncount);
1598         if (rval) return rval;
1599         rval = copy_ints (f->f, t->f, 1);
1600         if (rval) return rval;
1601         nbits = CCutil_sbits (ncount);
1602         for (i = 0; i < ncount; i++) {
1603             rval = copy_bits_val (f->f, t->f, &deg, nbits);
1604             if (rval) return rval;
1605             for (j=0; j<deg; j++) {
1606                 rval = copy_bits (f->f, t->f, 1, nbits);
1607                 if (rval) return rval;
1608                 rval = copy_ints (f->f, t->f, 1);
1609                 if (rval) return rval;
1610             }
1611         }
1612         break;
1613     default:
1614         fprintf (stderr, "Unknown fulladj version %ud\n", (unsigned) version);
1615         return -1;
1616     }
1617     return 0;
1618 }
1619 
CCtsp_prob_putfixed(CCtsp_PROB_FILE * p,int ncount,int ecount,int * elist)1620 int CCtsp_prob_putfixed (CCtsp_PROB_FILE *p, int ncount, int ecount,
1621         int *elist)
1622 {
1623     int i;
1624     int rval;
1625     int nbits;
1626 
1627     if (!p) return 1;
1628 
1629     rval = begin_put (p, &p->offsets.fix, CCtsp_Pfixed);
1630     if (rval) return rval;
1631 
1632     if (CCutil_swrite_char (p->f, PROB_FIXED_VERSION)) return 1;
1633 
1634     if (CCutil_swrite_int (p->f, ncount)) return 1;
1635     if (CCutil_swrite_int (p->f, ecount)) return 1;
1636 
1637     nbits = CCutil_sbits (ncount);
1638 
1639     for (i = 0; i < 2*ecount; i++) {
1640         if (CCutil_swrite_bits (p->f, elist[i], nbits)) return 1;
1641     }
1642 
1643     return 0;
1644 }
1645 
CCtsp_prob_getfixed(CCtsp_PROB_FILE * p,int ncount,int * ecount,int ** elist,int silent)1646 int CCtsp_prob_getfixed (CCtsp_PROB_FILE *p, int ncount, int *ecount,
1647         int **elist, int silent)
1648 {
1649     int i;
1650     int rval;
1651     int ncount2;
1652     char version;
1653     int nbits;
1654 
1655     *ecount = 0;
1656     *elist = (int *) NULL;
1657     if (!p) return -1;
1658 
1659     rval = begin_get (p, p->offsets.fix, CCtsp_Pfixed, silent);
1660     if (rval) return rval;
1661 
1662     if (CCutil_sread_char (p->f, &version)) goto FAILURE;
1663     switch (version) {
1664     case 1:
1665         if (CCutil_sread_int (p->f, &ncount2)) goto FAILURE;
1666         if (ncount != ncount2) {
1667             fprintf (stderr, "wrong ncount in fixed edges\n");
1668             goto FAILURE;
1669         }
1670         if (CCutil_sread_int (p->f, ecount)) goto FAILURE;
1671 
1672         if (*ecount) {
1673             *elist = CC_SAFE_MALLOC (2*(*ecount), int);
1674             if (!(*elist)) {
1675                 fprintf (stderr, "out of memory in CCtsp_prob_getfixed\n");
1676                 goto FAILURE;
1677             }
1678             nbits = CCutil_sbits (ncount);
1679             for (i = 0; i < 2*(*ecount); i++) {
1680                 if (CCutil_sread_bits (p->f, (&((*elist)[i])), nbits))
1681                     goto FAILURE;
1682             }
1683         }
1684         break;
1685     default:
1686         fprintf (stderr, "Unknown fixed version %ud\n", (unsigned) version);
1687         goto FAILURE;
1688     }
1689     return 0;
1690 
1691 FAILURE:
1692     CC_IFFREE (*elist, int);
1693     *ecount = 0;
1694     return -1;
1695 }
1696 
prob_copyfixed(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1697 static int prob_copyfixed (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1698 {
1699     int ecount;
1700     int ncount;
1701     int nbits;
1702     char version;
1703     int rval;
1704 
1705     rval = begin_copy (f, f->offsets.fix, t, &t->offsets.fix, CCtsp_Pfixed,
1706                        silent);
1707     if (rval == 1) return 0;
1708     else if (rval) return rval;
1709 
1710     rval = copy_char_val (f->f, t->f, (char *) &version);
1711     if (rval) return rval;
1712     switch (version) {
1713     case 1:
1714         rval = copy_int_val (f->f, t->f, &ncount);
1715         if (rval) return rval;
1716         rval = copy_int_val (f->f, t->f, &ecount);
1717         if (rval) return rval;
1718         nbits = CCutil_sbits (ncount);
1719         rval = copy_bits (f->f, t->f, ecount*2, nbits);
1720         if (rval) return rval;
1721         break;
1722     default:
1723         fprintf (stderr, "Unknown fixed version %ud\n", (unsigned) version);
1724         return -1;
1725     }
1726     return 0;
1727 }
1728 
CCtsp_prob_putexactdual(CCtsp_PROB_FILE * p,CCtsp_bigdual * d,int ncount)1729 int CCtsp_prob_putexactdual (CCtsp_PROB_FILE *p, CCtsp_bigdual *d, int ncount)
1730 {
1731     int i;
1732     int rval;
1733 
1734     if (!p) return 1;
1735 
1736     rval = begin_put (p, &p->offsets.exactdual, CCtsp_Pdual);
1737     if (rval) return rval;
1738 
1739     if (CCutil_swrite_char (p->f, PROB_EXACTDUAL_VERSION)) return 1;
1740     if (CCutil_swrite_int (p->f, ncount)) return 1;
1741     if (CCutil_swrite_int (p->f, d->cutcount)) return 1;
1742 
1743     for (i = 0; i < ncount; i++) {
1744         if (CCbigguy_swrite (p->f, d->node_pi[i])) return 1;
1745     }
1746     for (i = 0; i < d->cutcount; i++) {
1747         if (CCbigguy_swrite (p->f, d->cut_pi[i])) return 1;
1748     }
1749 
1750     return 0;
1751 }
1752 
1753 
CCtsp_prob_getexactdual(CCtsp_PROB_FILE * p,int ncount,CCtsp_bigdual ** d,int silent)1754 int CCtsp_prob_getexactdual (CCtsp_PROB_FILE *p, int ncount, CCtsp_bigdual **d,
1755         int silent)
1756 {
1757     CCtsp_bigdual *rd;
1758     int i;
1759     int rval;
1760     int ncount2;
1761     char version;
1762 
1763     *d = (CCtsp_bigdual *) NULL;
1764 
1765     if (!p) return -1;
1766 
1767     rval = begin_get (p, p->offsets.exactdual, CCtsp_Pdual, silent);
1768     if (rval) return rval;
1769 
1770     if (CCutil_sread_char (p->f, &version)) return -1;
1771     switch (version) {
1772     case 1:
1773         if (CCutil_sread_int (p->f, &ncount2)) goto FAILURE;
1774         if (ncount != ncount2) {
1775             fprintf (stderr, "wrong ncount in exact dual\n"); goto FAILURE;
1776         }
1777 
1778         *d = CC_SAFE_MALLOC (1, CCtsp_bigdual);
1779         if (!(*d)) {
1780             fprintf (stderr, "out of memory in CCtsp_prob_getexactdual\n");
1781             goto FAILURE;
1782         }
1783         rd = *d;
1784         rd->cutcount = 0;
1785         rd->node_pi = (CCbigguy *) NULL;
1786         rd->cut_pi = (CCbigguy *) NULL;
1787 
1788         if (CCutil_sread_int (p->f, &(rd->cutcount))) goto FAILURE;
1789 
1790         rd->node_pi = CC_SAFE_MALLOC (ncount, CCbigguy);
1791         if (!rd->node_pi) {
1792             fprintf (stderr, "out of memory in CCtsp_prob_getexactdual\n");
1793             goto FAILURE;
1794         }
1795         for (i = 0; i < ncount; i++) {
1796             if (CCbigguy_sread (p->f, &(rd->node_pi[i]))) goto FAILURE;
1797         }
1798         if (rd->cutcount) {
1799             rd->cut_pi = CC_SAFE_MALLOC (rd->cutcount, CCbigguy);
1800             if (!rd->cut_pi) {
1801                 fprintf (stderr, "out of memory in CCtsp_prob_getexactdual\n");
1802                 goto FAILURE;
1803             }
1804             for (i = 0; i < rd->cutcount; i++) {
1805                 if (CCbigguy_sread (p->f, &(rd->cut_pi[i]))) goto FAILURE;
1806             }
1807         }
1808         break;
1809     default:
1810         fprintf (stderr, "Unknown exact dual version %ud\n", (unsigned) version);
1811         goto FAILURE;
1812     }
1813     return 0;
1814 
1815 FAILURE:
1816     if (*d) {
1817         CC_IFFREE ((*d)->node_pi, CCbigguy);
1818         CC_IFFREE ((*d)->cut_pi, CCbigguy);
1819         CC_FREE (*d, CCtsp_bigdual);
1820     }
1821     return -1;
1822 }
1823 
prob_copyexactdual(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1824 static int prob_copyexactdual (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t,
1825        int silent)
1826 {
1827     int rval;
1828     int ncount;
1829     int cutcount;
1830     char version;
1831 
1832     rval = begin_copy (f, f->offsets.exactdual, t, &t->offsets.exactdual,
1833                        CCtsp_Pdual, silent);
1834     if (rval == 1) return 0;
1835     else if (rval) return rval;
1836 
1837     rval = copy_char_val (f->f, t->f, (char *) &version);
1838     if (rval) return rval;
1839     switch (version) {
1840     case 1:
1841         rval = copy_int_val (f->f, t->f, &ncount);
1842         if (rval) return rval;
1843         rval = copy_int_val (f->f, t->f, &cutcount);
1844         if (rval) return rval;
1845         rval = copy_bigguys (f->f, t->f, ncount);
1846         if (rval) return rval;
1847         rval = copy_bigguys (f->f, t->f, cutcount);
1848         if (rval) return rval;
1849         return 0;
1850     default:
1851         fprintf (stderr, "Unknown exact dual version %ud\n", (unsigned) version);
1852         return -1;
1853     }
1854 }
1855 
1856 
CCtsp_prob_puthistory(CCtsp_PROB_FILE * p,int depth,CCtsp_branchobj * history)1857 int CCtsp_prob_puthistory (CCtsp_PROB_FILE *p, int depth,
1858         CCtsp_branchobj *history)
1859 {
1860     int i, j;
1861     CCtsp_lpclique *c;
1862     int rval;
1863 
1864     if (!p) return 1;
1865 
1866     rval = begin_put (p, &p->offsets.history, CCtsp_Phistory);
1867     if (rval) return rval;
1868 
1869     if (CCutil_swrite_char (p->f, PROB_HISTORY_VERSION)) return 1;
1870     if (CCutil_swrite_int (p->f, depth)) return 1;
1871 
1872     for (i = 0; i < depth; i++) {
1873         if (CCutil_swrite_int (p->f, history[i].depth)) return 1;
1874         if (CCutil_swrite_int (p->f, history[i].rhs)) return 1;
1875         if (CCutil_swrite_int (p->f, history[i].ends[0])) return 1;
1876         if (CCutil_swrite_int (p->f, history[i].ends[1])) return 1;
1877         if (history[i].clique) {
1878             c = history[i].clique;
1879             if (CCutil_swrite_int (p->f, c->segcount)) return 1;
1880             for (j = 0; j < c->segcount; j++) {
1881                 if (CCutil_swrite_int (p->f, c->nodes[j].lo)) return 1;
1882                 if (CCutil_swrite_int (p->f, c->nodes[j].hi)) return 1;
1883             }
1884         } else {
1885             if (CCutil_swrite_int (p->f, 0)) return 1;
1886         }
1887         if (CCutil_swrite_char (p->f, history[i].sense)) return 1;
1888     }
1889 
1890     return 0;
1891 }
1892 
CCtsp_prob_gethistory(CCtsp_PROB_FILE * p,int * depth,CCtsp_branchobj ** history,int silent)1893 int CCtsp_prob_gethistory (CCtsp_PROB_FILE *p, int *depth,
1894         CCtsp_branchobj **history, int silent)
1895 {
1896     int i, j, nseg, rval;
1897     CCtsp_lpclique *c = (CCtsp_lpclique *) NULL;
1898     int *slist  = (int *) NULL;
1899     char version;
1900 
1901     *depth = 0;
1902     *history = (CCtsp_branchobj *) NULL;
1903     if (!p) return -1;
1904 
1905     rval = begin_get (p, p->offsets.history, CCtsp_Phistory, silent);
1906     if (rval) return rval;
1907 
1908     if (CCutil_sread_char (p->f, &version)) goto FAILURE;
1909     switch (version) {
1910     case 1:
1911         if (CCutil_sread_int (p->f, depth)) goto FAILURE;
1912 
1913         if (*depth) {
1914             *history = CC_SAFE_MALLOC (*depth, CCtsp_branchobj);
1915             if (!(*history)) {
1916                 fprintf (stderr, "out of memory in CCtsp_prob_gethistory\n");
1917                 goto FAILURE;
1918             }
1919             for (i = 0; i < (*depth); i++) {
1920                 if (CCutil_sread_int (p->f, &((*history)[i].depth)))
1921                     goto FAILURE;
1922                 if (CCutil_sread_int (p->f, &((*history)[i].rhs)))
1923                     goto FAILURE;
1924                 if (CCutil_sread_int (p->f, &((*history)[i].ends[0])))
1925                     goto FAILURE;
1926                 if (CCutil_sread_int (p->f, &((*history)[i].ends[1])))
1927                     goto FAILURE;
1928                 if (CCutil_sread_int (p->f, &nseg))
1929                     goto FAILURE;
1930                 if (nseg) {
1931                     slist = CC_SAFE_MALLOC (2*nseg, int);
1932                     if (!slist) goto FAILURE;
1933                     for (j = 0; j < nseg; j++) {
1934                         if (CCutil_sread_int (p->f, &slist[2*j]))
1935                             goto FAILURE;
1936                         if (CCutil_sread_int (p->f, &slist[2*j+1]))
1937                             goto FAILURE;
1938                     }
1939                     c = CC_SAFE_MALLOC (1, CCtsp_lpclique);
1940                     if (!c) goto FAILURE;
1941                     rval = CCtsp_seglist_to_lpclique (nseg, slist, c);
1942                     if (rval) {
1943                         fprintf (stderr, "CCtsp_seglist_to_lpclique failed\n");
1944                         CC_FREE (c, CCtsp_lpclique);
1945                         goto FAILURE;
1946                     }
1947                     (*history)[i].clique = c;
1948                 } else {
1949                     (*history)[i].clique = (CCtsp_lpclique *) NULL;
1950                 }
1951                 if (CCutil_sread_char (p->f, &((*history)[i].sense)))
1952                     return 1;
1953             }
1954         }
1955         break;
1956     default:
1957         fprintf (stderr, "Unknown history version %ud\n", (unsigned) version);
1958         goto FAILURE;
1959     }
1960     return 0;
1961 
1962 FAILURE:
1963     CC_IFFREE (slist, int);
1964     CC_IFFREE (*history, CCtsp_branchobj);
1965     *depth = 0;
1966     return -1;
1967 }
1968 
prob_copyhistory(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,int silent)1969 static int prob_copyhistory (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t, int silent)
1970 {
1971     int rval;
1972     int depth;
1973     int segcount;
1974     int i;
1975     char version;
1976 
1977     rval = begin_copy (f, f->offsets.history, t, &t->offsets.history,
1978                        CCtsp_Phistory, silent);
1979     if (rval == 1) return 0;
1980     else if (rval) return rval;
1981 
1982     rval = copy_char_val (f->f, t->f, (char *) &version);
1983     if (rval) return rval;
1984     switch (version) {
1985     case 1:
1986         rval = copy_int_val (f->f, t->f, &depth);
1987         if (rval) return rval;
1988 
1989         for (i=0; i<depth; i++) {
1990             rval = copy_ints (f->f, t->f, 4);
1991             if (rval) return rval;
1992             rval = copy_int_val (f->f, t->f, &segcount);
1993             if (rval) return rval;
1994             if (segcount != 0) {
1995                 rval = copy_ints (f->f, t->f, 2*segcount);
1996                 if (rval) return rval;
1997             }
1998             rval = copy_chars (f->f, t->f, 1);
1999             if (rval) return rval;
2000         }
2001         break;
2002     default:
2003         fprintf (stderr, "Unknown history version %ud\n", (unsigned) version);
2004         return -1;
2005     }
2006     return 0;
2007 }
2008 
CCtsp_prob_copy_section(CCtsp_PROB_FILE * f,CCtsp_PROB_FILE * t,char section,int silent)2009 int CCtsp_prob_copy_section (CCtsp_PROB_FILE *f, CCtsp_PROB_FILE *t,
2010         char section, int silent)
2011 {
2012     switch (section) {
2013       case CCtsp_Pheader:
2014         return prob_copyheader (f, t);
2015       case CCtsp_Ptour:
2016         return prob_copytour (f, t, silent);
2017       case CCtsp_Pedges:
2018         return prob_copyedges (f, t, silent);
2019       case CCtsp_Pcuts:
2020         return prob_copycuts (f, t, silent);
2021       case CCtsp_Pwarmstart:
2022         return prob_copywarmstart (f, t, silent);
2023       case CCtsp_Pfull:
2024         return prob_copyfulladj (f, t, silent);
2025       case CCtsp_Pfixed:
2026         return prob_copyfixed (f, t, silent);
2027       case CCtsp_Pdual:
2028         return prob_copyexactdual (f, t, silent);
2029       case CCtsp_Phistory:
2030         return prob_copyhistory (f, t, silent);
2031       default:
2032         fprintf (stderr, "Invalid section %c in CCtsp_prob_copy_section\n",
2033                  section);
2034         return 1;
2035     }
2036 }
2037 
remote_name(const char * f)2038 static int remote_name (const char *f)
2039 {
2040     return (CCutil_strchr_c (f, ':') != (const char *) NULL);
2041 }
2042 
split_name(const char * f,char * hostname,size_t hlen,char * probname,size_t plen)2043 static int split_name (const char *f, char *hostname, size_t hlen,
2044         char *probname, size_t plen)
2045 {
2046     size_t len;
2047     const char *p;
2048 
2049     p = CCutil_strchr_c (f, ':');
2050     if (p == (const char *) NULL) {
2051         fprintf (stderr, "non-net name in split_name\n");
2052         return 1;
2053     }
2054 
2055     len = p - f;
2056     if (len+1 > hlen) {
2057         fprintf (stderr, "hostname too long in split_name\n");
2058         return 1;
2059     }
2060 
2061     strncpy (hostname, f, len);
2062     hostname[len] = '\0';
2063 
2064     len = strlen (p+1);
2065     if (len+1 > plen) {
2066         fprintf (stderr, "filename too long in split_name\n");
2067         return 1;
2068     }
2069 
2070     strncpy (probname, p+1, len);
2071     probname[len] = '\0';
2072     return 0;
2073 }
2074 
copy_ints(CC_SFILE * f,CC_SFILE * t,int n)2075 static int copy_ints (CC_SFILE *f, CC_SFILE *t, int n)
2076 {
2077     int rval;
2078     int i;
2079     int x;
2080 
2081     for (i=0; i<n; i++) {
2082         rval = CCutil_sread_int (f, &x);
2083         if (rval) return rval;
2084         rval = CCutil_swrite_int (t, x);
2085         if (rval) return rval;
2086     }
2087     return 0;
2088 }
2089 
copy_int_val(CC_SFILE * f,CC_SFILE * t,int * n)2090 static int copy_int_val (CC_SFILE *f, CC_SFILE *t, int *n)
2091 {
2092     int rval;
2093 
2094     rval = CCutil_sread_int (f, n);
2095     if (rval) return rval;
2096     rval = CCutil_swrite_int (t, *n);
2097     if (rval) return rval;
2098     return 0;
2099 }
2100 
copy_bits(CC_SFILE * f,CC_SFILE * t,int n,int nbits)2101 static int copy_bits (CC_SFILE *f, CC_SFILE *t, int n, int nbits)
2102 {
2103     int rval;
2104     int i;
2105     int x;
2106 
2107     for (i=0; i<n; i++) {
2108         rval = CCutil_sread_bits (f, &x, nbits);
2109         if (rval) return rval;
2110         rval = CCutil_swrite_bits (t, x, nbits);
2111         if (rval) return rval;
2112     }
2113     return 0;
2114 }
2115 
copy_bits_val(CC_SFILE * f,CC_SFILE * t,int * n,int nbits)2116 static int copy_bits_val (CC_SFILE *f, CC_SFILE *t, int *n, int nbits)
2117 {
2118     int rval;
2119 
2120     rval = CCutil_sread_bits (f, n, nbits);
2121     if (rval) return rval;
2122     rval = CCutil_swrite_bits (t, *n, nbits);
2123     if (rval) return rval;
2124     return 0;
2125 }
2126 
copy_chars(CC_SFILE * f,CC_SFILE * t,int n)2127 static int copy_chars (CC_SFILE *f, CC_SFILE *t, int n)
2128 {
2129     int rval;
2130     int i;
2131     char x;
2132 
2133     for (i=0; i<n; i++) {
2134         rval = CCutil_sread_char (f, &x);
2135         if (rval) return rval;
2136         rval = CCutil_swrite_char (t, x);
2137         if (rval) return rval;
2138     }
2139     return 0;
2140 }
2141 
copy_char_val(CC_SFILE * f,CC_SFILE * t,char * n)2142 static int copy_char_val (CC_SFILE *f, CC_SFILE *t, char *n)
2143 {
2144     int rval;
2145 
2146     rval = CCutil_sread_char (f, n);
2147     if (rval) return rval;
2148     rval = CCutil_swrite_char (t, *n);
2149     if (rval) return rval;
2150     return 0;
2151 }
2152 
copy_bigguys(CC_SFILE * f,CC_SFILE * t,int n)2153 static int copy_bigguys (CC_SFILE *f, CC_SFILE *t, int n)
2154 {
2155     int rval;
2156     int i;
2157     CCbigguy x;
2158 
2159     for (i=0; i<n; i++) {
2160         rval = CCbigguy_sread (f, &x);
2161         if (rval) return rval;
2162         rval = CCbigguy_swrite (t, x);
2163         if (rval) return rval;
2164     }
2165     return 0;
2166 }
2167 
CCtsp_problabel(const char * probloc)2168 char *CCtsp_problabel (const char *probloc)
2169 {
2170     const char *p;
2171     const char *problabel = probloc;
2172     char *probcopy = (char *) NULL;
2173     char *p2;
2174 
2175     p = CCutil_strrchr_c (problabel, ':');
2176     if (p != (const char *) NULL) problabel = p+1;
2177     p = CCutil_strrchr_c (problabel, '/');
2178     if (p != (const char *) NULL) problabel = p+1;
2179     probcopy = CCutil_strdup (problabel);
2180     if (probcopy == (char *) NULL) return (char *) NULL;
2181     p2 = CCutil_strchr (probcopy, '.');
2182     if (p2 != (char *) NULL) *p2 = '\0';
2183     return probcopy;
2184 }
2185