1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/***********************************************************
* Get a filename and open it for reading, retry until
* the file can be opened. '.' terminates the program.
*
* If Fname != NULL, try Fname first.
****/
FILE *GetFile(char *Fname)
{
char * myName = "TEMPLATE.mci";
FILE * file=NULL;
Boolean firsttime=1;
do {
if(firsttime && Fname[0]!='\0') {
/* use the filename from command line */
firsttime = 0;
}
else {
printf("Input filename(or . to exit):");
//scanf("%s", Fname);
firsttime = 0;
}
//if(strlen(Fname) == 1 && Fname[0] == '.')
// exit(1); /* exit if no filename entered. */
//file = fopen(Fname, "r");
file = fopen(myName, "r");
} while(file == NULL);
return(file);
}
/***********************************************************
* Kill the ith char (counting from 0), push the following
* chars forward by one.
****/
void KillChar(size_t i, char * Str)
{
size_t sl = strlen(Str);
for(;i<sl;i++) Str[i] = Str[i+1];
}
/***********************************************************
* Eliminate the chars in a string which are not printing
* chars or spaces.
*
* Spaces include ' ', '\f', '\t' etc.
*
* Return 1 if no nonprinting chars found, otherwise
* return 0.
****/
Boolean CheckChar(char * Str)
{
Boolean found = 0; /* found bad char. */
size_t sl = strlen(Str);
size_t i=0;
while(i<sl)
if (Str[i]<0 || Str[i]>255)
nrerror("Non-ASCII file\n");
else if(isprint(Str[i]) || isspace(Str[i]))
i++;
else {
found = 1;
KillChar(i, Str);
sl--;
}
return(found);
}
/***********************************************************
* Return 1 if this line is a comment line in which the
* first non-space character is "#".
*
* Also return 1 if this line is space line.
****/
Boolean CommentLine(char *Buf)
{
size_t spn, cspn;
spn = strspn(Buf, " \t");
/* length spanned by space or tab chars. */
cspn = strcspn(Buf, "#\n");
/* length before the 1st # or return. */
if(spn == cspn) /* comment line or space line. */
return(1);
else /* the line has data. */
return(0);
}
/***********************************************************
* Skip space or comment lines and return a data line only.
****/
char * FindDataLine(FILE *File_Ptr)
{
static char buf[STRLEN]; /* LW 1/11/2000. Added static. */
buf[0] = '\0';
do { /* skip space or comment lines. */
if(fgets(buf, 255, File_Ptr) == NULL) {
printf("Incomplete data\n");
buf[0]='\0';
break;
}
else
CheckChar(buf);
} while(CommentLine(buf));
return(buf);
}
/***********************************************************
* Skip file version, then read number of runs.
****/
short ReadNumRuns(FILE* File_Ptr)
{
char buf[STRLEN];
short n=0;
FindDataLine(File_Ptr); /* skip file version. */
strcpy(buf, FindDataLine(File_Ptr));
if(buf[0]=='\0') nrerror("Reading number of runs\n");
sscanf(buf, "%hd",&n);
return(n);
}
/***********************************************************
* Read the file name and the file format.
*
* The file format can be either A for ASCII or B for
* binary.
****/
void ReadFnameFormat(FILE *File_Ptr, InputStruct *In_Ptr)
{
char buf[STRLEN];
/** read in file name and format. **/
strcpy(buf, FindDataLine(File_Ptr));
if(buf[0]=='\0')
nrerror("Reading file name and format.\n");
sscanf(buf, "%s %c",
In_Ptr->out_fname, &(In_Ptr->out_fformat) );
if(toupper(In_Ptr->out_fformat) != 'B')
In_Ptr->out_fformat = 'A';
}
/***********************************************************
* Read the number of photons.
****/
void ReadNumPhotons(FILE *File_Ptr, InputStruct *In_Ptr)
{
char buf[STRLEN];
/** read in number of photons. **/
strcpy(buf, FindDataLine(File_Ptr));
if(buf[0]=='\0')
nrerror("Reading number of photons.\n");
sscanf(buf, "%ld", &In_Ptr->num_photons);
if(In_Ptr->num_photons<=0)
nrerror("Nonpositive number of photons.\n");
}
/***********************************************************
* Read the members dz, dr and dt.
****/
void ReadDzDrDt(FILE *File_Ptr, InputStruct *In_Ptr)
{
char buf[STRLEN];
/** read in dz, dr and dt. **/
strcpy(buf, FindDataLine(File_Ptr));
if(buf[0]=='\0') nrerror("Reading dz, dr, dt.\n");
sscanf(buf, "%lf%lf%lf", &In_Ptr->dz, &In_Ptr->dr, &In_Ptr->dt);
if(In_Ptr->dz<=0) nrerror("Nonpositive dz.\n");
if(In_Ptr->dr<=0) nrerror("Nonpositive dr.\n");
if(In_Ptr->dt<=0) nrerror("Nonpositive dt.\n");
}
|