00001 #include <stdio.h>
00002 #include <math.h>
00003 #if !(defined _MSC_VER)
00004 #ifdef __FreeBSD__
00005 #include <sys/types.h>
00006 #endif
00007 #include <dirent.h>
00008 #endif
00009 #include <string.h>
00010 #include <stdlib.h>
00011
00012 #ifdef _MSC_VER
00013 #include <direct.h>
00014 #else
00015 #include <unistd.h>
00016 #endif
00017
00018
00019
00020 #if (defined _MSC_VER)
00021
00022 #define getwd(x) _getcwd(x,1024)
00023 #endif
00024
00025 #include "defs.h"
00026 #include "machine.h"
00027 #include "MALLOC.h"
00028 #include "hashtable_metanet.h"
00029
00030 #define MAXNAM 80
00031
00032 extern char* my_basename();
00033 extern int CheckGraphName();
00034 extern void cerro __PARAMS((char *str));
00035 extern char* dirname();
00036 extern char *StripGraph();
00037
00038 typedef int (*PF)(const void *,const void *);
00039
00040 static int CompString(char **s1,char **s2)
00041 {
00042 return strcmp((char*)*s1,(char*)*s2);
00043 }
00044
00045 static char description[2*MAXNAM];
00046
00047 void C2F(loadg)(path,lpath,name,lname,directed,n,tail,head,
00048 node_name,node_type,node_x,node_y,node_color,node_diam,node_border,
00049 node_font_size,node_demand,
00050 edge_name,edge_color,edge_width,edge_hi_width,edge_font_size,
00051 edge_length,edge_cost,edge_min_cap,edge_max_cap,edge_q_weight,edge_q_orig,
00052 edge_weight,
00053 default_node_diam,default_node_border,default_edge_width,
00054 default_edge_hi_width,default_font_size,
00055 ndim,ma)
00056 char *path; int *lpath,*directed,*n,**tail,**head;
00057 char **name; int *lname;
00058 char ***node_name;
00059 int **node_type,**node_x,**node_y,**node_color,**node_diam,**node_border;
00060 int **node_font_size;
00061 double **node_demand;
00062 char ***edge_name;
00063 int **edge_color,**edge_width,**edge_hi_width,**edge_font_size;
00064 double **edge_length,**edge_cost,**edge_min_cap,**edge_max_cap;
00065 double **edge_q_weight,**edge_q_orig,**edge_weight;
00066 int *default_node_diam,*default_node_border,*default_edge_width;
00067 int *default_edge_hi_width,*default_font_size;
00068 int *ndim,*ma;
00069 {
00070 FILE *fg;
00071 #ifndef _MSC_VER
00072 DIR *dirp=NULL;
00073 #endif
00074 char fname[2 * MAXNAM];
00075 char line[5 * MAXNAM];
00076 char strname[MAXNAM], head_name[MAXNAM], tail_name[MAXNAM];
00077 int isize,dsize;
00078 int i,s;
00079 ENTRY node;
00080 ENTRY *found=NULL;
00081 char dir[1024];
00082 char *pname=NULL;
00083 char **lar=NULL;
00084
00085 path[*lpath] = '\0';
00086 #ifndef _MSC_VER
00087 if ((dirp=opendir(path)) != NULL)
00088 {
00089 sprintf(description,"\"%s\" is a directory",path);
00090 cerro(description);
00091 closedir(dirp);
00092 return;
00093 }
00094 #endif
00095 if (dirname(path) == NULL) getwd(dir);
00096 else strcpy(dir,dirname(path));
00097 #ifndef _MSC_VER
00098 if ((dirp=opendir(dir)) == NULL)
00099 {
00100 sprintf(description,"Directory \"%s\" does not exist",dir);
00101 cerro(description);
00102 return;
00103 }
00104 closedir(dirp);
00105 #endif
00106
00107 pname = StripGraph(my_basename(path));
00108
00109 *lname = (int)strlen(pname);
00110
00111 if ((*name = (char *)MALLOC((unsigned)sizeof(char)*(*lname + 1))) == NULL)
00112 {
00113 cerro("Running out of memory");
00114 return;
00115 }
00116 strcpy(*name,pname);
00117
00118 if (pname) { FREE(pname); pname=NULL;}
00119
00120 #ifndef _MSC_VER
00121 if(!CheckGraphName(*name,dir))
00122 {
00123 sprintf(description,"Graph file \"%s/%s.graph\" does not exist",dir,*name);
00124 cerro(description);
00125 return;
00126 }
00127 #endif
00128 isize = sizeof(int);
00129 dsize = sizeof(double);
00130
00131 strcpy(fname,dir);
00132 strcat(fname,"/");
00133 strcat(fname,*name);
00134 strcat(fname,".graph");
00135 fg = fopen(fname,"r");
00136 if (fg == 0)
00137 {
00138 sprintf(description,"Unable to open file \"%s/%s.graph\"",dir,*name);
00139 cerro(description);
00140 return;
00141 }
00142
00143
00144 fgets(line,5 * MAXNAM,fg);
00145 fgets(line,5 * MAXNAM,fg);
00146 *default_node_diam = NODEDIAM;
00147 *default_node_border = NODEW;
00148 *default_edge_width = ARCW;
00149 *default_edge_hi_width = ARCH;
00150 *default_font_size = DRAWFONTSIZE;
00151 sscanf(line,"%d %d %d %d %d %d",directed,default_node_diam,
00152 default_node_border,default_edge_width,
00153 default_edge_hi_width,default_font_size);
00154 if (*default_node_diam == 0) *default_node_diam = NODEDIAM;
00155 if (*default_node_border == 0) *default_node_border = NODEW;
00156 if (*default_edge_width == 0) *default_edge_width = ARCW;
00157 if (*default_edge_hi_width == 0) *default_edge_hi_width = ARCH;
00158 if (*default_font_size == 0) *default_font_size = DRAWFONTSIZE;
00159 fgets(line,5 * MAXNAM,fg);
00160 fgets(line,5 * MAXNAM,fg);
00161 sscanf(line,"%d",ma);
00162 fgets(line,5 * MAXNAM,fg);
00163 fgets(line,5 * MAXNAM,fg);
00164 sscanf(line,"%d",ndim);
00165 *n = *ndim;
00166 fgets(line,5 * MAXNAM,fg);
00167 fgets(line,5 * MAXNAM,fg);
00168 fgets(line,5 * MAXNAM,fg);
00169 fgets(line,5 * MAXNAM,fg);
00170 fgets(line,5 * MAXNAM,fg);
00171
00172
00173 s = sizeof(char *) * *ndim;
00174 if ((*node_name = (char **)MALLOC(s)) == NULL)
00175 {
00176 cerro("Running out of memory");
00177 return;
00178 }
00179 s = isize * *ndim;
00180 if ((*node_type = (int *)MALLOC(s)) == NULL)
00181 {
00182 cerro("Running out of memory");
00183 return;
00184 }
00185 if ((*node_x = (int *)MALLOC(s)) == NULL)
00186 {
00187 cerro("Running out of memory");
00188 return;
00189 }
00190 if ((*node_y = (int *)MALLOC(s)) == NULL)
00191 {
00192 cerro("Running out of memory");
00193 return;
00194 }
00195 if ((*node_color = (int *)MALLOC(s)) == NULL)
00196 {
00197 cerro("Running out of memory");
00198 return;
00199 }
00200 if ((*node_diam = (int *)MALLOC(s)) == NULL)
00201 {
00202 cerro("Running out of memory");
00203 return;
00204 }
00205 if ((*node_border = (int *)MALLOC(s)) == NULL)
00206 {
00207 cerro("Running out of memory");
00208 return;
00209 }
00210 if ((*node_font_size = (int *)MALLOC(s)) == NULL)
00211 {
00212 cerro("Running out of memory");
00213 return;
00214 }
00215 s = dsize * *ndim;
00216 if ((*node_demand = (double *)MALLOC(s)) == NULL)
00217 {
00218 cerro("Running out of memory");
00219 return;
00220 }
00221
00222
00223 s = sizeof(char *) * *ma;
00224 if ((*edge_name = (char **)MALLOC(s)) == NULL)
00225 {
00226 cerro("Running out of memory");
00227 return;
00228 }
00229 s = isize * *ma;
00230 if ((*head = (int *)MALLOC(s)) == NULL)
00231 {
00232 cerro("Running out of memory");
00233 return;
00234 }
00235 if ((*tail = (int *)MALLOC(s)) == NULL)
00236 {
00237 cerro("Running out of memory");
00238 return;
00239 }
00240 if ((*edge_color = (int *)MALLOC(s)) == NULL)
00241 {
00242 cerro("Running out of memory");
00243 return;
00244 }
00245 if ((*edge_width = (int *)MALLOC(s)) == NULL)
00246 {
00247 cerro("Running out of memory");
00248 return;
00249 }
00250 if ((*edge_hi_width = (int *)MALLOC(s)) == NULL)
00251 {
00252 cerro("Running out of memory");
00253 return;
00254 }
00255 if ((*edge_font_size = (int *)MALLOC(s)) == NULL)
00256 {
00257 cerro("Running out of memory");
00258 return;
00259 }
00260 s = dsize * *ma;
00261 if ((*edge_length = (double *)MALLOC(s)) == NULL)
00262 {
00263 cerro("Running out of memory");
00264 return;
00265 }
00266 if ((*edge_cost = (double *)MALLOC(s)) == NULL)
00267 {
00268 cerro("Running out of memory");
00269 return;
00270 }
00271 if ((*edge_min_cap = (double *)MALLOC(s)) == NULL)
00272 {
00273 cerro("Running out of memory");
00274 return;
00275 }
00276 if ((*edge_max_cap = (double *)MALLOC(s)) == NULL)
00277 {
00278 cerro("Running out of memory");
00279 return;
00280 }
00281 if ((*edge_q_weight = (double *)MALLOC(s)) == NULL)
00282 {
00283 cerro("Running out of memory");
00284 return;
00285 }
00286 if ((*edge_q_orig = (double *)MALLOC(s)) == NULL)
00287 {
00288 cerro("Running out of memory");
00289 return;
00290 }
00291 if ((*edge_weight = (double *)MALLOC(s)) == NULL)
00292 {
00293 cerro("Running out of memory");
00294 return;
00295 }
00296
00297
00298 for (i = 0; i < 2 * *ma; i++) fgets(line,5 * MAXNAM,fg);
00299
00300 fgets(line,5 * MAXNAM,fg);
00301 fgets(line,5 * MAXNAM,fg);
00302 fgets(line,5 * MAXNAM,fg);
00303 fgets(line,5 * MAXNAM,fg);
00304 fgets(line,5 * MAXNAM,fg);
00305 fgets(line,5 * MAXNAM,fg);
00306
00307 myhcreate(*ndim);
00308 for (i = 0; i < *ndim; i++)
00309 {
00310 fgets(line,5 * MAXNAM,fg);
00311 (*node_type)[i] = 0;
00312 sscanf(line,"%s %d",strname,&((*node_type)[i]));
00313 if (((*node_name)[i] = (char *)MALLOC(strlen(strname)+1)) == NULL)
00314 {
00315 cerro("Running out of memory");
00316 return;
00317 }
00318 strcpy((*node_name)[i],strname);
00319 fgets(line,5 * MAXNAM,fg);
00320 (*node_x)[i] = 0;
00321 (*node_y)[i] = 0;
00322 (*node_color)[i] = 0;
00323 (*node_diam)[i] = 0;
00324 (*node_border)[i] = 0;
00325 (*node_font_size)[i] = 0;
00326 sscanf(line,"%d %d %d %d %d %d",
00327 &((*node_x)[i]),&((*node_y)[i]),&((*node_color)[i]),
00328 &((*node_diam)[i]),&((*node_border)[i]),&((*node_font_size)[i]));
00329 fgets(line,5 * MAXNAM,fg);
00330 sscanf(line,"%le",&((*node_demand)[i]));
00331
00332 if ((node.key = (char *)MALLOC(strlen(strname)+1)) == NULL)
00333 {
00334 cerro("Running out of memory");
00335 return;
00336 }
00337
00338 strcpy(node.key,strname);
00339 sprintf(strname,"%d",i+1);
00340
00341 if ((node.data = (char *)MALLOC(strlen(strname)+1)) == NULL)
00342 {
00343 cerro("Running out of memory");
00344 return;
00345 }
00346 strcpy(node.data,strname);
00347 myhsearch(node,SCIENTER);
00348
00349 if (node.data) {FREE(node.data);node.data=NULL;}
00350 if (node.key) {FREE(node.key);node.key=NULL;}
00351 }
00352
00353
00354 if (*ndim != 1)
00355 {
00356 if ((lar = (char **)MALLOC(sizeof(char *) * *ndim)) == NULL)
00357 {
00358 cerro("Running out of memory");
00359 return;
00360 }
00361
00362 for (i = 0; i < *ndim; i++) lar[i] = (*node_name)[i];
00363
00364 qsort((char*)lar,*ndim,sizeof(char*),(PF)CompString);
00365
00366 for (i = 0; i < *ndim - 1; i++)
00367 {
00368 if (!strcmp(lar[i],lar[i+1]))
00369 {
00370 sprintf(description,"Bad graph file. Node \"%s\" is duplicated",lar[i]);
00371 cerro(description);
00372 FREE(lar);
00373 lar=NULL;
00374 return;
00375 }
00376 }
00377
00378 if (!strcmp(lar[*ndim - 2],lar[*ndim - 1]))
00379 {
00380 sprintf(description,"Bad graph file. Node \"%s\" is duplicated",lar[*ndim - 2]);
00381 cerro(description);
00382 if (lar) {FREE(lar);lar=NULL;}
00383 return;
00384 }
00385 if (lar) {FREE(lar);lar=NULL;}
00386 }
00387
00388
00389 rewind(fg);
00390 for (i = 0; i < 11; i++) fgets(line,5 * MAXNAM,fg);
00391
00392 for (i = 0; i < *ma; i++)
00393 {
00394 fgets(line,5 * MAXNAM,fg);
00395 (*edge_color)[i] = 0;
00396 (*edge_width)[i] = 0;
00397 (*edge_hi_width)[i] = 0;
00398 (*edge_font_size)[i] = 0;
00399 sscanf(line,"%s %s %s %d %d %d %d\n",strname,
00400 tail_name,head_name,
00401 &((*edge_color)[i]),&((*edge_width)[i]),&((*edge_hi_width)[i]),
00402 &((*edge_font_size)[i]));
00403
00404 if (((*edge_name)[i] = (char *)MALLOC(strlen(strname)+1)) == NULL)
00405 {
00406 cerro("Running out of memory");
00407 return;
00408 }
00409 strcpy((*edge_name)[i],strname);
00410
00411 if ((node.key = (char *)MALLOC(strlen(head_name)+1)) == NULL)
00412 {
00413 cerro("Running out of memory");
00414 return;
00415 }
00416
00417 strcpy(node.key,head_name);
00418 found = myhsearch(node,SCIFIND);
00419 if (found == NULL)
00420 {
00421 sprintf(description,"Bad graph file. Node \"%s\" referenced by arc \"%s\" not found",head_name,(*edge_name)[i]);
00422 cerro(description);
00423 return;
00424 }
00425 (*head)[i] = atoi(found->data);
00426
00427 if(found)
00428 {
00429 if (found->data) {FREE(found->data);found->data=NULL;}
00430 if (found->key) {FREE(found->key);found->key=NULL;}
00431 FREE(found);found=NULL;
00432 }
00433
00434
00435 if ((node.key = (char *)MALLOC(strlen(tail_name)+1)) == NULL)
00436 {
00437 cerro("Running out of memory");
00438 return;
00439 }
00440
00441 strcpy(node.key,tail_name);
00442 found = myhsearch(node,SCIFIND);
00443 if (found == NULL)
00444 {
00445 sprintf(description,
00446 "Bad graph file. Node \"%s\" referenced by arc \"%s\" not found",
00447 tail_name,(*edge_name)[i]);
00448 cerro(description);
00449 return;
00450 }
00451 (*tail)[i] = atoi(found->data);
00452 if(found)
00453 {
00454 if (found->data) {FREE(found->data);found->data=NULL;}
00455 if (found->key) {FREE(found->key);found->key=NULL;}
00456 FREE(found);found=NULL;
00457 }
00458
00459 fgets(line,5 * MAXNAM,fg);
00460 sscanf(line,"%le %le %le %le %le %le %le",
00461 &((*edge_cost)[i]),&((*edge_min_cap)[i]),
00462 &((*edge_max_cap)[i]),&((*edge_length)[i]),
00463 &((*edge_q_weight)[i]),&((*edge_q_orig)[i]),
00464 &((*edge_weight)[i]));
00465 }
00466
00467 myhdestroy();
00468 fclose(fg);
00469 if (node.key) {FREE(node.key);node.key=NULL;}
00470 if (node.data) {FREE(node.data);node.data=NULL;}
00471 }