// massage2.cpp vers. 0.42 adapted from mstun.cpp FAC Feb 01 2011 // Compile: g++ -g -Wall massage2.cpp -o massage2 // Usage: massage2 < rawLoopsOut.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]; FILE *bfile; //plot file char binfilename[50]; // usually ssloop1, ssloop2, ... ssloop20 etc. 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 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("#massage2: 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("#massage2: Got #INCREMENT= \n"); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage2: Error: no 2nd field after #INCREMENT=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan0inc ); printf ("#massage2: Chan0 Incm. for Loop plots = %10.5f \n", chan0inc); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 3rd field was found { printf ("#massage2: Error: no 3rd field after #INCREMENT=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan1inc ); printf ("#massage2: Chan1 Incm. for Loop plots = %10.5f \n", chan1inc); continue; } if (strcmp (cfield1Ptr, "#CALIBRATE=\0") == 0) { printf("#massage2: Got #CALIBRATE= \n"); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage2: Error: no 2nd field after #CALIBRATE=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan0Cal ); printf ("#massage2: Chan0 Calib. for Loop plots = %f \n", chan0Cal); cfield2Ptr = strtok (NULL, " "); if (cfield2Ptr == NULL ) //then no 3rd field was found { printf ("#massage2: Error: no 3rd field after #CALIBRATE=\n"); goto STOP; } sscanf (cfield2Ptr, "%f", &chan1Cal ); printf ("#massage2: 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 ("#massage2: 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= 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() strcpy ( sentence2, sentence ); // keep a copy for possible printing //scanf ("%100s",sentence); //direct stdin input printf("#massage2: 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("#massage2: Got #cycle= \n"); cfield2Ptr = strtok (NULL, " "); //fetch 2nd field if (cfield2Ptr == NULL ) //then no 2nd field was found { printf ("#massage2: Error: no 2nd field after #cycle=\n"); goto STOP; } sscanf (cfield2Ptr, "%li", &nl_cycle ); //printf ("#massage2: 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); fprintf (bfile, "%s\n", sentence2 ); //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 ); // keep a copy for possible printing //scanf ("%100s",sentence); //stdin input <- does not work. Only 1 field read cfield1Ptr = strtok (sentence, " "); if (strcmp (cfield1Ptr, "#m\0") ==0) { fprintf (bfile, "%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"); fprintf (bfile, "#massage2: Error: no 2nd data field \n"); goto STOP; } sscanf (cfield2Ptr, "%f", &volt1 ); x0out= volt0*chan0Cal; x1out= volt1*chan1Cal; fprintf (bfile, "%f %f \n", x0out, x1out ); } //end of max min scan loop fclose (bfile); goto GETLOOP; ENDFILE: printf("# End of input on stdin\n"); CLOSE: printf("# Closing files: .env\n"); fclose(envfile); fclose(bfile); STOP: printf("#massage2: exits\n"); exit(1); } //end of main()