- Main Page
- Data Structures
- Files

- File List
- Globals

# jasperserver/attachment.c

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2007 JasperSoft http://www.jaspersoft.com
    00003  *
    00004  * This program is free software; you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation; either version 2 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed WITHOUT ANY WARRANTY; and without the
    00010  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00011  * See the GNU General Public License for more details.
    00012  *
    00013  * You should have received a copy of the GNU General Public License
    00014  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
    00015  * or write to:
    00016  *
    00017  * Free Software Foundation, Inc.,
    00018  * 59 Temple Place - Suite 330,
    00019  * Boston, MA  USA  02111-1307
    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; // must set to non-NULL (this is our fd handle which we need in the callbacks)
    00056         attachment.__size = sb.st_size; // must set size
    00057     }
    00058     else
    00059     {
    00060         fprintf(stderr,"Unable to find the size of the file %s", fileName);
    00061         return 4; // Unable to find file size...
    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      //printf("File opened %x", handle);
    00079      //fflush(stdout);
    00080      return handle;
    00081 }
    00082
    00083 void jasperserver_dime_read_close(struct soap *soap, void *handle)
    00084 {
    00085      //printf("File closed!");
    00086      //fflush(stdout);
    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   //printf("File read %ul!", s);
    00094   //fflush(stdout);
    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         //printf("\nSize %ul\n", *sizePtr);
    00122         //printf("\nAddress %x\n", sizePtr);
    00123         //printf("\nSize %ul\n\n", sb.st_size);
    00124
    00125         fread(*dataPtr, 1, sb.st_size, fd);
    00126     }
    00127     fclose(fd);
    00128     return 0;
    00129 }