refactor: simplify parsing

This commit is contained in:
apocelipes
2023-12-04 20:34:41 +09:00
committed by Carter Li
parent 0662af17f4
commit 1c24cb3ebb
4 changed files with 13 additions and 12 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
};
}
else
break; // because we read the whole /proc/stat, we can safely quit when the line does not start with "cpuN"
break;
start = token + 1;
}
+6 -6
View File
@@ -21,22 +21,22 @@ const char* ffDetectMemory(FFMemoryResult* ram)
char *token = NULL;
if((token = strstr(buf, "MemTotal:")) != NULL)
sscanf(token, "MemTotal: %" PRIu64, &memTotal);
memTotal = strtoul(token + strlen("MemTotal:"), NULL, 10);
if((token = strstr(buf, "MemFree:")) != NULL)
sscanf(token, "MemFree: %" PRIu64, &memFree);
memFree = strtoul(token + strlen("MemFree:"), NULL, 10);
if((token = strstr(buf, "Buffers:")) != NULL)
sscanf(token, "Buffers: %" PRIu64, &buffers);
buffers = strtoul(token + strlen("Buffers:"), NULL, 10);
if((token = strstr(buf, "Cached:")) != NULL)
sscanf(token, "Cached: %" PRIu64, &cached);
cached = strtoul(token + strlen("Cached:"), NULL, 10);
if((token = strstr(buf, "Shmem:")) != NULL)
sscanf(token, "Shmem: %" PRIu64, &shmem);
shmem = strtoul(token + strlen("Shmem:"), NULL, 10);
if((token = strstr(buf, "SReclaimable:")) != NULL)
sscanf(token, "SReclaimable: %" PRIu64, &sReclaimable);
sReclaimable = strtoul(token + strlen("SReclaimable:"), NULL, 10);
ram->bytesTotal = memTotal * 1024lu;
ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * 1024lu;
+2 -2
View File
@@ -18,10 +18,10 @@ const char* ffDetectSwap(FFSwapResult* swap)
char *token = NULL;
if ((token = strstr(buf, "SwapTotal:")) != NULL)
sscanf(token, "SwapTotal: %" PRIu64, &swapTotal);
swapTotal = strtoul(token + strlen("SwapTotal:"), NULL, 10);
if ((token = strstr(buf, "SwapFree:")) != NULL)
sscanf(token, "SwapFree: %" PRIu64, &swapFree);
swapFree = strtoul(token + strlen("SwapFree:"), NULL, 10);
swap->bytesTotal = swapTotal * 1024lu;
swap->bytesUsed = (swapTotal - swapFree) * 1024lu;
+4 -3
View File
@@ -13,11 +13,12 @@ const char* ffDetectUptime(FFUptimeResult* result)
return "ffReadFileData(\"/proc/uptime\", sizeof(buf) - 1, buf) failed";
buf[nRead] = '\0';
double sec;
if(sscanf(buf, "%lf", &sec) > 0)
char *err = NULL;
double sec = strtod(buf, &err);
if(err != buf)
result->uptime = (uint64_t) (sec * 1000);
else
return "sscanf(buf.chars, \"%lf\", &sec) failed";
return "strtod(buf, &err) failed";
result->bootTime = ffTimeGetNow() + result->uptime;