00001 #include <stdio.h>
00002 #if !(defined _MSC_VER)
00003 #if defined(netbsd) || defined(freebsd)
00004 #include <sys/types.h>
00005 #endif
00006 #include <dirent.h>
00007 #endif
00008 #include <string.h>
00009 #include <stdlib.h>
00010
00011 #include <math.h>
00012
00013 #ifdef _MSC_VER
00014 #include <direct.h>
00015 #endif
00016 #if (defined _MSC_VER)
00017
00018 #include <io.h>
00019 #define getwd(x) _getcwd(x,1024)
00020 #define chdir(x) _chdir(x)
00021 #endif
00022
00023 #if defined(netbsd) || defined(freebsd)|| defined(linux)
00024 #include <unistd.h>
00025 #endif
00026
00027 #include "machine.h"
00028 #include "MALLOC.h"
00029
00030 #define MAXNAM 80
00031
00032 extern char* my_basename();
00033 extern void cerro();
00034 extern char* dirname();
00035 extern int CheckGraphName();
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 void C2F(saveg)(path,lpath,name,lname,directed,node_number,tail,head,
00046 node_name,node_type,node_x,node_y,node_color,node_diam,node_border,
00047 node_font_size,node_demand,
00048 edge_name,edge_color,edge_width,edge_hi_width,edge_font_size,
00049 edge_length,edge_cost,edge_min_cap,edge_max_cap,edge_q_weight,edge_q_orig,
00050 edge_weight,
00051 default_node_diam,default_node_border,default_edge_width,
00052 default_edge_hi_width,default_font_size,ma)
00053 char *path; int *lpath;
00054 char *name; int *lname,*directed,*node_number,*tail,*head;
00055 char ***node_name;
00056 int *node_type,*node_x,*node_y,*node_color,*node_diam,*node_border;
00057 int *node_font_size;
00058 double *node_demand;
00059 char ***edge_name;
00060 int *edge_color,*edge_width,*edge_hi_width,*edge_font_size;
00061 double *edge_length,*edge_cost,*edge_min_cap,*edge_max_cap;
00062 double *edge_q_weight,*edge_q_orig,*edge_weight;
00063 int *default_node_diam,*default_node_border,*default_edge_width;
00064 int *default_edge_hi_width,*default_font_size;
00065 int *ma;
00066 {
00067 FILE *f;
00068 #if !(defined _MSC_VER)
00069 DIR *dirp;
00070 #else
00071 int it;
00072 #endif
00073 char fname[2*MAXNAM];
00074 char description[2*MAXNAM];
00075 char dir[1024], nname[2*MAXNAM];
00076 int i;
00077 char **lar;
00078
00079
00080 if (*node_number != 1) {
00081 if ((lar = (char **)MALLOC(sizeof(char *) * *node_number)) == NULL) {
00082 cerro("Running out of memory");
00083 return;
00084 }
00085 for (i = 0; i < *node_number; i++) lar[i] = (*node_name)[i];
00086 qsort((char*)lar,*node_number,sizeof(char*),(PF)CompString);
00087 for (i = 0; i < *node_number - 1; i++) {
00088 if (!strcmp(lar[i],lar[i+1])) {
00089 sprintf(description,
00090 "Bad graph file. Node \"%s\" is duplicated",lar[i]);
00091 cerro(description);
00092 FREE(lar);
00093 return;
00094 }
00095 }
00096 if (!strcmp(lar[*node_number - 2],lar[*node_number - 1])) {
00097 sprintf(description,
00098 "Bad graph file. Node \"%s\" is duplicated",lar[*node_number - 2]);
00099 cerro(description);
00100 FREE(lar);
00101 return;
00102 }
00103 FREE(lar);
00104 }
00105
00106 path[*lpath] = '\0';
00107 name[*lname] = '\0';
00108
00109 if (!strcmp(path," ")) {
00110 getwd(dir);
00111 strcpy(nname,name);
00112 }
00113 else {
00114 #if (defined _MSC_VER)
00115 getwd(dir);
00116 #if (defined _MSC_VER)
00117 it= chdir(path);
00118 chdir(dir);
00119 #else
00120 it= chdir_(path,strlen(path));
00121 chdir_(dir,strlen(dir));
00122 #endif
00123 if (it == 0) {
00124 strcpy(dir,path);
00125 }
00126 #else
00127 if ((dirp=opendir(path)) != NULL) {
00128 strcpy(dir,path);
00129 closedir(dirp);
00130 }
00131 #endif
00132 else {
00133 strcpy(nname,StripGraph(my_basename(path)));
00134 if (dirname(path) == NULL) getwd(dir);
00135 else strcpy(dir,dirname(path));
00136 }
00137 }
00138 #if !(defined _MSC_VER)
00139 if ((dirp=opendir(dir)) == NULL) {
00140 sprintf(description,"Directory \"%s\" does not exist",dir);
00141 cerro(description);
00142 return;
00143 }
00144 closedir(dirp);
00145
00146
00147
00148
00149
00150
00151
00152 #endif
00153
00154 strcpy(fname,dir);
00155 strcat(fname,"/");
00156 strcat(fname,nname);
00157 strcat(fname,".graph");
00158 f = fopen(fname,"w");
00159 if (f == NULL) {
00160 sprintf(description,
00161 "Unable to write file in directory %s, check access",dir);
00162 cerro(description);
00163 return;
00164 }
00165
00166
00167 fprintf(f,"GRAPH TYPE (0 = UNDIRECTED, 1 = DIRECTED), DEFAULTS (NODE DIAMETER, NODE BORDER, ARC WIDTH, HILITED ARC WIDTH, FONTSIZE):\n");
00168 fprintf(f,"%d %d %d %d %d %d\n",*directed,*default_node_diam,
00169 *default_node_border,*default_edge_width,*default_edge_hi_width,
00170 *default_font_size);
00171 if (*directed) {
00172 fprintf(f,"NUMBER OF ARCS:\n");
00173 }
00174 else {
00175 fprintf(f,"NUMBER OF EDGES:\n");
00176 }
00177 fprintf(f,"%d\n",*ma);
00178 fprintf(f,"NUMBER OF NODES:\n");
00179 fprintf(f,"%d\n",*node_number);
00180 fprintf(f,"****************************************\n");
00181
00182
00183
00184 if (*directed) {
00185 fprintf(f,"DESCRIPTION OF ARCS:\n");
00186 fprintf(f,"ARC NAME, TAIL NODE NAME, HEAD NODE NAME, COLOR, WIDTH, HIWIDTH, FONTSIZE\n");
00187 }
00188 else {
00189 fprintf(f,"DESCRIPTION OF EDGES:\n");
00190 fprintf(f,"EDGE NAME, NODE NAME, NODE NAME, COLOR, WIDTH, HIWIDTH, FONTSIZE\n");
00191 }
00192 fprintf
00193 (f,"COST, MIN CAP, MAX CAP, LENGTH, Q WEIGHT, Q ORIGIN, WEIGHT\n");
00194 fprintf(f,"\n");
00195
00196 for (i = 0; i < *ma; i++) {
00197 fprintf(f,"%s %s %s %d %d %d %d\n",(*edge_name)[i],(*node_name)[tail[i]-1],
00198 (*node_name)[head[i]-1],edge_color[i],edge_width[i],
00199 edge_hi_width[i],edge_font_size[i]);
00200 fprintf(f,"%e %e %e %e %e %e %e\n",
00201 edge_cost[i],edge_min_cap[i],edge_max_cap[i],edge_length[i],
00202 edge_q_weight[i],edge_q_orig[i],edge_weight[i]);
00203 }
00204
00205
00206
00207 fprintf(f,"****************************************\n");
00208 fprintf(f,"DESCRIPTION OF NODES:\n");
00209 fprintf(f,"NODE NAME, POSSIBLE TYPE (1 = SINK, 2 = SOURCE)\n");
00210 fprintf(f,"X, Y, COLOR, DIAMETER, BORDER, FONTSIZE\n");
00211 fprintf(f,"DEMAND\n");
00212 fprintf(f,"\n");
00213
00214 for (i = 0; i < *node_number; i++) {
00215 if (node_type[i] == 0)
00216 fprintf(f,"%s\n",(*node_name)[i]);
00217 else fprintf(f,"%s %d\n",(*node_name)[i],node_type[i]);
00218 fprintf(f,"%d %d %d %d %d %d \n",
00219 node_x[i],node_y[i],node_color[i],node_diam[i],
00220 node_border[i],node_font_size[i]);
00221 fprintf(f,"%e\n",node_demand[i]);
00222 }
00223
00224 fclose(f);
00225 }