| [a0bcf1] | 1 | //#include "init.h"
 | 
|---|
 | 2 | #include <stdio.h>
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 | enum value_type { string_type, double_type, int_type, grid, lower_trigrid, upper_trigrid};      //!< Specifying the type to be read of a parameter
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | /** Reads parameter from a parsed file.
 | 
|---|
 | 8 |  * \warning value is modified (both in contents and position)!
 | 
|---|
 | 9 |  * \param file file to be parsed
 | 
|---|
 | 10 |  * \param name Name of value in file (at least 3 chars!)
 | 
|---|
 | 11 |  * \param xth Which among a number of parameters it is (in grid case it's row number as grid is read as a whole!)
 | 
|---|
 | 12 |  * \param yth Only needed in grid case, specifying column number
 | 
|---|
 | 13 |  * \param type Type of the Parameter to be read
 | 
|---|
 | 14 |  * \param value address of the value to be read (must have been allocated)
 | 
|---|
 | 15 |  * \sa ReadMainParameterFile Calling routine
 | 
|---|
 | 16 |  */
 | 
|---|
 | 17 | int ParseForParameter(FILE *file, const char *const name, int const xth, int const yth, enum value_type type, void *value) {
 | 
|---|
 | 18 |         int i,j;        // loop variables
 | 
|---|
 | 19 |         int me;
 | 
|---|
 | 20 |         long file_position = ftell(file);       // mark current position
 | 
|---|
 | 21 |         char *dummy1, *dummy, *free_dummy;              // pointers in the line that is read in per step
 | 
|---|
 | 22 |         char *free_parameter, *parameter;       // parameter dummy
 | 
|---|
 | 23 |         dummy1 = free_dummy = (char *) malloc(256 * sizeof(char)); //,"ParseForParameter: MemAlloc for dummy1 failed");
 | 
|---|
 | 24 |   //MPI_Comm_rank(MPI_COMM_WORLD, &me);
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 |         //fprintf(stderr,"(%i) Parsing for %s\n",me,name);      
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 |         int line = 0;   // marks line where parameter was found
 | 
|---|
 | 29 |         int found = 0;  // marks if parameter name was found
 | 
|---|
 | 30 |         while((!feof(file)) && (!found)) {
 | 
|---|
 | 31 |                 dummy1 = dummy = free_dummy;
 | 
|---|
 | 32 |                 do {
 | 
|---|
 | 33 |                         fgets(dummy1, 256, file);       // Read the whole line, skips commentary and empty ones
 | 
|---|
 | 34 |                         line++;
 | 
|---|
 | 35 |                 } while ((dummy1[0] == '#') || (dummy1[0] == '\n'));
 | 
|---|
 | 36 |                 if (dummy1 == NULL)
 | 
|---|
 | 37 |                         fprintf(stderr,"(%i) Error reading line %i\n",me,line);
 | 
|---|
 | 38 |                 else 
 | 
|---|
 | 39 |                         //fprintf(stderr,"(%i) Reading next line %i: %s\n",me, line, dummy1);
 | 
|---|
 | 40 |                 dummy = strchr(dummy1,'\t');    // set dummy on first tab
 | 
|---|
 | 41 |                 if (dummy == NULL) {
 | 
|---|
 | 42 |                         dummy = strchr(dummy1, '\n');   // set on line end then (whole line = keyword)
 | 
|---|
 | 43 |                         //fprintf(stderr,"(%i)Error: Cannot find tabs on line %i in search for %s\n", me, line, name);
 | 
|---|
 | 44 |             //Error(FileOpenParams, NULL);                      
 | 
|---|
 | 45 |                 } else {
 | 
|---|
 | 46 |                         //fprintf(stderr,"(%i) found tab at %i\n",me,(int)dummy-(int)dummy1);
 | 
|---|
 | 47 |                 }
 | 
|---|
 | 48 |                 if ((dummy-dummy1 >= 3) && (strncmp(dummy1, name, strlen(name)) == 0)) {
 | 
|---|
 | 49 |                         found = 1; // found the parameter!
 | 
|---|
 | 50 |                         //fprintf(stderr,"(%i) found %s at line %i\n",me, name, line);          
 | 
|---|
 | 51 |                         
 | 
|---|
 | 52 |                         for (i=0;i<xth;i++) {   // i = rows
 | 
|---|
 | 53 |                                 if (type >= 3) {
 | 
|---|
 | 54 |                                         // grid structure means, grid begins in the next line, not after keyword
 | 
|---|
 | 55 |                                         dummy1 = dummy = free_dummy;
 | 
|---|
 | 56 |                                         do {
 | 
|---|
 | 57 |                                                 fgets(dummy1, 256, file);       // Read the whole line, skips commentary and empty ones
 | 
|---|
 | 58 |                                                 line++;
 | 
|---|
 | 59 |                                         } while ((dummy1[0] == '#') || (dummy1[0] == '\n'));
 | 
|---|
 | 60 |                                         if (dummy1 == NULL){
 | 
|---|
 | 61 |                                                 fprintf(stderr,"(%i) Error reading line %i\n", me, line);
 | 
|---|
 | 62 |                                         } else {
 | 
|---|
 | 63 |                                                 //fprintf(stderr,"(%i) Reading next line %i: %s\n", me, line, dummy1);
 | 
|---|
 | 64 |                                         }
 | 
|---|
 | 65 |                                 } else {
 | 
|---|
 | 66 |                                         while (*dummy == '\t')
 | 
|---|
 | 67 |                                                 dummy++;
 | 
|---|
 | 68 |                                 }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |                                 for (j=0;j<yth;j++) { // j = columns
 | 
|---|
 | 71 |                                         // check for lower triangular area and upper triangular area
 | 
|---|
 | 72 |                                         if ( ((i > j) && (type == upper_trigrid)) || ((j > i) && (type == lower_trigrid))) {
 | 
|---|
 | 73 |                                                 *((double *)value) = 0.0;
 | 
|---|
 | 74 |                                                 fprintf(stderr,"%f\t",*((double *)value));
 | 
|---|
 | 75 |                                                 value += sizeof(double);
 | 
|---|
 | 76 |                                         } else {
 | 
|---|
 | 77 |                                                 dummy1 = dummy;
 | 
|---|
 | 78 |                                                 dummy = strchr(dummy1, '\t');   // seek for tab
 | 
|---|
 | 79 |                                                 if (dummy == NULL) {
 | 
|---|
 | 80 |                                                         dummy = strchr(dummy1, '\n');    // at line end then
 | 
|---|
 | 81 |                                                         if ((j < yth-1) && (type < 4)) {        // check if xth value or not yet
 | 
|---|
 | 82 |                                                                 fprintf(stderr,"(%i)Error: EoL at %i and still missing %i values %d for parameter %s\n", me, line, yth-j, name);
 | 
|---|
 | 83 |                                                     //Error(FileOpenParams, NULL);                      
 | 
|---|
 | 84 |                                                         }
 | 
|---|
 | 85 |                                                 } else {
 | 
|---|
 | 86 |                                                         //fprintf(stderr,"(%i) found tab at %i\n",me,(int)dummy-(int)free_dummy);
 | 
|---|
 | 87 |                                                 }
 | 
|---|
 | 88 |                                                 //fprintf(stderr,"(%i) value from %i to %i\n",me,(int)dummy1-(int)free_dummy,(int)dummy-(int)free_dummy);
 | 
|---|
 | 89 |                                                 switch(type) { 
 | 
|---|
 | 90 |                                                         case(grid):
 | 
|---|
 | 91 |                                                         case(lower_trigrid):
 | 
|---|
 | 92 |                                                         case(upper_trigrid):
 | 
|---|
 | 93 |                                                                 *((double *)value) = atof(dummy1);
 | 
|---|
 | 94 |                                                                 if ((i==0) && (j==0))   fprintf(stderr,"(%i)%s = ",me, name);
 | 
|---|
 | 95 |                                                                 fprintf(stderr,"%f\t",*((double *)value));
 | 
|---|
 | 96 |                                                                 value += sizeof(double);
 | 
|---|
 | 97 |                                                                 break;
 | 
|---|
 | 98 |                                                         case(double_type):
 | 
|---|
 | 99 |                                                                 *((double *)value) = atof(dummy1);
 | 
|---|
 | 100 |                                                           if (i == xth-1) fprintf(stderr,"(%i)%s = %f\n",me, name, *((double *) value));
 | 
|---|
 | 101 |                                                                 //value += sizeof(double);
 | 
|---|
 | 102 |                                                                 break;
 | 
|---|
 | 103 |                                                         case(int_type):
 | 
|---|
 | 104 |                                                                 *((int *)value) = atoi(dummy1);
 | 
|---|
 | 105 |                                                           if (i == xth-1) fprintf(stderr,"(%i)%s = %i\n",me, name, *((int *) value));
 | 
|---|
 | 106 |                                                                 //value += sizeof(int);
 | 
|---|
 | 107 |                                                                 break;
 | 
|---|
 | 108 |                                                         default:
 | 
|---|
 | 109 |                                                         case(string_type):
 | 
|---|
 | 110 |                                                                 strncpy((char *)value, dummy1, dummy-dummy1);
 | 
|---|
 | 111 |                                                                 fprintf(stderr,"(%i)%s is '%s'\n",me,name,((char *) value));
 | 
|---|
 | 112 |                                                                 //value += sizeof(char);
 | 
|---|
 | 113 |                                                         break;
 | 
|---|
 | 114 |                                                 }
 | 
|---|
 | 115 |                                         }
 | 
|---|
 | 116 |                                         while (*dummy == '\t')
 | 
|---|
 | 117 |                                                 dummy++;
 | 
|---|
 | 118 |                                 }
 | 
|---|
 | 119 |                                 //fprintf(stderr,"\n");
 | 
|---|
 | 120 |                         }
 | 
|---|
 | 121 |                 }
 | 
|---|
 | 122 |         }       
 | 
|---|
 | 123 |         free(free_dummy);
 | 
|---|
 | 124 |         fseek(file, file_position, SEEK_SET);   // rewind to start position
 | 
|---|
 | 125 |         //fprintf(stderr, "(%i) End of Parsing\n\n",me);
 | 
|---|
 | 126 |         
 | 
|---|
 | 127 |         return (found); // true if found, false if not
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | int main(int argc, char **argv) {
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 |         FILE *cpiInputFile;
 | 
|---|
 | 133 |         if (argc < 1) {
 | 
|---|
 | 134 |       printf("Cant't find pseudopot path or file\n");
 | 
|---|
 | 135 |       return 255;
 | 
|---|
 | 136 |         }
 | 
|---|
 | 137 |     cpiInputFile = fopen(argv[1],"r");
 | 
|---|
 | 138 |     if (cpiInputFile == NULL) {
 | 
|---|
 | 139 |       printf("Cant't find pseudopot path or file\n");
 | 
|---|
 | 140 |       return 255;
 | 
|---|
 | 141 |     }
 | 
|---|
 | 142 |     double test;
 | 
|---|
 | 143 |         ParseForParameter(cpiInputFile, NULL, 1, 1, double_type, &test);
 | 
|---|
 | 144 |         printf("Parsed Value: %f", test);
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 |         return 1;       
 | 
|---|
 | 147 | }
 | 
|---|