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