Layers
Diffusion in heterogeneous environments
io.c
Go to the documentation of this file.
1 
12 // includes
13 #include "header.h"
14 
25 void get_filename(char *in, char *out)
26 {
27  // The input and output filenames are 4 characters longer than
28  // the base filename, hence the "- 4"
29  if (strlen(in) < (FILENAME_MAX - 4))
30  strcpy(out, in);
31  else
32  error("Filename length is too long");
33 }
34 
35 
60 void get_io_filenames(char *argstring, const char *inf_extension, const char *outf_extension, char *infilename, char *outfilename)
61 {
62  char *strng;
63 
64  get_filename(argstring, infilename);
65  strcpy(outfilename, infilename);
66 
67  strng = index(outfilename, '.'); /* Find the '.' in the filename */
68  if (strng == NULL) { /* If no '.', then add default extensions */
69  strcat(infilename, inf_extension);
70  strcat(outfilename, outf_extension);
71  } else {
72  strcpy(strng, outf_extension); /* Replace ext. with default */
73  }
74 }
75 
76 
91 int assemble_command(int argc, char *argv[], char *command)
92 {
93  int i, length;
94 
95  strcpy(command,argv[0]);
96  strcat(command, " ");
97  length=strlen(argv[0]) + 1;
98  for (i=1; i<argc; i++) {
99  length += strlen(argv[i]) + 1;
100  if (length > MAX_COMMAND_LENGTH) {
101  printf("Warning: length of command too long to put in struct \n");
102  strcat(command, "...");
103  break;
104  }
105  strcat(command, argv[i]);
106  strcat(command, " ");
107  }
108 
109  return (i);
110 }
111 
112 
113 /*******************************************************************
114  This function is for parsing a string which has several
115  doubles delimited by spaces or commas. It uses strtok.
116  The string has parameters for the extra sources.
117 
118  For example,
119  --additional_sources "2 50.0 0.0 100.0 -50.0 0.0 100.0"
120  -> 2 means there are 2 additional sources
121  -> +/-50.0, 0.0, and 100.0 mean the sz, sr, and crnt
122  values for the sources
123 
124  Input: string describing the quantity (sz, sr, or crnt)
125  and # of the extra source -- both for diagnostics
126  Return: value
127 *******************************************************************/
153 double read_source_parameter(char *string, int nsource)
154 {
155  double value;
156  char *token;
157 
158  token = strtok(NULL, " ,");
159  if (token == NULL)
160  error("Cannot read %s token; nsource = %d\n", string, nsource);
161 
162  value = atof(token);
163 
164  return (value);
165 }
Header file for program 3layer.
void error(char *errorstring,...)
Print an error message to stderr and exit with status EXIT_FAILURE.
Definition: extras.c:20
#define MAX_COMMAND_LENGTH
Maximum number of characters of command to copy to output file.
Definition: header.h:37
double read_source_parameter(char *string, int nsource)
Read a parameter from the string argument to the additional_sources option.
Definition: io.c:153
void get_io_filenames(char *argstring, const char *inf_extension, const char *outf_extension, char *infilename, char *outfilename)
Determine the input filename and the default output file name.
Definition: io.c:60
void get_filename(char *in, char *out)
Make sure that the filename is not too long.
Definition: io.c:25
int assemble_command(int argc, char *argv[], char *command)
Create a string containing the command used to run the program.
Definition: io.c:91