~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to routines/metanet/loadg.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#if !(defined __MSC__) && !(defined __ABSC__)
 
3
#ifdef __FreeBSD__
 
4
#include <sys/types.h>
 
5
#endif
 
6
#include <dirent.h>
 
7
#endif
 
8
#include <string.h>
 
9
#ifdef __STDC__
 
10
#include <stdlib.h>
 
11
#else
 
12
#include <malloc.h>
 
13
#endif
 
14
 
 
15
#if (defined __MSC__) || (defined __ABSC__)
 
16
/** only used for x=dir[1024] **/
 
17
#ifndef __ABSC__
 
18
#define  getwd(x) _getcwd(x,1024)
 
19
#else
 
20
#define  getwd(x) getcwd_(x,1024)
 
21
#endif
 
22
#endif
 
23
 
 
24
#include "mysearch.h"
 
25
#include "../machine.h"
 
26
#include "defs.h"
 
27
 
 
28
#define MAXNAM 80
 
29
 
 
30
extern double atof();
 
31
extern char* my_basename();
 
32
extern int CheckGraphName();
 
33
extern void cerro();
 
34
extern char* dirname();
 
35
extern char *StripGraph();
 
36
 
 
37
typedef int (*PF)();
 
38
 
 
39
static int CompString(s1, s2)
 
40
char **s1, **s2;
 
41
{
 
42
  return strcmp((char*)*s1,(char*)*s2);
 
43
}
 
44
 
 
45
static char description[2*MAXNAM];
 
46
 
 
47
void C2F(loadg)(path,lpath,name,lname,directed,n,tail,head,
 
48
  node_name,node_type,node_x,node_y,node_color,node_diam,node_border,
 
49
  node_font_size,node_demand,
 
50
  edge_name,edge_color,edge_width,edge_hi_width,edge_font_size,
 
51
  edge_length,edge_cost,edge_min_cap,edge_max_cap,edge_q_weight,edge_q_orig,
 
52
  edge_weight,
 
53
  default_node_diam,default_node_border,default_edge_width,
 
54
  default_edge_hi_width,default_font_size,
 
55
  ndim,ma)
 
56
char *path; int *lpath,*directed,*n,**tail,**head;
 
57
char **name; int *lname;
 
58
char ***node_name; 
 
59
int **node_type,**node_x,**node_y,**node_color,**node_diam,**node_border;
 
60
int **node_font_size;
 
61
double **node_demand;
 
62
char ***edge_name;
 
63
int **edge_color,**edge_width,**edge_hi_width,**edge_font_size;
 
64
double **edge_length,**edge_cost,**edge_min_cap,**edge_max_cap;
 
65
double **edge_q_weight,**edge_q_orig,**edge_weight;
 
66
int *default_node_diam,*default_node_border,*default_edge_width;
 
67
int *default_edge_hi_width,*default_font_size;
 
68
int *ndim,*ma;
 
69
{
 
70
  /** #ifdef __MSC__
 
71
  return;
 
72
  #else  **/
 
73
  FILE *fg;
 
74
#if !(defined __MSC__) && !(defined __ABSC__)
 
75
  DIR *dirp;
 
76
#endif
 
77
  char fname[2 * MAXNAM];
 
78
  char line[5 * MAXNAM];
 
79
  char strname[MAXNAM], head_name[MAXNAM], tail_name[MAXNAM];
 
80
  int isize,dsize;
 
81
  int i,s;
 
82
  ENTRY node,*found;
 
83
  char dir[1024];
 
84
  char *pname;
 
85
  char **lar;
 
86
 
 
87
  path[*lpath] = '\0';
 
88
#if !(defined __MSC__) && !(defined __ABSC__)
 
89
  if ((dirp=opendir(path)) != NULL) {
 
90
    sprintf(description,"\"%s\" is a directory",path);
 
91
    cerro(description);
 
92
    closedir(dirp);
 
93
    return;
 
94
  }
 
95
#endif
 
96
  if (dirname(path) == NULL) getwd(dir);
 
97
  else strcpy(dir,dirname(path));
 
98
#if !(defined __MSC__) && !(defined __ABSC__)
 
99
  if ((dirp=opendir(dir)) == NULL) {
 
100
    sprintf(description,"Directory \"%s\" does not exist",dir);
 
101
    cerro(description);
 
102
    return;
 
103
  }
 
104
  closedir(dirp);
 
105
#endif
 
106
  pname = StripGraph(my_basename(path));
 
107
 
 
108
  *lname = strlen(pname);
 
109
 
 
110
  if ((*name = (char *)malloc((unsigned)sizeof(char)*(*lname + 1)))
 
111
      == NULL) {
 
112
    cerro("Running out of memory");
 
113
    return;
 
114
  }
 
115
  strcpy(*name,pname);
 
116
#if !(defined __MSC__) && !(defined __ABSC__)
 
117
  if(!CheckGraphName(*name,dir)) {
 
118
    sprintf(description,"Graph file \"%s/%s.graph\" does not exist",dir,*name);
 
119
    cerro(description);
 
120
    return;
 
121
  }
 
122
#endif  
 
123
  isize = sizeof(int);
 
124
  dsize = sizeof(double);
 
125
 
 
126
  strcpy(fname,dir);
 
127
  strcat(fname,"/");
 
128
  strcat(fname,*name);
 
129
  strcat(fname,".graph");
 
130
  fg = fopen(fname,"r");
 
131
  if (fg == 0) {
 
132
    sprintf(description,"Unable to open file \"%s/%s.graph\"",dir,*name);
 
133
    cerro(description);
 
134
    return;
 
135
  }
 
136
  /* read graph from graph file */
 
137
  
 
138
  fgets(line,5 * MAXNAM,fg);
 
139
  fgets(line,5 * MAXNAM,fg);
 
140
  *default_node_diam = NODEDIAM;
 
141
  *default_node_border = NODEW;
 
142
  *default_edge_width = ARCW;
 
143
  *default_edge_hi_width = ARCH;
 
144
  *default_font_size = DRAWFONTSIZE;
 
145
  sscanf(line,"%d %d %d %d %d %d",directed,default_node_diam,
 
146
         default_node_border,default_edge_width,
 
147
         default_edge_hi_width,default_font_size);
 
148
  if (*default_node_diam == 0) *default_node_diam = NODEDIAM;
 
149
  if (*default_node_border == 0) *default_node_border = NODEW;
 
150
  if (*default_edge_width == 0) *default_edge_width = ARCW;
 
151
  if (*default_edge_hi_width == 0) *default_edge_hi_width = ARCH;
 
152
  if (*default_font_size == 0) *default_font_size = DRAWFONTSIZE;
 
153
  fgets(line,5 * MAXNAM,fg);
 
154
  fgets(line,5 * MAXNAM,fg);
 
155
  sscanf(line,"%d",ma);
 
156
  fgets(line,5 * MAXNAM,fg);
 
157
  fgets(line,5 * MAXNAM,fg);
 
158
  sscanf(line,"%d",ndim);
 
159
  *n = *ndim;
 
160
  fgets(line,5 * MAXNAM,fg);
 
161
  fgets(line,5 * MAXNAM,fg);
 
162
  fgets(line,5 * MAXNAM,fg);
 
163
  fgets(line,5 * MAXNAM,fg);
 
164
  fgets(line,5 * MAXNAM,fg);
 
165
  
 
166
  /* alloc memory for nodes */
 
167
  s = sizeof(char *) * *ndim;
 
168
  if ((*node_name = (char **)malloc(s)) == NULL) {
 
169
    cerro("Running out of memory");
 
170
    return;
 
171
  }
 
172
  s = isize * *ndim;
 
173
  if ((*node_type = (int *)malloc(s)) == NULL) {
 
174
    cerro("Running out of memory");
 
175
    return;
 
176
  }
 
177
    if ((*node_x = (int *)malloc(s)) == NULL) {
 
178
    cerro("Running out of memory");
 
179
    return;
 
180
  }
 
181
  if ((*node_y = (int *)malloc(s)) == NULL) {
 
182
    cerro("Running out of memory");
 
183
    return;
 
184
  }
 
185
  if ((*node_color = (int *)malloc(s)) == NULL) {
 
186
    cerro("Running out of memory");
 
187
    return;
 
188
  }
 
189
  if ((*node_diam = (int *)malloc(s)) == NULL) {
 
190
    cerro("Running out of memory");
 
191
    return;
 
192
  }
 
193
  if ((*node_border = (int *)malloc(s)) == NULL) {
 
194
    cerro("Running out of memory");
 
195
    return;
 
196
  }
 
197
  if ((*node_font_size = (int *)malloc(s)) == NULL) {
 
198
    cerro("Running out of memory");
 
199
    return;
 
200
  }
 
201
  s = dsize * *ndim;
 
202
  if ((*node_demand = (double *)malloc(s)) == NULL) {
 
203
    cerro("Running out of memory");
 
204
    return;
 
205
  }
 
206
 
 
207
  /* alloc memory for edges */
 
208
  s = sizeof(char *) * *ma;
 
209
  if ((*edge_name = (char **)malloc(s)) == NULL) {
 
210
    cerro("Running out of memory");
 
211
    return;
 
212
  }
 
213
  s = isize * *ma;
 
214
  if ((*head = (int *)malloc(s)) == NULL) {
 
215
    cerro("Running out of memory");
 
216
    return;
 
217
  }
 
218
  if ((*tail = (int *)malloc(s)) == NULL) {
 
219
    cerro("Running out of memory");
 
220
    return;
 
221
  } 
 
222
  if ((*edge_color = (int *)malloc(s)) == NULL) {
 
223
    cerro("Running out of memory");
 
224
    return;
 
225
  }
 
226
  if ((*edge_width = (int *)malloc(s)) == NULL) {
 
227
    cerro("Running out of memory");
 
228
    return;
 
229
  }
 
230
  if ((*edge_hi_width = (int *)malloc(s)) == NULL) {
 
231
    cerro("Running out of memory");
 
232
    return;
 
233
  }
 
234
  if ((*edge_font_size = (int *)malloc(s)) == NULL) {
 
235
    cerro("Running out of memory");
 
236
    return;
 
237
  }
 
238
  s = dsize * *ma;
 
239
  if ((*edge_length = (double *)malloc(s)) == NULL) {
 
240
    cerro("Running out of memory");
 
241
    return;
 
242
  }
 
243
  if ((*edge_cost = (double *)malloc(s)) == NULL) {
 
244
    cerro("Running out of memory");
 
245
    return;
 
246
  }
 
247
  if ((*edge_min_cap = (double *)malloc(s)) == NULL) {
 
248
    cerro("Running out of memory");
 
249
    return;
 
250
  }
 
251
  if ((*edge_max_cap = (double *)malloc(s)) == NULL) {
 
252
    cerro("Running out of memory");
 
253
    return;
 
254
  }
 
255
  if ((*edge_q_weight = (double *)malloc(s)) == NULL) {
 
256
    cerro("Running out of memory");
 
257
    return;
 
258
  }
 
259
  if ((*edge_q_orig = (double *)malloc(s)) == NULL) {
 
260
    cerro("Running out of memory");
 
261
    return;
 
262
  }
 
263
  if ((*edge_weight = (double *)malloc(s)) == NULL) {
 
264
    cerro("Running out of memory");
 
265
    return;
 
266
  }
 
267
 
 
268
  /* jump to node description */
 
269
  for (i = 0; i < 2 * *ma; i++)
 
270
    fgets(line,5 * MAXNAM,fg);
 
271
 
 
272
  fgets(line,5 * MAXNAM,fg);
 
273
  fgets(line,5 * MAXNAM,fg);
 
274
  fgets(line,5 * MAXNAM,fg);
 
275
  fgets(line,5 * MAXNAM,fg);
 
276
  fgets(line,5 * MAXNAM,fg);
 
277
  fgets(line,5 * MAXNAM,fg);
 
278
 
 
279
  myhcreate(*ndim);
 
280
  for (i = 0; i < *ndim; i++) {
 
281
    fgets(line,5 * MAXNAM,fg);
 
282
    (*node_type)[i] = 0;
 
283
    sscanf(line,"%s %d",strname,&((*node_type)[i]));
 
284
    if (((*node_name)[i] = (char *)malloc(strlen(strname)+1)) == NULL) {
 
285
      cerro("Running out of memory");
 
286
      return;
 
287
    }
 
288
    strcpy((*node_name)[i],strname);
 
289
    fgets(line,5 * MAXNAM,fg);
 
290
    (*node_x)[i] = 0;
 
291
    (*node_y)[i] = 0;
 
292
    (*node_color)[i] = 0;
 
293
    (*node_diam)[i] = 0;
 
294
    (*node_border)[i] = 0;
 
295
    (*node_font_size)[i] = 0;
 
296
    sscanf(line,"%d %d %d %d %d %d",
 
297
           &((*node_x)[i]),&((*node_y)[i]),&((*node_color)[i]),
 
298
           &((*node_diam)[i]),&((*node_border)[i]),&((*node_font_size)[i]));
 
299
    fgets(line,5 * MAXNAM,fg);
 
300
    sscanf(line,"%le",&((*node_demand)[i]));
 
301
    if ((node.key = (char *)malloc(strlen(strname)+1)) == NULL) {
 
302
      cerro("Running out of memory");
 
303
      return;
 
304
    }
 
305
    strcpy(node.key,strname);
 
306
    sprintf(strname,"%d",i+1);
 
307
    if ((node.data = (char *)malloc(strlen(strname)+1)) == NULL) {
 
308
      cerro("Running out of memory");
 
309
      return;
 
310
    }   
 
311
    strcpy(node.data,strname);
 
312
    myhsearch(node,ENTER);
 
313
  }
 
314
 
 
315
  /* check uniqueness of node names */
 
316
  if (*ndim != 1) {
 
317
    if ((lar = (char **)malloc(sizeof(char *) * *ndim)) == NULL) {
 
318
      cerro("Running out of memory");
 
319
      return;
 
320
    }
 
321
    for (i = 0; i < *ndim; i++) lar[i] = (*node_name)[i];
 
322
    qsort((char*)lar,*ndim,sizeof(char*),(PF)CompString);
 
323
    for (i = 0; i < *ndim - 1; i++) {
 
324
      if (!strcmp(lar[i],lar[i+1])) {
 
325
        sprintf(description,
 
326
                "Bad graph file. Node \"%s\" is duplicated",lar[i]);
 
327
        cerro(description);
 
328
        free(lar);
 
329
        return;
 
330
      }
 
331
    }
 
332
    if (!strcmp(lar[*ndim - 2],lar[*ndim - 1])) {
 
333
      sprintf(description,
 
334
              "Bad graph file. Node \"%s\" is duplicated",lar[*ndim - 2]);
 
335
      cerro(description);
 
336
      free(lar);
 
337
      return;
 
338
    }
 
339
    free(lar);
 
340
  }
 
341
 
 
342
   /* rewind and go to arc description */
 
343
  rewind(fg);
 
344
  for (i = 0; i < 11; i++)
 
345
    fgets(line,5 * MAXNAM,fg);
 
346
  for (i = 0; i < *ma; i++) {
 
347
  fgets(line,5 * MAXNAM,fg);
 
348
  (*edge_color)[i] = 0;
 
349
  (*edge_width)[i] = 0;
 
350
  (*edge_hi_width)[i] = 0;
 
351
  (*edge_font_size)[i] = 0;
 
352
  sscanf(line,"%s %s %s %d %d %d %d\n",strname,
 
353
         tail_name,head_name,
 
354
         &((*edge_color)[i]),&((*edge_width)[i]),&((*edge_hi_width)[i]),
 
355
         &((*edge_font_size)[i]));
 
356
    if (((*edge_name)[i] = (char *)malloc(strlen(strname)+1)) == NULL) {
 
357
      cerro("Running out of memory");
 
358
      return;
 
359
    }
 
360
  strcpy((*edge_name)[i],strname);
 
361
 
 
362
  if ((node.key = (char *)malloc(strlen(head_name)+1)) == NULL) {
 
363
    cerro("Running out of memory");
 
364
    return;
 
365
  }
 
366
  strcpy(node.key,head_name);
 
367
  found = myhsearch(node,FIND);
 
368
  if (found == NULL) {
 
369
    sprintf(description,
 
370
            "Bad graph file. Node \"%s\" referenced by arc \"%s\" not found",
 
371
            head_name,(*edge_name)[i]);
 
372
    cerro(description);
 
373
    return;
 
374
  }
 
375
  (*head)[i] = atoi(found->data);
 
376
  if ((node.key = (char *)malloc(strlen(tail_name)+1)) == NULL) {
 
377
    cerro("Running out of memory");
 
378
    return;
 
379
  }
 
380
  strcpy(node.key,tail_name);
 
381
  found = myhsearch(node,FIND);
 
382
  if (found == NULL) {
 
383
    sprintf(description,
 
384
            "Bad graph file. Node \"%s\" referenced by arc \"%s\" not found",
 
385
            tail_name,(*edge_name)[i]);
 
386
    cerro(description);
 
387
    return;
 
388
  }
 
389
  (*tail)[i] = atoi(found->data);
 
390
 
 
391
  fgets(line,5 * MAXNAM,fg);
 
392
  sscanf(line,"%le %le %le %le %le %le %le",
 
393
         &((*edge_cost)[i]),&((*edge_min_cap)[i]),
 
394
         &((*edge_max_cap)[i]),&((*edge_length)[i]),
 
395
         &((*edge_q_weight)[i]),&((*edge_q_orig)[i]),
 
396
         &((*edge_weight)[i]));
 
397
  }
 
398
  myhdestroy();
 
399
  fclose(fg);
 
400
  /** #endif**/ /**  __MSC__ **/
 
401
}