Blender V2.27

BLO_readblenfile.c

Go to the documentation of this file.
00001 /*
00002  * $Id: BLO_readblenfile.c 33448 2010-12-03 17:05:21Z campbellbarton $
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  */
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_readblenfile.h"
00050 
00051 #include "BKE_blender.h"
00052 #include "BKE_report.h"
00053 
00054 #include "BLI_blenlib.h"
00055 
00057 const char *headerMagic = "BLENDFI";
00058 
00067 void BLO_setversionnumber(char array[4], int version)
00068 {
00069         memset(array, 0, sizeof(char)*4);
00070 
00071         array[1] = version / 100;
00072         array[2] = version % 100;
00073 }
00074 
00082 void BLO_setcurrentversionnumber(char array[4])
00083 {
00084         BLO_setversionnumber(array, BLENDER_VERSION);
00085 }
00086 
00087 #ifndef O_BINARY
00088 #define O_BINARY 0
00089 #endif
00090 
00091 /* Runtime reading */
00092 
00093 static int handle_read_msb_int(int handle) {
00094         unsigned char buf[4];
00095 
00096         if (read(handle, buf, 4)!=4)
00097                 return -1;
00098         else
00099                 return (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + (buf[3]<<0);
00100 }
00101 
00102 int blo_is_a_runtime(char *path) {
00103         int res= 0, fd= open(path, O_BINARY|O_RDONLY, 0);
00104         int datastart;
00105         char buf[8];
00106 
00107         if (fd==-1)
00108                 goto cleanup;
00109         
00110         lseek(fd, -12, SEEK_END);
00111         
00112         datastart= handle_read_msb_int(fd);
00113         if (datastart==-1)
00114                 goto cleanup;
00115         else if (read(fd, buf, 8)!=8)
00116                 goto cleanup;
00117         else if (memcmp(buf, "BRUNTIME", 8)!=0)
00118                 goto cleanup;
00119         else
00120                 res= 1;
00121 
00122 cleanup:
00123         if (fd!=-1)
00124                 close(fd);
00125 
00126         return res;     
00127 }
00128 
00129 BlendFileData *
00130 blo_read_runtime(
00131         char *path, 
00132         ReportList *reports)
00133 {
00134         BlendFileData *bfd= NULL;
00135         size_t actualsize;
00136         int fd, datastart;
00137         char buf[8];
00138 
00139         fd= open(path, O_BINARY|O_RDONLY, 0);
00140         if (fd==-1) {
00141                 BKE_reportf(reports, RPT_ERROR, "Unable to open \"%s\": %s.", path, strerror(errno));
00142                 goto cleanup;
00143         }
00144         
00145         actualsize= BLI_filesize(fd);
00146 
00147         lseek(fd, -12, SEEK_END);
00148 
00149         datastart= handle_read_msb_int(fd);
00150         if (datastart==-1) {
00151                 BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (problem seeking)", path);
00152                 goto cleanup;
00153         } else if (read(fd, buf, 8)!=8) {
00154                 BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (truncated header)", path);
00155                 goto cleanup;
00156         } else if (memcmp(buf, "BRUNTIME", 8)!=0) {
00157                 BKE_reportf(reports, RPT_ERROR, "Unable to read  \"%s\" (not a blend file)", path);
00158                 goto cleanup;
00159         } else {        
00160                 //printf("starting to read runtime from %s at datastart %d\n", path, datastart);
00161                 lseek(fd, datastart, SEEK_SET);
00162                 bfd = blo_read_blendafterruntime(fd, path, actualsize-datastart, reports);
00163                 fd= -1; // file was closed in blo_read_blendafterruntime()
00164         }
00165         
00166 cleanup:
00167         if (fd!=-1)
00168                 close(fd);
00169         
00170         return bfd;
00171 }
00172