[66] | 1 | /* |
---|
| 2 | * ---------------------------------------------------------------------- |
---|
| 3 | * FUNCTION: rpResult |
---|
| 4 | * |
---|
| 5 | * Clients call this function at the end of their simulation, to |
---|
| 6 | * pass the simulation result back to the Rappture GUI. It writes |
---|
| 7 | * out the given XML object to a runXXX.xml file, and then writes |
---|
| 8 | * out the name of that file to stdout. |
---|
| 9 | * ====================================================================== |
---|
| 10 | * AUTHOR: Michael McLennan, Purdue University |
---|
[3177] | 11 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[115] | 12 | * |
---|
| 13 | * See the file "license.terms" for information on usage and |
---|
| 14 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[66] | 15 | * ====================================================================== |
---|
| 16 | */ |
---|
| 17 | #include <stdlib.h> |
---|
| 18 | #include <time.h> |
---|
[1018] | 19 | #include <RpLibrary.h> |
---|
| 20 | #include <errno.h> |
---|
[6705] | 21 | #include <limits.h> |
---|
| 22 | #include <unistd.h> |
---|
[66] | 23 | |
---|
| 24 | void |
---|
[1018] | 25 | rpResult(RpLibrary* lib) |
---|
| 26 | { |
---|
[66] | 27 | char outputFile[100]; |
---|
[6705] | 28 | char currentWorkingDirectory[PATH_MAX+1]; |
---|
| 29 | char *cwd; |
---|
[1018] | 30 | std::string xtext; |
---|
[66] | 31 | FILE* fp; |
---|
| 32 | time_t t; |
---|
| 33 | |
---|
[1018] | 34 | xtext = lib->xml(); |
---|
[66] | 35 | |
---|
[6705] | 36 | // get current working directory |
---|
| 37 | cwd = getcwd(currentWorkingDirectory,PATH_MAX); |
---|
| 38 | |
---|
[66] | 39 | // create output filename |
---|
| 40 | snprintf(outputFile, 50, "run%d.xml", (int)time(&t)); |
---|
| 41 | |
---|
| 42 | // write out the result file |
---|
| 43 | fp = fopen(outputFile, "w"); |
---|
| 44 | if(!fp) { |
---|
[1382] | 45 | fprintf(stderr,"can't save results: %s\n", strerror(errno)); |
---|
[66] | 46 | return; |
---|
| 47 | } |
---|
[1018] | 48 | int fsize = fwrite(xtext.c_str(), xtext.length(), sizeof(char), fp); |
---|
| 49 | if (fsize != (int)xtext.length()) { |
---|
[1382] | 50 | fprintf(stderr, "short write: can't save results: %s\n", |
---|
[6705] | 51 | strerror(errno)); |
---|
[1018] | 52 | fclose(fp); |
---|
| 53 | return; |
---|
| 54 | } |
---|
[66] | 55 | fclose(fp); |
---|
| 56 | // tell Rappture the file name |
---|
[6705] | 57 | if (cwd != NULL) { |
---|
| 58 | printf("=RAPPTURE-RUN=>%s/%s\n", currentWorkingDirectory, outputFile); |
---|
| 59 | } else { |
---|
| 60 | printf("=RAPPTURE-RUN=>%s\n", outputFile); |
---|
| 61 | } |
---|
[66] | 62 | } |
---|