@@ -78,19 +78,20 @@ const char *CpuInfo::getNextLine() {
7878 return savedCurrentLine;
7979}
8080
81- Collection::Collection () {
81+ Collection::Collection (CpuInfo &cpuInfo) : cpuInfo(cpuInfo ) {
8282 totalNumberOfSockets = 0 ;
8383 totalNumberOfCpuCores = 0 ;
8484 currentProcessor = NULL ;
8585
8686 processors.reserve (96 );
8787
88- parseCpuFile ( " /proc/cpuinfo " );
88+ parseCpuInfo ( );
8989 collectBasicCpuInformation ();
9090}
9191
9292Collection &Collection::getSingleInstance () {
93- static Collection collection;
93+ static CpuInfo cpuInfo;
94+ static Collection collection (cpuInfo);
9495 return collection;
9596}
9697
@@ -119,35 +120,20 @@ const Processor &Collection::getProcessor(unsigned processorId) {
119120 return collection.processors [processorId];
120121}
121122
122- void Collection::parseCpuFile (const char *fileName) {
123- FILE *file = fopen (fileName, " rb" );
124- if (!file) {
125- return ;
126- }
127-
128- parseCpuFileContent (file);
129-
130- fclose (file);
131- }
132-
133- void Collection::parseCpuFileContent (FILE *file) {
134- while (!feof (file)) {
135- char lineBuffer[1024 ];
136- if (!fgets (lineBuffer, sizeof (lineBuffer), file)) {
137- break ;
138- }
139-
140- parseCpuFileLine (lineBuffer);
123+ void Collection::parseCpuInfo () {
124+ const char *cpuInfoLine = cpuInfo.getFirstLine ();
125+ for (; cpuInfoLine; cpuInfoLine = cpuInfo.getNextLine ()) {
126+ parseCpuInfoLine (cpuInfoLine);
141127 }
142128}
143129
144- void Collection::parseCpuFileLine (const char *lineBuffer ) {
145- int delimiterPosition = strcspn (lineBuffer , " :" );
130+ void Collection::parseCpuInfoLine (const char *cpuInfoLine ) {
131+ int delimiterPosition = strcspn (cpuInfoLine , " :" );
146132
147- if (lineBuffer [delimiterPosition] == ' \0 ' ) {
133+ if (cpuInfoLine [delimiterPosition] == ' \0 ' ) {
148134 currentProcessor = NULL ;
149135 } else {
150- parseValue (lineBuffer , &lineBuffer [delimiterPosition + 2 ]);
136+ parseValue (cpuInfoLine , &cpuInfoLine [delimiterPosition + 2 ]);
151137 }
152138}
153139
0 commit comments