Blender  V2.93
logmemfile.c
Go to the documentation of this file.
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  * Copyright 2006 Joseph Eagar <joeedh@gmail.com>
17  */
18 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "logImageCore.h"
30 #include "logmemfile.h"
31 
32 int logimage_fseek(LogImageFile *logFile, intptr_t offset, int origin)
33 {
34  if (logFile->file) {
35  fseek(logFile->file, offset, origin);
36  }
37  else { /* we're seeking in memory */
38  if (origin == SEEK_SET) {
39  if (offset > logFile->memBufferSize) {
40  return 1;
41  }
42  logFile->memCursor = logFile->memBuffer + offset;
43  }
44  else if (origin == SEEK_END) {
45  if (offset > logFile->memBufferSize) {
46  return 1;
47  }
48  logFile->memCursor = (logFile->memBuffer + logFile->memBufferSize) - offset;
49  }
50  else if (origin == SEEK_CUR) {
51  uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
52  if (pos + offset > logFile->memBufferSize) {
53  return 1;
54  }
55 
56  logFile->memCursor += offset;
57  }
58  }
59  return 0;
60 }
61 
62 int logimage_fwrite(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
63 {
64  if (logFile->file) {
65  return fwrite(buffer, size, count, logFile->file);
66  }
67  /* we're writing to memory */
68  /* do nothing as this isn't supported yet */
69  return count;
70 }
71 
72 int logimage_fread(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
73 {
74  if (logFile->file) {
75  return fread(buffer, size, count, logFile->file);
76  }
77  /* we're reading from memory */
78  unsigned char *buf = (unsigned char *)buffer;
79  uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
80  size_t total_size = size * count;
81  if (pos + total_size > logFile->memBufferSize) {
82  /* how many elements can we read without overflow ? */
83  count = (logFile->memBufferSize - pos) / size;
84  /* recompute the size */
85  total_size = size * count;
86  }
87 
88  if (total_size != 0) {
89  memcpy(buf, logFile->memCursor, total_size);
90  }
91 
92  return count;
93 }
94 
95 int logimage_read_uchar(unsigned char *x, LogImageFile *logFile)
96 {
97  uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
98  if (pos + sizeof(unsigned char) > logFile->memBufferSize) {
99  return 1;
100  }
101 
102  *x = *(unsigned char *)logFile->memCursor;
103  logFile->memCursor += sizeof(unsigned char);
104  return 0;
105 }
106 
107 int logimage_read_ushort(unsigned short *x, LogImageFile *logFile)
108 {
109  uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
110  if (pos + sizeof(unsigned short) > logFile->memBufferSize) {
111  return 1;
112  }
113 
114  *x = *(unsigned short *)logFile->memCursor;
115  logFile->memCursor += sizeof(unsigned short);
116  return 0;
117 }
118 
119 int logimage_read_uint(unsigned int *x, LogImageFile *logFile)
120 {
121  uintptr_t pos = (uintptr_t)logFile->memCursor - (uintptr_t)logFile->memBuffer;
122  if (pos + sizeof(unsigned int) > logFile->memBufferSize) {
123  return 1;
124  }
125 
126  *x = *(unsigned int *)logFile->memCursor;
127  logFile->memCursor += sizeof(unsigned int);
128  return 0;
129 }
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
uint pos
int count
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
int logimage_read_ushort(unsigned short *x, LogImageFile *logFile)
Definition: logmemfile.c:107
int logimage_read_uchar(unsigned char *x, LogImageFile *logFile)
Definition: logmemfile.c:95
int logimage_fseek(LogImageFile *logFile, intptr_t offset, int origin)
Definition: logmemfile.c:32
int logimage_read_uint(unsigned int *x, LogImageFile *logFile)
Definition: logmemfile.c:119
int logimage_fread(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
Definition: logmemfile.c:72
int logimage_fwrite(void *buffer, size_t size, unsigned int count, LogImageFile *logFile)
Definition: logmemfile.c:62
_W64 unsigned int uintptr_t
Definition: stdint.h:122
_W64 int intptr_t
Definition: stdint.h:121
uintptr_t memBufferSize
Definition: logImageCore.h:82
unsigned char * memBuffer
Definition: logImageCore.h:81
unsigned char * memCursor
Definition: logImageCore.h:83