Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/emuware/EW_state.cpp
2 views
1
#include "EW_state.h"
2
#include <cstring>
3
#include <algorithm>
4
#include <stdarg.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
8
namespace EW {
9
10
NewStateDummy::NewStateDummy()
11
:length(0)
12
{
13
}
14
void NewStateDummy::Save(const void *ptr, size_t size, const char *name)
15
{
16
length += size;
17
}
18
void NewStateDummy::Load(void *ptr, size_t size, const char *name)
19
{
20
}
21
22
NewStateExternalBuffer::NewStateExternalBuffer(char *buffer, long maxlength)
23
:buffer(buffer), length(0), maxlength(maxlength)
24
{
25
}
26
27
void NewStateExternalBuffer::Save(const void *ptr, size_t size, const char *name)
28
{
29
if (maxlength - length >= (long)size)
30
{
31
std::memcpy(buffer + length, ptr, size);
32
}
33
length += size;
34
}
35
36
void NewStateExternalBuffer::Load(void *ptr, size_t size, const char *name)
37
{
38
char *dst = static_cast<char *>(ptr);
39
if (maxlength - length >= (long)size)
40
{
41
std::memcpy(dst, buffer + length, size);
42
}
43
length += size;
44
}
45
46
NewStateExternalFunctions::NewStateExternalFunctions(const FPtrs *ff)
47
:Save_(ff->Save_),
48
Load_(ff->Load_),
49
EnterSection_(ff->EnterSection_),
50
ExitSection_(ff->ExitSection_)
51
{
52
}
53
54
void NewStateExternalFunctions::Save(const void *ptr, size_t size, const char *name)
55
{
56
Save_(ptr, size, name);
57
}
58
void NewStateExternalFunctions::Load(void *ptr, size_t size, const char *name)
59
{
60
Load_(ptr, size, name);
61
}
62
63
void NewStateExternalFunctions::EnterSection(const char *name, ...)
64
{
65
//analysis: multiple passes to generate string not ideal, but there arent many sections.. so it should be OK. improvement would be special vararg overload
66
va_list ap;
67
va_start(ap,name);
68
char easybuf[32];
69
int size = vsnprintf(easybuf,0,name,ap);
70
char *ptr = easybuf;
71
if(size>31)
72
ptr = (char*)malloc(size+1);
73
vsprintf(ptr,name,ap);
74
EnterSection_(ptr);
75
if(ptr != easybuf)
76
free(ptr);
77
va_end(ap);
78
}
79
void NewStateExternalFunctions::ExitSection(const char *name, ...)
80
{
81
va_list ap;
82
va_start(ap,name);
83
char easybuf[32];
84
int size = vsnprintf(easybuf,0,name,ap);
85
char *ptr = easybuf;
86
if(size>31)
87
ptr = (char*)malloc(size+1);
88
vsprintf(ptr,name,ap);
89
ExitSection_(ptr);
90
if(ptr != easybuf)
91
free(ptr);
92
va_end(ap);
93
}
94
95
96
}
97
98