/* compa.c (a COMposite Particle to Amplitudes) Copyright (c) Masato JIMBO 1997 All Rights Reserved */ #include /* strcpy,strtok,size_t,memmove,strlen,strstr */ #include /* mkdir,chdir; in HP-UX, */ /* in BC++, in QC */ #include /* FILE,NULL,fgets,fopen,fputs,printf */ #include /* system */ #define sgrc "/home/jimbo/grace/bin/grc" /* depends on your environment */ #define sgrcfort "/home/jimbo/grace/bin/grcfort" /* depends on your env */ #define MAX_i 32 /* the maximum number of elements = MAX_i - 1 */ #define PERM S_IRWXU|S_IRGRP /* the permission mode of the directories on the unix */ #define sPend "Pend" /* the number of graphs in out.grf */ char *repastr(char *, const char *, const char *); int comp2el(void); int main(void) { int err; err=comp2el(); switch(err) { case 1: printf("The file \"compo.def\" dose not exist in the current directory.\n"); break; case 2: printf("The file \"in.prc\" dose not exist in the current directory.\n"); break; case 3: printf("The number of elements is overflowed.\n"); break; case 4: printf("The file \"elements.log\" cannot be created "); printf("in the current directory.\n"); break; case 5: printf("Some sub-directory cannot be created in the current directory.\n"); break; case 6: printf("The file \"in.prc\" cannot be created in some sub-directory.\n"); break; default: printf("The process was finished successfully.\n"); } return 0; } /* the end of main */ int comp2el(void) /* a COMposite Particle TO its ELements */ { FILE *f_in_def; FILE *f_in_pro; FILE *f_in_grf; FILE *f_out_pro; FILE *f_out_log; int err_flag=0,perm_err; int i,top_l,i_end,j; char s[256]; /* an array for one line */ char s1[256]; /* an array for one line */ char s2[256]; /* an array for one line */ char sel[MAX_i][64]; /* arrays in which the tokens are saved */ char *ptmp; /* a temporary pointer for a token */ char sgr[32]; /* an array in which the number of graphs is saved */ f_in_def=fopen("compo.def","r"); if(f_in_def!=NULL) { i=0; while(fgets(s,sizeof s,f_in_def)!=NULL) /* read lines from compo.def */ { top_l=0; repastr(s,"\n"," "); /* replace a return code with a space */ /* The delimiter of elements is a space. */ if(i==0) { strcpy(sel[i],strtok(s,"=")); top_l=-1; } /* The name of the composite particle has to be placed */ /* at the left of a sign of equality. */ if(top_l==0) { i++; strcpy(sel[i],strtok(s," ")); top_l=-1; } /* for the first token in the line (on and after the second line) */ if(top_l!=-1) break; while((ptmp=strtok(NULL," "))!=NULL) { i++; if(i>=MAX_i) { err_flag=3; break; } strcpy(sel[i],ptmp); } } /* the end of reading lines from compo.def */ i_end=i; fclose(f_in_def); for(j=1;j<=i_end;j++) { f_in_pro=fopen("in.prc","r"); if(f_in_pro!=NULL) { f_out_log=fopen("elements.log","a"); if(f_out_log==NULL) { err_flag=4; break; } perm_err=mkdir(sel[j],PERM); if(perm_err==-1) { err_flag=5; break; } chdir(sel[j]); f_out_pro=fopen("in.prc","w"); if(f_out_pro!=NULL) { while(fgets(s1,sizeof s1,f_in_pro)!=NULL) /* read lines from in.prc */ { repastr(s1,sel[0],sel[j]); /* replace the first token with another */ fputs(s1,f_out_pro); /* write a line into the new in.prc */ } fclose(f_out_pro); system(sgrc); strcpy(s2,sel[j]); strcpy(sgr,"0"); f_in_grf=fopen("out.grf","r"); if(f_in_grf!=NULL) { while(fgets(s,sizeof s,f_in_grf)!=NULL) /* read lines from out.grf */ { if(strstr(s,sPend)!=NULL) { strtok(s,"="); strcpy(sgr,strtok(NULL,";")); } } fclose(f_in_grf); if(strcmp(sgr,"0")>0) system(sgrcfort); strcat(s2," "); strcat(s2,sgr); strcat(s2,"\n"); } else strcat(s2," -1\n"); fputs(s2,f_out_log); } /* the end of if(f_out_pro!=NULL) */ else err_flag=6; chdir(".."); fclose(f_out_log); fclose(f_in_pro); } /* the end of if(f_in_pro!=NULL) */ else err_flag=2; } /* the end of the for loop */ } /* the end of if(f_in_def!=NULL) */ else err_flag=1; return err_flag; } /* the end of comp2el */ char *repastr( /* REPlace A STRing with another */ char *st, /* Total String */ const char *so, /* Old String */ const char *sn) /* New String */ { size_t Lo; /* Length of so */ size_t Ln; /* Length of sn */ char *const p=strstr(st,so); /* the address of the "so" found in the "st" */ if(p==NULL) return NULL; /*if the "so" is not found in the "st", return NULL.*/ Lo=strlen(so); Ln=strlen(sn); if(Lo>=Ln) { memmove(p,sn,Ln); if(Lo>Ln) memmove(p+Ln,p+Lo,st+strlen(st)-(p+Lo-1)); } else { memmove(p+Ln,p+Lo,st+strlen(st)-(p+Lo-1)); memmove(p,sn,Ln); } return p+Ln; /* the address of the point at which */ /* the last replacement occured */ } /* the end of repastr */