Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revart & add vgsmml_compile_from_memory2 #8

Merged
merged 1 commit into from
Feb 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## Version 1.04
- revart: https://github.com/suzukiplan/vgs-mml-compiler/pull/6
- added: `vgsmml_compile_from_memory2` as a function without memory destruction

## Version 1.03
- ensures duplicate memory of MML string: https://github.com/AmamiyaRinyuki/vgs-bgm-plugins/issues/1

Expand Down
43 changes: 34 additions & 9 deletions src/vgsmml.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_file(const char* path, struct V
return result;
}

/* NOTE: YOU MUST SPECIFY THE NULL TERMINATED STRING TO THE DATA ARGUMENT */
struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size, struct VgsMmlErrorInfo* err)
{
struct VgsBgmData* result;
int nLine, cLine;
int i;
int* pos;
char* buf;
char* buf = (char*)data;

if (NULL == err) {
return NULL;
Expand All @@ -161,15 +162,12 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
return NULL;
}

/* ensures duplicate memory */
buf = (char*)malloc(size + 1);
if (NULL == buf) {
strcpy(err->message, "no memory");
err->code = VGSMML_ERR_NO_MEMORY;
/* memory check */
if ('\0' != buf[size - 1]) {
strcpy(err->message, "needed specify the \'\\0\' terminated string to the data argument.");
err->code = VGSMML_ERR_INVALID;
return NULL;
}
memcpy(buf, data, size);
buf[size] = '\0';

/* count line */
for (nLine = 1, i = 0; buf[i]; i++) {
Expand All @@ -180,7 +178,6 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
if (NULL == (pos = (int*)malloc((nLine + 1) * sizeof(int)))) {
strcpy(err->message, "no memory");
err->code = VGSMML_ERR_NO_MEMORY;
free(buf);
return NULL;
}

Expand All @@ -207,6 +204,34 @@ struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size,
/* execute next phase */
result = phase3(buf, pos, err);
free(pos);
return result;
}

/* compiles without memory destruction */
struct VgsBgmData* __stdcall vgsmml_compile_from_memory2(const void* data, size_t size, struct VgsMmlErrorInfo* err)
{
struct VgsBgmData* result;
char* buf;

if (NULL == err) {
return NULL;
}
if (NULL == data || size < 1) {
strcpy(err->message, "invalid data size");
err->code = VGSMML_ERR_INVALID;
return NULL;
}

buf = (char*)malloc(size + 1);
if (NULL == buf) {
strcpy(err->message, "no memory");
err->code = VGSMML_ERR_NO_MEMORY;
return NULL;
}
memcpy(buf, data, size);
buf[size] = '\0';

result = vgsmml_compile_from_memory(buf, size + 1, err);
free(buf);
return result;
}
Expand Down
2 changes: 2 additions & 0 deletions src/vgsmml.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ struct VgsMmlErrorInfo {
#ifdef _WIN32
struct VgsBgmData* __stdcall vgsmml_compile_from_file(const char* path, struct VgsMmlErrorInfo* err);
struct VgsBgmData* __stdcall vgsmml_compile_from_memory(void* data, size_t size, struct VgsMmlErrorInfo* err);
struct VgsBgmData* __stdcall vgsmml_compile_from_memory2(const void* data, size_t size, struct VgsMmlErrorInfo* err);
void __stdcall vgsmml_free_bgm_data(struct VgsBgmData* data);
#else
struct VgsBgmData* vgsmml_compile_from_file(const char* path, struct VgsMmlErrorInfo* err);
struct VgsBgmData* vgsmml_compile_from_memory(void* data, size_t size, struct VgsMmlErrorInfo* err);
struct VgsBgmData* vgsmml_compile_from_memory2(const void* data, size_t size, struct VgsMmlErrorInfo* err);
void vgsmml_free_bgm_data(struct VgsBgmData* data);
#endif

Expand Down
5 changes: 3 additions & 2 deletions test/test_memchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ int main(int argc, char* argv[])
struct VgsMmlErrorInfo err;
char foo[4];
strcpy(foo, "foo");
if (NULL == vgsmml_compile_from_memory(foo, 3, &err) && VGSMML_ERR_SYNTAX_UNKNOWN == err.code && 1 == err.line) {
printf("%s (code=%d, line=%d)\n", err.message, err.code, err.line);
if (NULL == vgsmml_compile_from_memory(foo, 3, &err) && VGSMML_ERR_INVALID == err.code) {
puts(err.message);
puts("test succeed");
return 0;
}
puts("test failed");
Expand Down