libragephoto: impl. support for NULL/nullptr to clear data values
This commit is contained in:
parent
32b8258af1
commit
4518ae5aa3
2 changed files with 112 additions and 95 deletions
|
|
@ -124,22 +124,21 @@ static inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, siz
|
|||
|
||||
static inline bool writeDataChar(const char *input, char **output)
|
||||
{
|
||||
if (input) {
|
||||
const size_t src_s = strlen(input) + 1;
|
||||
if (*output) {
|
||||
const size_t dst_s = strlen(*output) + 1;
|
||||
if (dst_s > src_s) {
|
||||
char *t_output = (char*)realloc(*output, src_s);
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
}
|
||||
else if (dst_s < src_s) {
|
||||
char *t_output = (char*)malloc(src_s);
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
free(*output);
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
|
|
@ -150,12 +149,16 @@ static inline bool writeDataChar(const char *input, char **output)
|
|||
}
|
||||
else {
|
||||
char *t_output = (char*)malloc(src_s);
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
}
|
||||
}
|
||||
else if (*output) {
|
||||
free(*output);
|
||||
*output = NULL;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -1024,7 +1027,7 @@ bool ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t ph
|
|||
const size_t writeSize = fwrite(data, sizeof(char), fileSize, file);
|
||||
fclose(file);
|
||||
free(data);
|
||||
return (fileSize == writeSize);
|
||||
return fileSize == writeSize;
|
||||
}
|
||||
|
||||
bool ragephoto_savefile(ragephoto_t instance, const char *filename)
|
||||
|
|
@ -1192,6 +1195,7 @@ void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat)
|
|||
|
||||
bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize)
|
||||
{
|
||||
if (data && size) {
|
||||
if (instance->data->jpeg) {
|
||||
if (instance->data->jpegSize > size) {
|
||||
char *t_photoData = (char*)realloc(instance->data->jpeg, size);
|
||||
|
|
@ -1226,6 +1230,11 @@ bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t siz
|
|||
memcpy(instance->data->jpeg, data, size);
|
||||
instance->data->jpegSize = size;
|
||||
}
|
||||
}
|
||||
else if (instance->data->jpeg) {
|
||||
free(instance->data->jpeg);
|
||||
instance->data->jpeg = NULL;
|
||||
}
|
||||
|
||||
if (bufferSize != 0) {
|
||||
instance->data->jpegBuffer = bufferSize;
|
||||
|
|
|
|||
|
|
@ -118,22 +118,21 @@ inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, size_t inp
|
|||
|
||||
inline bool writeDataChar(const char *input, char **output)
|
||||
{
|
||||
if (input) {
|
||||
const size_t src_s = strlen(input) + 1;
|
||||
if (*output) {
|
||||
const size_t dst_s = strlen(*output) + 1;
|
||||
if (dst_s > src_s) {
|
||||
char *t_output = static_cast<char*>(realloc(*output, src_s));
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
}
|
||||
else if (dst_s < src_s) {
|
||||
char *t_output = static_cast<char*>(malloc(src_s));
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
free(*output);
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
|
|
@ -144,12 +143,16 @@ inline bool writeDataChar(const char *input, char **output)
|
|||
}
|
||||
else {
|
||||
char *t_output = static_cast<char*>(malloc(src_s));
|
||||
if (!t_output) {
|
||||
if (!t_output)
|
||||
return false;
|
||||
}
|
||||
*output = t_output;
|
||||
memcpy(*output, input, src_s);
|
||||
}
|
||||
}
|
||||
else if (*output) {
|
||||
free(*output);
|
||||
*output = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -1018,7 +1021,6 @@ bool RagePhoto::saveFile(const char *filename, uint32_t photoFormat)
|
|||
ofs.close();
|
||||
return ok;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -1189,6 +1191,7 @@ void RagePhoto::setHeader(const char *header, uint32_t headerSum, uint32_t heade
|
|||
|
||||
bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
|
||||
{
|
||||
if (data && size) {
|
||||
if (m_data->jpeg) {
|
||||
if (m_data->jpegSize > size) {
|
||||
char *t_photoData = static_cast<char*>(realloc(m_data->jpeg, size));
|
||||
|
|
@ -1223,6 +1226,11 @@ bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize)
|
|||
memcpy(m_data->jpeg, data, size);
|
||||
m_data->jpegSize = size;
|
||||
}
|
||||
}
|
||||
else if (m_data->jpeg) {
|
||||
free(m_data->jpeg);
|
||||
m_data->jpeg = nullptr;
|
||||
}
|
||||
|
||||
if (bufferSize != 0) {
|
||||
m_data->jpegBuffer = bufferSize;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue