1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2004 Blender Foundation.
17  * All rights reserved.
18  * external writefile function prototypes
19  */
20 
21 #pragma once
22 
23 /** \file
24  * \ingroup blenloader
25  */
26 
27 struct GHash;
28 struct Scene;
29 
30 typedef struct {
31   void *next, *prev;
32   const char *buf;
33   /** Size in bytes. */
34   size_t size;
35   /** When true, this chunk doesn't own the memory, it's shared with a previous #MemFileChunk */
36   bool is_identical;
37   /** When true, this chunk is also identical to the one in the next step (used by undo code to
38    * detect unchanged IDs).
39    * Defined when writing the next step (i.e. last undo step has those always false). */
40   bool is_identical_future;
41   /** Session UUID of the ID being currently written (MAIN_ID_SESSION_UUID_UNSET when not writing
42    * ID-related data). Used to find matching chunks in previous memundo step. */
43   uint id_session_uuid;
44 } MemFileChunk;
45 
46 typedef struct MemFile {
47   ListBase chunks;
48   size_t size;
49 } MemFile;
50 
51 typedef struct MemFileWriteData {
52   MemFile *written_memfile;
53   MemFile *reference_memfile;
54 
55   uint current_id_session_uuid;
56   MemFileChunk *reference_current_chunk;
57 
58   /** Maps an ID session uuid to its first reference MemFileChunk, if existing. */
59   struct GHash *id_session_uuid_mapping;
60 } MemFileWriteData;
61 
62 typedef struct MemFileUndoData {
63   char filename[1024]; /* FILE_MAX */
64   MemFile memfile;
65   size_t undo_size;
66 } MemFileUndoData;
67 
68 /* actually only used writefile.c */
69 
70 void BLO_memfile_write_init(MemFileWriteData *mem_data,
71                             MemFile *written_memfile,
72                             MemFile *reference_memfile);
73 void BLO_memfile_write_finalize(MemFileWriteData *mem_data);
74 
75 void BLO_memfile_chunk_add(MemFileWriteData *mem_data, const char *buf, size_t size);
76 
77 /* exports */
78 extern void BLO_memfile_free(MemFile *memfile);
79 extern void BLO_memfile_merge(MemFile *first, MemFile *second);
80 extern void BLO_memfile_clear_future(MemFile *memfile);
81 
82 /* utilities */
83 extern struct Main *BLO_memfile_main_get(struct MemFile *memfile,
84                                          struct Main *bmain,
85                                          struct Scene **r_scene);
86 extern bool BLO_memfile_write_file(struct MemFile *memfile, const char *filename);
87