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

mdp_prompt.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 const char STD_INPUT[] = "";
00015 const char STD_INPUT_FILE[] = "<stdin>";
00016 
00018 double val(string s) {
00019   return atof(s.c_str());
00020 }
00021 
00022 
00025 string prompt(string filename, 
00026               string variable, 
00027               string def_val="0.0", 
00028               int p=0) {
00029   FILE *fp=0;
00030   static char tmp[1024];
00031   int active, length;
00032   char c,d;
00033   char response[1024];
00034 
00035 #ifdef PARALLEL
00036   mpi.barrier();
00037 #endif
00038 
00039   if(ME==p) {
00040     if(filename==STD_INPUT) {
00041       printf("Input value of %s (default is `%s'): ", 
00042              variable.c_str(), def_val.c_str());
00043       cin >> response;
00044     } else {
00045       if(filename==STD_INPUT_FILE) { 
00046         fp=stdin;
00047         printf("Input variable (default is `%s %s')...\n", 
00048                variable.c_str(), def_val.c_str());
00049       } else {
00050         fp=fopen(filename.c_str(),"r");
00051       }
00052       fseek(fp, 0, 0);
00053       active=1;
00054       length=0;
00055       d=0;
00056       tmp[0]='\0';
00057       response[0]='\0';
00058       do {
00059         c=fgetc(fp);    
00060         switch(active) {
00061         case 0: if(c=='\n') {active=1; length=0;} break;
00062         case 1: if(c=='#') { 
00063           active=0;
00064         } else if((length>0) && ((c==' ') || (c=='\t'))) {
00065           active=2; length=0;
00066         } else if((length>0) && (c=='\n')) {
00067           active=1; length=0;
00068           if(variable==tmp) c=EOF;
00069         } else if((c!=' ') && (c!='\t') && (c!='\n')) {
00070           tmp[length]=c; tmp[length+1]='\0'; length++; 
00071         } break;        
00072         case 2: if(c=='#') { 
00073           active=0;
00074         } else if((c=='\n') || (c==EOF)) {
00075           active=1;
00076           length=0;
00077           if(variable==tmp) d=1;
00078         } else if((length==0) && ((c==' ') || (c=='\t') || (c=='='))) {
00079           // do nothing
00080         } else if((c==' ') || (c=='\t')) {
00081           active=0;
00082           if(variable==tmp) d=1;
00083         } else { 
00084           response[length]=c; response[length+1]='\0'; length++; 
00085         } break;
00086         }
00087         // printf("%i %c %s %s\n", active, c, tmp, response);
00088       } while ((c!=EOF) && (d!=1));
00089 
00090       if((d==0) || (strcmp(response,"")==0)) 
00091         strcpy(response, def_val.c_str());
00092       
00093       if(fp!=stdin && fp!=0) 
00094         fclose(fp);
00095     }
00096     printf("... Adopting %s equal to \"%s\"\n\n", variable.c_str(), response);
00097   }
00098 
00099 #ifdef PARALLEL
00100   mpi.broadcast(response, 1024, p);
00101 #endif
00102   string s(response);
00103   return s;
00104 }
00105 
00106 
00107 
00108 
00109 

Generated on Sun Feb 27 15:12:21 2005 by  doxygen 1.4.1