Main Page | Class List | File List | Class Members | File Members

input.c

Go to the documentation of this file.
00001 #include "standard.h"
00002 #include <stdarg.h>
00003 #include "ansi.h"
00004 
00005 
00006 /*
00007  * Here are the input functions you should be using:
00008  *
00009  * int get_int();
00010  * double get_real();
00011  * void get_str(char *s);
00012  * 
00013  */
00014 
00015 #define STRLEN 256
00016 
00017 extern int debug;
00018 static FILE *input_file;  /*Rajiv */
00019 static char input_filename[STRLEN];
00020 static int line_number=0;  /* After first read_line(), line_number = 1 */
00021 
00022 
00023 int exist(char *fn)
00024      /*
00025       * Returns 1 if file exists (i.e. can be read from).  Returns 0 if it doesn't.
00026       */
00027 {
00028   int x = open(fn);
00029   if (x != -1) {
00030     close(x);
00031     x = 1;
00032   } else
00033     x = 0;
00034   return (x);
00035 }
00036 
00037 void banner(char *message)
00038 {
00039   int i;
00040   int x = (75 - strlen(message)) / 2;
00041 
00042   printf("+-----------------------------------------------------------------------------+\n");
00043   printf(": ");
00044   for (i=0; i<x; i++)
00045     putchar(' ');
00046   printf(message);
00047   if (((75 - strlen(message)) % 2) == 1)
00048     putchar(' ');
00049   for (i=0; i<x; i++)
00050     putchar(' ');  
00051   printf(" :\n");
00052   printf("+-----------------------------------------------------------------------------+\n");
00053 }
00054 
00055 
00056 #if(OLDHDR)
00057 void hdr(char *msg)
00058 {
00059   printf("              .-------- --- -- - -  -     -       -      -- ---.\n");
00060   printf("              : %-46s :\n", msg);
00061   printf("              `---- ---- - --          -  ---   -    - -- -----'\n");
00062 }
00063 #else
00064 void hdr(char *msg)
00065 {
00066   printf("              ##################################################\n");
00067   printf("              # %-46s #\n", msg);
00068   printf("              ##################################################\n");
00069 }
00070 #endif
00071 
00072 void hdr2(char *msg)
00073 {
00074   printf("          +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+\n");
00075   printf("          | %-55s |\n", msg);
00076   printf("          +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+\n");
00077 }
00078 
00079 void prompt(char *s)
00080 {
00081   printf("+-----------------------------------------------------------------------------+\n");
00082   printf(":                                                                             :\n");
00083   printf("+-----------------------------------------------------------------------------+\n");
00084   up(2);
00085   right(2);
00086   printf(s);
00087   fflush(stdout);
00088 }
00089 
00090 
00091 static void read_line1(char *s)
00092      /* 
00093       * Read at most one line from the file.  
00094       */
00095 {
00096   int i = 0;
00097   int done = 0;
00098   int escape = 0;
00099   char c;
00100 
00101   s[0]='\0';
00102 
00103   while(!done && !feof(input_file)) {
00104     c = fgetc(input_file);
00105     if (escape) {
00106       s[i++]=c;
00107       escape=0;
00108     }
00109     else {
00110       if (c == '\\') {
00111         escape = 1;
00112       }
00113       else {
00114         if (c == '\n'){
00115           done = 1;
00116         }
00117         else {
00118           if (feof(input_file))
00119             done = 1;
00120           else
00121             s[i++] = c;
00122         }
00123       }
00124     }
00125   }
00126   s[i]='\0';
00127   line_number++;
00128 }
00129 
00130 
00131 static int strip_comments(char *s)
00132      /*
00133       * Strips anything including and after a '#' or ';'.
00134       */
00135 {
00136   int i=0;
00137   int comments=0;
00138   int len=strlen(s);
00139   while (i < strlen(s)) {
00140     if ((s[i]=='#') || (s[i]==';')) {
00141       comments = 1;
00142       s[i]='\0';
00143     }
00144     i++;
00145   }
00146   return(comments);
00147 }
00148 
00149 
00150 static void strip_ending_whitespace(char *s)
00151 {
00152   int x = strlen(s)-1;
00153   if (x > 0) {
00154     while (x > 0 && (s[x]==' ' || s[x]=='\t')) {
00155       s[x] = '\0';
00156       x--;
00157     }
00158   }
00159 }
00160 
00161 static void read_line(char *s)
00162      /* 
00163       * Read a line, but ignore lines that start with '#' or ';' 
00164       *
00165       *  s[0] will end up being == '\0' when we reach EOF.
00166       */
00167 {
00168   read_line1(s);
00169   strip_comments(s);
00170   strip_ending_whitespace(s);
00171 }
00172 
00173 
00174 char upper(char c)
00175      /*
00176       * Return the uppercase version of the input character,
00177       * or just return c if c is not in 'a'..'z'.
00178       */
00179 {
00180   if ((c >= 'a') && (c <= 'z'))
00181     return (c - 'a' + 'A');
00182   else
00183     return c;
00184 }
00185 
00186 int equals(char *s1, char *s2)
00187      /*
00188       * Returns 1 if strings are equal, 0 if they are not.
00189       */
00190 {
00191   int i=0;
00192   while ((s1[i]!='\0') && (s2[i]!='\0') && (upper(s1[i])==upper(s2[i])))
00193     i++;
00194   if (s1[i]==s2[i])
00195     return 1;
00196   else
00197     return 0;
00198 }
00199 
00200 
00201 int get_int(void)
00202      /*
00203       * Returns an integer read from input
00204       */
00205 {
00206   char s[STRLEN];
00207   int x;
00208 
00209   read_line(s);
00210   x = atoi(s);
00211   if (input_file != stdin)
00212     printf("%d\n", x);
00213   return(x);
00214 }
00215 
00216 
00217 double get_real(void)
00218      /*
00219       * Read a floating point number from input.
00220       */
00221 {
00222   char s[STRLEN];
00223   double x;
00224   read_line(s);
00225   x = atof(s);
00226   if (input_file != stdin)
00227     printf("%f\n", x);
00228   return(x);
00229 }
00230 
00231 
00232 
00233 void get_str(char *s)
00234      /*
00235       * Read a string from input.
00236       */
00237 {
00238   read_line(s);
00239   if ((input_file != stdin) && (debug))
00240     printf("%s\n", s);
00241 }
00242 
00243 
00244 char *text(int x)
00245      /* 
00246       * returns a string corresponding a text description of the integer x 
00247       */
00248 {
00249   static char sx[STRLEN];
00250   switch(x) {
00251     case 0: return("Zero");
00252     case 1: return("One");
00253     case 2: return("Two");
00254     case 3: return("Three");
00255     case 4: return("Four");
00256     case 5: return("Five");
00257     case 6: return("Six");
00258     case 7: return("Seven");
00259     case 8: return("Eight");
00260     case 9: return("Nine");
00261     case 10: return("Ten");
00262     case 11: return("Eleven");
00263     case 12: return("Twelve");
00264     case 13: return("Thirteen");
00265     case 14: return("Fourteen");
00266     case 15: return("Fifteen");
00267     case 16: return("Sixteen");
00268     case 17: return("Seventeen");
00269     case 18: return("Eighteen");
00270     case 19: return("Nineteen");
00271     case 20: return("Twenty");
00272     default: sprintf(sx, "%d", x); return(sx);
00273   }
00274 }
00275 
00276 void open_input_file(char *fn)
00277 {
00278   if ((input_file != stdin) && (input_file != NULL))
00279     fclose(stdin);
00280   input_file = fopen(fn, "r");
00281   if (input_file != NULL) {
00282     line_number = 0;
00283   }
00284   else {
00285     error("open_input_file", "Couldn't open config file");
00286   }
00287   strcpy(input_filename, fn);
00288 }
00289 
00290 void close_input_file(void)
00291 {
00292   if ((input_file != stdin) && (input_file != NULL))
00293     fclose(input_file);
00294   input_file = stdin;
00295 }
00296 
00297 
00298 int eof_input_file()
00299 {
00300   return (feof(input_file));
00301 }
00302 
00303 
00304 
00305 
00306 

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1