style cleanup: follow style guide for formatting of if/for/while loops, and else...
[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                 }
43                 else if (origin==SEEK_END) {
44                         if (offset > file->membuffersize) return 1;
45                         file->memcursor = (file->membuffer + file->membuffersize) - offset;
46                 }
47                 else if (origin==SEEK_CUR) {
48                         uintptr_t pos = (uintptr_t)file->membuffer - (uintptr_t)file->memcursor;
49                         if (pos + offset > file->membuffersize) return 1;
50                         if (pos < 0) return 1;
51                         file->memcursor += offset;
52                 }
53         }
54         return 0;
55 }
56
57 int logimage_fwrite(void *buffer, unsigned int size, unsigned int count, void *logfile)
58 {
59         struct _Log_Image_File_t_ *file = (struct _Log_Image_File_t_*) logfile;
60         if (file->file) return fwrite(buffer, size, count, file->file);
61         else { /*we're writing to memory*/
62                 /*do nothing as this isn't supported yet*/
63                 return count;
64         }
65 }
66
67 int logimage_fread(void *buffer, unsigned int size, unsigned int count, void *logfile)
68 {
69         struct _Log_Image_File_t_ *file = (struct _Log_Image_File_t_*) logfile;
70         if (file->file) return fread(buffer, size, count, file->file);
71         else { /*we're reading from memory*/
72                 int i;
73                 /* we convert ot uchar just on the off chance some platform can't handle
74                  * pointer arithmetic with type (void*). */
75                 unsigned char *buf = (unsigned char *) buffer; 
76                 
77                 for (i=0; i<count; i++) {
78                         memcpy(buf, file->memcursor, size);
79                         file->memcursor += size;
80                         buf += size;
81                 }
82                 return count;
83         }
84 }