Blender V2.61 - r43446

runtime.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  *
00027  */
00028 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <fcntl.h>
00039 #include <errno.h>
00040 
00041 #ifdef WIN32
00042 #include <io.h>     // read, open
00043 #include "BLI_winstuff.h"
00044 #else // ! WIN32
00045 #include <unistd.h>     // read
00046 #endif
00047 
00048 #include "BLO_readfile.h"
00049 #include "BLO_runtime.h"
00050 
00051 #include "BKE_blender.h"
00052 #include "BKE_report.h"
00053 #include "BKE_utildefines.h"
00054 
00055 #include "BLI_blenlib.h"
00056 
00057 /* Runtime reading */
00058 
00059 static int handle_read_msb_int(int handle)
00060 {
00061     unsigned char buf[4];
00062 
00063     if(read(handle, buf, 4) != 4)
00064         return -1;
00065 
00066     return (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + (buf[3]<<0);
00067 }
00068 
00069 int BLO_is_a_runtime(const char *path)
00070 {
00071     int res= 0, fd= open(path, O_BINARY|O_RDONLY, 0);
00072     int datastart;
00073     char buf[8];
00074 
00075     if(fd==-1)
00076         goto cleanup;
00077     
00078     lseek(fd, -12, SEEK_END);
00079     
00080     datastart= handle_read_msb_int(fd);
00081 
00082     if(datastart==-1)
00083         goto cleanup;
00084     else if(read(fd, buf, 8)!=8)
00085         goto cleanup;
00086     else if(memcmp(buf, "BRUNTIME", 8)!=0)
00087         goto cleanup;
00088     else
00089         res= 1;
00090 
00091 cleanup:
00092     if(fd!=-1)
00093         close(fd);
00094 
00095     return res; 
00096 }
00097 
00098 BlendFileData *BLO_read_runtime(const char *path, ReportList *reports)
00099 {
00100     BlendFileData *bfd= NULL;
00101     size_t actualsize;
00102     int fd, datastart;
00103     char buf[8];
00104 
00105     fd= open(path, O_BINARY|O_RDONLY, 0);
00106 
00107     if(fd==-1) {
00108         BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", path, strerror(errno));
00109         goto cleanup;
00110     }
00111     
00112     actualsize= BLI_file_descriptor_size(fd);
00113 
00114     lseek(fd, -12, SEEK_END);
00115 
00116     datastart= handle_read_msb_int(fd);
00117 
00118     if(datastart==-1) {
00119         BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (problem seeking)", path);
00120         goto cleanup;
00121     }
00122     else if(read(fd, buf, 8)!=8) {
00123         BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (truncated header)", path);
00124         goto cleanup;
00125     }
00126     else if(memcmp(buf, "BRUNTIME", 8)!=0) {
00127         BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (not a blend file)", path);
00128         goto cleanup;
00129     }
00130     else {  
00131         //printf("starting to read runtime from %s at datastart %d\n", path, datastart);
00132         lseek(fd, datastart, SEEK_SET);
00133         bfd = blo_read_blendafterruntime(fd, path, actualsize-datastart, reports);
00134         fd= -1; // file was closed in blo_read_blendafterruntime()
00135     }
00136     
00137 cleanup:
00138     if(fd!=-1)
00139         close(fd);
00140     
00141     return bfd;
00142 }
00143