Rewrite extractVersionQuadFromString() to avoid copying the input string
This commit is contained in:
parent
8b84d17c8d
commit
b2497a3918
1 changed files with 12 additions and 25 deletions
37
src/Misc.c
37
src/Misc.c
|
|
@ -80,32 +80,19 @@ int gracefullyDisconnectEnetPeer(ENetHost* host, ENetPeer* peer, enet_uint32 lin
|
||||||
}
|
}
|
||||||
|
|
||||||
int extractVersionQuadFromString(const char* string, int* quad) {
|
int extractVersionQuadFromString(const char* string, int* quad) {
|
||||||
char versionString[128];
|
const char* nextNumber = string;
|
||||||
char* nextDot;
|
for (int i = 0; i < 4; i++) {
|
||||||
char* nextNumber;
|
// Parse the next component
|
||||||
int i;
|
quad[i] = (int)strtol(nextNumber, (char**)&nextNumber, 10);
|
||||||
|
|
||||||
strcpy(versionString, string);
|
// Skip the dot if we still have version components left.
|
||||||
nextNumber = versionString;
|
//
|
||||||
|
// We continue looping even when we're at the end of the
|
||||||
for (i = 0; i < 4; i++) {
|
// input string to ensure all subsequent version components
|
||||||
if (i == 3) {
|
// are zeroed.
|
||||||
nextDot = strchr(nextNumber, '\0');
|
if (*nextNumber != 0) {
|
||||||
|
nextNumber++;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
nextDot = strchr(nextNumber, '.');
|
|
||||||
}
|
|
||||||
if (nextDot == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cut the string off at the next dot
|
|
||||||
*nextDot = '\0';
|
|
||||||
|
|
||||||
quad[i] = atoi(nextNumber);
|
|
||||||
|
|
||||||
// Move on to the next segment
|
|
||||||
nextNumber = nextDot + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue