//////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Code Written by David Saenz at The University of Texas at El Paso // // Project supported by the Network for Computational Nanotechnology (NCN) //-------------------------------------------------------------------------------------------------------------- // Rappturizer.c - Version 1.0 // // This program will take user's input according to a desired application objective and automatically // generate an XML file for the rappture interface, as well as the Rappturized program template for the user // to just copy/paste his code where indicated and be able to build a fast rappture application. // The user will have to declare constants and include the required libraries on the file generated. // // This version will only support programs written in C. Future versions will support more sources. // // For any Q&A contact me at my nanoHUB account. (David Saenz from UTEP) // //////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #define L 100 void openFile(void); int main() { FILE *xmlFile; FILE *programFile; int source; int input_type=0; int input_no; int output_no_loop; int input_bool; int input_int; float input_default=0; float input_min; float input_max; char *dummy = "%f"; char *dummy2 = "\n"; char title[L]; char about[L]; char command[L]; char input_token[L]; char input_label[L][L]; char sweep[L]; char output_label[L][L]; char input_description[L]; char output_min[L]; char output_max[L]; char input_string[L]; int i=1,j=0; xmlFile = fopen("tool.xml","a+"); /* apend file (add text to a file or create a file if it does not exist.*/ fprintf(xmlFile, "\n\n\n\n"); printf("\n\nRappturizer V 1.0\n--------------------------\n"); printf("Enter the program title (Ex. for \"plot.c\" => type \"plot\"):"); scanf("%s",title); printf("Source:\n1.- C"); //\n2.- MatLab\n3.- Fortran\n"); Options for codes in Matlab and Fortran available in the future. SOURCE: scanf("%i",&source); //print some rappture common lines switch (source) { case 1: fprintf(xmlFile, "@tool/%s @driver\n", title); strcat(title,".c"); programFile = fopen(title,"a+"); break; case 2: fprintf(xmlFile, "matlab -nodisplay -r infile='@driver',path('@tool',path),%s\n", title); strcat(title,".m"); break; case 3: strcat(title,".f"); programFile = fopen(title,"a+"); fprintf(xmlFile, "@tool/%s @driver\n", title); break; default: printf("Option not supported, try again:\n"); goto SOURCE; break; } //Print Variable initialization in the rappture file /////////////////////////////////////////////////////////////////////////////////////////////// fprintf(programFile,"#include \"rappture.h\"\n#include \n#include \n#include \n\n"); fprintf(programFile,"int main(int argc, char * argv[]){\n\n"); fprintf(programFile,"RpLibrary* lib = NULL;\nconst char* data = NULL;\nchar line[100];\nint err = 0;\n\n"); fprintf(programFile,"lib = rpLibrary(argv[1]);\nif (lib == NULL) {\n\t// cannot open file or out of memory\n"); fprintf(programFile,"\tprintf(\"FAILED creating Rappture Library\");\n\treturn(1);\n }\n\n"); ////////////////////////////////////////////////////////////////////////////////////////////// printf("\nType a comment related to the program:"); getchar(); gets(about); //Print some object characteristics in the rappturized program file fprintf(xmlFile, "%s\n", title); fprintf(xmlFile, "%s\n", about); fprintf(xmlFile, "\n", about); //Prompt user to start input section printf("\nStart of input section"); printf("\n------------------------"); fprintf(xmlFile, "\n\n\n"); //Prompt user fir input number printf("\nHow many inputs does your program has?"); scanf("%i",&input_no); //Input loop for (i=1 ; i<(input_no+1); i++) { printf("\nInput #%i:",i); printf("\n--------------"); printf("\nType the name of the input variable (no spaces):"); scanf("%s",input_label[i]); //Print some lines related to input translation in the program file. fprintf(programFile,"//Read input and display error message if cannot be read\n"); fprintf(programFile,"rpGetString(lib,\"input.number(n%i).current\",&data);\n",i); //Prompt user for input type INPUT: printf("\nWhat type of input is it?"); printf("\n1.- Number.\n2.- String.\n3.- Boolean.\n4.- Integer.\n"); scanf("%i",&input_type); //Print input label accordingly, as well as scan variable from the rappture interface. switch (input_type) { case 1: fprintf(xmlFile, "\n",i); fprintf(programFile," float %s = rpConvertDbl(data, \"\", &err);\n",input_label[i]); break; case 2: fprintf(xmlFile, "\n",i); fprintf(programFile," char %s = rpConvertDbl(data, \"\", &err);\n",input_label[i]); break; case 3: fprintf(xmlFile, "\n",i); fprintf(programFile," bool %s = rpConvertDbl(data, \"\", &err);\n",input_label[i]); break; case 4: fprintf(xmlFile, "\n",i); fprintf(programFile," int %s = rpConvertDbl(data, \"\", &err);\n",input_label[i]); break; default: printf("\nPlease choose a valid option."); goto INPUT; break; } //Open about section and print label in XML file. fprintf(xmlFile, " \n"); fprintf(xmlFile, " \n", input_label[i]); //Print some error handling lines in the program file. fprintf(programFile," if (err) {\n printf (\"Error while retrieving input.number(n%i).current\");\n", i); fprintf(programFile," return(1);\n }\n\n"); //Scan description for input parameter. This text will be dsplayed on the rapptre interface when the mouse hovers over the parameter. printf("\nType a description for the input parameter:"); getchar(); gets(input_description); //Print description and close field on the XML file. fprintf(xmlFile, " %s\n", input_description); fprintf(xmlFile, " \n"); //Scan some object specifics. Some characteristics have been commented for future upgrade and program simplicity. switch (input_type) { case 1: //printf("Minimum allowed value:"); //scanf("%f",&input_min); //fprintf(xmlFile, " %f\n",input_min); //printf("Maximum allowed value:"); //scanf("%f",&input_max); //fprintf(xmlFile, " %f\n",input_max); printf("\nDefault value:"); scanf("%f",&input_default); fprintf(xmlFile, " %f\n", input_default); break; case 2: printf("\nDefault string:"); gets(input_string); fprintf(xmlFile, " %s\n", input_string); break; case 3: BOOL: printf("\nDefault value:\n1.- True/Yes\n0.- False/No"); scanf("%i",&input_bool); fprintf(xmlFile, " %i\n", input_bool); if ((input_bool != 0) && (input_bool != 1)) {printf("\nPlease choose a supported option:"); goto BOOL;} break; case 4: printf("\nMinimum value:"); scanf("%f",&input_min); fprintf(xmlFile, " %f\n",input_min); printf("\nMaximum value:"); scanf("%f",&input_max); fprintf(xmlFile, " %f\n",input_max); printf("\nDefault value:"); scanf("%f",&input_default); fprintf(xmlFile, " %f\n", input_default); break; default: printf("\nPlease choose a valid option."); goto INPUT; break; } //Closing object lines. switch (input_type) { case 1: fprintf(xmlFile, "\n\n",i); break; case 2: fprintf(xmlFile, "\n\n",i); break; case 3: fprintf(xmlFile, "\n\n",i); break; case 4: fprintf(xmlFile, "\n\n",i); break; default: break; } } //Inform user about output section start printf("\nStarting Output Section:"); printf("\n------------------------"); //Prompt user for output number (only numeric) printf("\nHow many outputs does your program has?"); printf("\n(This program only supports numeric outputs):"); scanf("%i",&output_no_loop); //Output loop for (i=1 ; i<(output_no_loop+1); i++) { printf("\nWhat is the name of the Output #%i:",i); scanf("%s",output_label[i]); fprintf(programFile,"float %s;\n",output_label[i]); } //Initialize looping variable in the written file fprintf(programFile,"int i = 0;\n\n"); //Get information about the output graph printf("\nFormatting the output graph:"); printf("\n-----------------------------"); printf("\nEnter the name of the independent variable you want to sweep through:"); scanf("%s",sweep); printf("\nEnter the minimum plot bound:"); scanf("%s",output_min); printf("\nEnter the maximum plot bound:"); scanf("%s",output_max); for (i=1; i<(output_no_loop+1); i++) { //Declare curve in the written file. fprintf(programFile,"rpPutString (lib,\"output.curve(%s).about.label\",\"%s\",RPLIB_OVERWRITE );\n",output_label[i], output_label[i]); fprintf(programFile,"rpPutString (lib,\"output.curve(%s).xaxis.label\",\"%s\",RPLIB_OVERWRITE );\n",output_label[i], sweep); fprintf(programFile,"rpPutString (lib,\"output.curve(%s).yaxis.label\",\"%s\",RPLIB_OVERWRITE );\n",output_label[i], output_label[i]); fprintf(programFile,"\n"); } //Loop over the variable to sweep on the written file. This is where the user inserts his code. //To get the output array, the calculations are performed seceral times with different inputs, and outputs are appended to an output file. fprintf(programFile,"\nfor(%s=%s;%s<(%s+1);%s++){\n",sweep,output_min,sweep,output_max,sweep); fprintf(programFile,"/////////////////////////////////////////////////////////////////\n"); fprintf(programFile,"//Place your code here!!!\n//Also declare any other variables you are using and constants defined.\n"); //Print inputs/outputs for user acquaintance. fprintf(programFile,"\n//Inputs used:\n//--------------"); for (i = 1; i < (input_no+1); i++) { fprintf(programFile,"\n//%i .- %s", i, input_label[i]); } fprintf(programFile,"\n//Outputs used:\n//--------------"); for (i = 1; i < (output_no_loop+1); i++) { fprintf(programFile,"\n//%i .- %s", i, output_label[i]); } //Print some new lines to separate "program paste section". fprintf(programFile,"\n\n\n\n\n\n\n\n\n\n\n\n\n/////////////////////////////////////////////////////////////////\n"); //Write results on each output curve for (i=1; i<(output_no_loop+1); i++){ fprintf(programFile,"\nsprintf(line,\"%s %s\\n\",%s, %s);\n",dummy,dummy,sweep,output_label[i]); fprintf(programFile,"rpPutString(lib,\"output.curve(%s).component.xy\", line, RPLIB_APPEND);\n", output_label[i]); } //Write some lines necessary to end the code. fprintf(programFile,"\n}\n\n"); fprintf(programFile,"rpResult(lib);\nreturn 0;\n}\n"); //Close & Finalize Files fprintf(xmlFile, "\n"); fprintf(xmlFile, "\n\n"); //Close XML file fclose(xmlFile); //Close program File. fclose(programFile); //Display Success Messege. printf("\n\nRun success!!\n\n\"tool.xml\" and \"%s\" File generated in working directory\n", title); printf("\n \"Now:\n1.- Copy - paste your code in the file\n2.- Define your constants & include libraries\n3.- Check the program for possible mistakes\" "); printf("\n Good Luck!! \n\nPress enter to exit...\n\n"); //This command holds the terminal window until enter is pressed. getchar(); return 0; }