00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "attachment.h"
00023 #include <sys/stat.h>
00024
00025 int jasperserver_attach_file(struct soap* soap,const char* fileName)
00026 {
00027 struct stat sb;
00028 FILE* fd = NULL;
00029 jasperserver_attachment_t attachment;
00030
00031 if (!fileName){
00032 fprintf(stderr, "File to attach not specified!");
00033 return 1;
00034 }
00035
00036 if (!soap)
00037 {
00038 fprintf(stderr, "Soap struct NULL!");
00039 return 2;
00040 }
00041
00042 fd = fopen(fileName,"rb");
00043
00044 if (!fd)
00045 {
00046 fprintf(stderr, "File %s not found!",fileName);
00047 return 3;
00048 }
00049
00050 if (!fstat(fileno(fd), &sb) && sb.st_size > 0)
00051 {
00052 soap->fdimereadopen = jasperserver_dime_read_open;
00053 soap->fdimereadclose = jasperserver_dime_read_close;
00054 soap->fdimeread = jasperserver_dime_read;
00055 attachment.__ptr = (unsigned char*)fd;
00056 attachment.__size = sb.st_size;
00057 }
00058 else
00059 {
00060 fprintf(stderr,"Unable to find the size of the file %s", fileName);
00061 return 4;
00062 }
00063 attachment.id = "attachment";
00064 attachment.type = "application/octet-stream";
00065 attachment.options = soap_dime_option(soap, 0, "attachment");
00066
00067 if (soap_set_dime_attachment(soap, attachment.__ptr, attachment.__size, attachment.type, attachment.id, 0, attachment.options) != SOAP_OK)
00068 {
00069 printf("Error setting up DIME attachment");
00070 return 5;
00071 }
00072
00073 return 0;
00074 }
00075
00076 void *jasperserver_dime_read_open(struct soap *soap, void *handle, const char *id, const char *type, const char *options)
00077 {
00078
00079
00080 return handle;
00081 }
00082
00083 void jasperserver_dime_read_close(struct soap *soap, void *handle)
00084 {
00085
00086
00087 fclose((FILE*)handle);
00088 }
00089
00090 size_t jasperserver_dime_read(struct soap *soap, void *handle, char *buf, size_t len)
00091 {
00092 size_t s = fread(buf, 1, len, (FILE*)handle);
00093
00094
00095 return s;
00096 }
00097
00098
00099 int jasperserver_read_file(const char* fileName, char **dataPtr, size_t *sizePtr)
00100 {
00101 FILE* fd = NULL;
00102 struct stat sb;
00103
00104 if (!fileName){
00105 fprintf(stderr, "File to attach not specified!");
00106 return 1;
00107 }
00108
00109 fd = fopen(fileName,"rb");
00110
00111 if (!fd)
00112 {
00113 fprintf(stderr, "File %s not found!",fileName);
00114 return 3;
00115 }
00116
00117 if (!fstat(fileno(fd), &sb) && sb.st_size > 0)
00118 {
00119 *dataPtr = (char *)malloc(sb.st_size);
00120 *sizePtr = sb.st_size;
00121
00122
00123
00124
00125 fread(*dataPtr, 1, sb.st_size, fd);
00126 }
00127 fclose(fd);
00128 return 0;
00129 }