mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-24 05:31:13 +00:00
Rewrite extractVersionQuadFromString() to avoid copying the input string
This commit is contained in:
+11
-24
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user