synched with trunk at revision 37212
[blender.git] / source / blender / imbuf / intern / cineon / logmemfile.c
1 /** \file blender/imbuf/intern/cineon/logmemfile.c
2  *  \ingroup imbcineon
3  */
4 /*
5  *       Cineon image file format library routines.
6  *
7  *       Copyright 2006 Joseph Eagar (joeedh@gmail.com)
8  *
9  *       This program is free software; you can redistribute it and/or modify it
10  *       under the terms of the GNU General Public License as published by the Free
11  *       Software Foundation; either version 2 of the License, or (at your option)
12  *       any later version.
13  *
14  *       This program is distributed in the hope that it will be useful, but
15  *       WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  *       or FITNESS FOR A PARTICULAR PURPOSE.    See the GNU General Public License
17  *       for more details.
18  *
19  *       You should have received a copy of the GNU General Public License
20  *       along with this program; if not, write to the Free Software
21  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "logImageCore.h"
29
30 #include "logmemfile.h" /* own include */
31
32 int logimage_fseek(void* logfile, intptr_t offsett, int origin)
33 {       
34         struct _Log_Image_File_t_ *file = (struct _Log_Image_File_t_*) logfile;
35         intptr_t offset = offsett;
36         
37         if (file->file) fseek(file->file, offset, origin);
38         else { /*we're seeking in memory*/
39                 if (origin==SEEK_SET) {
40                         if (offset > file->membuffersize) return 1;
41                         file->memcursor = file->membuffer + offset;
42                 } else if (origin==SEEK_END) {
43                         if (offset > file->membuffersize) return 1;
44                         file->memcursor = (file->membuffer + file->membuffersize) - offset;
45                 } else if (origin==SEEK_CUR) {
46                         uintptr_t pos = (uintptr_t)file->membuffer - (uintptr_t)file->memcursor;
47                         if (pos + offset > file->membuffersize) return 1;
48                         if (pos < 0) return 1;
49                         file->memcursor += offset;
50                 }
51         }
52         return 0;
53 }
54
55 int logimage_fwrite(void *buffer, unsigned int size, unsigned int count, void *logfile)
56 {
57         struct _Log_Image_File_t_ *file = (struct _Log_Image_File_t_*) logfile;
58         if (file->file) return fwrite(buffer, size, count, file->file);
59         else { /*we're writing to memory*/
60                 /*do nothing as this isn't supported yet*/
61                 return count;
62         }
63 }
64
65 int logimage_fread(void *buffer, unsigned int size, unsigned int count, void *logfile)
66 {
67         struct _Log_Image_File_t_ *file = (struct _Log_Image_File_t_*) logfile;
68         if (file->file) return fread(buffer, size, count, file->file);
69         else { /*we're reading from memory*/
70                 int i;
71                 /*we convert ot uchar just on the off chance some platform can't handle
72                   pointer arithmetic with type (void*). */
73                 unsigned char *buf = (unsigned char *) buffer; 
74                 
75                 for (i=0; i<count; i++) {
76                         memcpy(buf, file->memcursor, size);
77                         file->memcursor += size;
78                         buf += size;
79                 }
80                 return count;
81         }
82 }