Created
October 27, 2017 01:08
-
-
Save pileon/40305e3fd8277e9d396bb442de63ea26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object* read(char* str) | |
{ | |
Object* object = (Object*)malloc(sizeof(*object)); | |
object->identity[0] = 0; | |
int capacity = (100 + 1) - (10); | |
// No allocation here object->name = (char*)malloc(capacity * sizeof(*object->name)); | |
object->value = 0.0; | |
int length = strlen(str); | |
if (length > capacity) | |
{ | |
// No reallocation here object->name = (char*)realloc(object->name, (capacity * 2) * sizeof(*object->name)); | |
// Increase size (TODO: Should make sure that it's actually larger than length) | |
capacity *= 2; | |
} | |
// One single allocation here | |
object->name = (char*)malloc(capacity * sizeof(*object->name)); | |
int arguments = sscanf(str, "%" STRING_SPACE "s %lf %[^\n]s", | |
object->identity, | |
&object->value, | |
object->name); | |
if (arguments == MATCHER) { | |
return object; | |
} else { | |
return NULL; | |
} | |
return object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment