// massage3.cpp vers. 0.42 adapted from loopget.cpp FAC May 23 2011 // Compile: g++ -g -Wall massage3.cpp -o massage3 // Usage: massage3 < rawLoopsOut.txt >outFile.txt // Copyright (C) 2011 F.A. Conle // This file is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the license, or (at // your option) any later version. // This file is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTA- // BILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public // License for more details. // You should have received a copy of the GNU General PUblic License along // with this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place -Suite 330, Boston, MA 02111-1307, USA. Try also their // web site: http://www.gnu.org/copyleft/gpl.html #include #include #include #include #include #include //used for sleep() in Linux. ? #include //Linux for sleep() #include //Linux req. exit() #include //Linux req. for strtol() int main(int argc, char ** argv ) //argc and argv are not used here. { FILE *envfile; //Read this initilization file char envname[50]; // No file required. Output to stdout. // FILE *bfile; //plot file // char binfilename[50]; // usually ssloopAll.tx char sentence[100]; //used to read lines from *.env file char sentence2[100]; //used to read lines from *.env file char *cfield1Ptr; //used to extract the first field from a sentence char *cfield2Ptr; //used to extract other fields int iret; long int nl_cycle; float volt0, volt1; float x0out, x1out; float chan0Inc, chan1Inc, chan0Cal,chan1Cal; float chan0IncTotal, chan1IncTotal; float scale2; //multiplier to convert int to +-10Volts float scale1; // " to convert +-10Volts to int scale1 = 3276.7; scale2 = 0.000305185; //Read in the saveLoopList.env file strcpy(envname, "massage.env\0"); printf("#\n#loopget: Opening envfile name: %s\n",envname); if ( (envfile = fopen (envname, "r") ) == NULL) { printf("# Error opening %s file. \n",envname); iret = 0; goto CLOSE; } //sentence is a char [100] size while (1) { fgets( sentence, 100, envfile ); // fgets needs \n at end of input if ( feof(stdin) ) goto ENDFILE; // check for EOF right after fgets() printf("#massage3: got: %s \n", sentence); cfield1Ptr = strtok (sentence, " "); //fetch 1st field // Note that strcmp(s1,s2) = 0 when s1=s2 if (strcmp ( cfield1Ptr, "#INCREMENT=\0") == 0) { printf("#massage3: Got #INCREMENT= \n"); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage3: Error: no 2nd field after #INCREMENT=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan0Inc ); printf ("#massage3: Chan0 Incm. for Loop plots = %10.5f \n", chan0Inc); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 3rd field was found { printf ("#massage3: Error: no 3rd field after #INCREMENT=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan1Inc ); printf ("#massage3: Chan1 Incm. for Loop plots = %10.5f \n", chan1Inc); continue; } if (strcmp (cfield1Ptr, "#CALIBRATE=\0") == 0) { printf("#massage3: Got #CALIBRATE= \n"); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage3: Error: no 2nd field after #CALIBRATE=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan0Cal ); printf ("#massage3: Chan0 Calib. for Loop plots = %f \n", chan0Cal); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 3rd field was found { printf ("#massage3: Error: no 3rd field after #CALIBRATE=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan1Cal ); printf ("#massage3: Chan1 Incm. for Loop plots = %f \n", chan1Cal); continue; } if (strcmp (cfield1Ptr, "#END=\0") == 0) // It appears that #BEGIN= 0 must have a "0" or other 2nd field on the line { // end of *.env file lines printf ("#massage3: Recognized #END= in *.env file\n" ); break; } // ignore any comment lines in *.env file } //end of while() for envfile read fclose(envfile); // The above "break" should place us here // Found a #END= chan0IncTotal = 0.; chan1IncTotal = 0.; GETLOOP: while (1) // Start reading standard input. Look for #cycle= { fgets( sentence, 100, stdin ); if ( feof(stdin) ) goto ENDFILE; // check for EOF right after fgets() //scanf ("%100s",sentence); //direct stdin input printf("#massage3: got: %s\n",sentence); cfield1Ptr = strtok (sentence, " "); //fetch 1st field // Note that strcmp(s1,s2) = 0 when s1=s2 if (strcmp ( cfield1Ptr, "#cycle=\0") == 0) { //printf("#massage3: Got #cycle= \n"); cfield2Ptr = strtok (NULL, " "); //fetch 2nd field if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage3: Error: no 2nd field after #cycle=\n"); goto STOP; } sscanf (cfield2Ptr, "%li", &nl_cycle ); //printf ("#massage3: Found #cylce= %li \n", nl_cycle); printf ("#cylce= %li \n", nl_cycle); break; } }// end of look for #cycle= while() loop. // //Open a file for this cycle // sprintf(binfilename, "ssloop%li\0", nl_cycle ); // // the null char "\0" above creates a warning in g++. Maybe extra? // printf("# Opening file = %s\n", binfilename); //debug print // if ( (bfile = fopen (binfilename, "w") ) == NULL) // { // printf("# Error opening loop file= %s\n", binfilename); // iret = 0; // goto CLOSE; // } // fprintf (bfile, "#INCREMENT= %f %f \n", chan0Inc, chan1Inc); // fprintf (bfile, "#CALIBRATE= %f %f \n", chan0Cal, chan1Cal); //Now read data volt0 and volt1 until we hit a #m in first field while (1) { fgets( sentence, 100, stdin ); if ( feof(stdin) ) goto ENDFILE; // check for EOF right after fgets() strcpy(sentence2, sentence); // save a copy for possible print //scanf ("%100s",sentence); //stdin input <- does not work. Only 1 field read cfield1Ptr = strtok (sentence, " "); if (strcmp (cfield1Ptr, "#m\0") ==0) { printf ( "%s\n",sentence2 ); break; } // If not #m then must still be data. No garbage check for now. sscanf (cfield1Ptr, "%f", &volt0 ); cfield2Ptr = strtok (NULL, " "); //fetch 2nd field if(cfield2Ptr == NULL) // no 2nd voltage found { printf ("#massage2: Error: no 2nd data field \n"); goto STOP; } sscanf (cfield2Ptr, "%f", &volt1 ); //sscanf (sentence, "%f%f", &volt0, &volt1 ); //Doesnt work x0out= ( volt0 + chan0IncTotal )*chan0Cal; x1out= ( volt1 + chan1IncTotal )*chan1Cal; //fprintf (bfile, "%f %f\n", x0out, x1out ); printf ("%f %f\n", x0out, x1out ); } //end of max min scan loop // fclose (bfile); chan0IncTotal = chan0IncTotal+chan0Inc; chan1IncTotal = chan1IncTotal+chan1Inc; goto GETLOOP; ENDFILE: printf("# End of input on stdin\n"); CLOSE: printf("# Closing files: .env\n"); fclose(envfile); // fclose(bfile); STOP: printf("#massage3: exits\n"); exit(1); } //end of main()