#include <iostream>
#include <fstream>
#include <map>
#include <stdio.h>
#include <string>

using namespace std;

// set up a std::map for holding the memory info
struct ltstr {
	bool operator()(const std::string a, const std::string b ) const
		{
			return ( a < b );
		}
};

typedef std::map < std::string, int, ltstr > MemoryMap;
MemoryMap oMemoryMap;

int main ( )
{
	// open up /proc/meminfo so we can read the data
	ifstream meminfo ( "/proc/meminfo" );
	char NameBuffer [ 256 ];
	char ValueBuffer [ 256 ];
	char DiscardBuffer [ 256 ];

	// throw out the first three lines.  The info is 
	//   redundant, and not available in 2.5
	meminfo.getline ( DiscardBuffer, 256 );
	meminfo.getline ( DiscardBuffer, 256 );
	meminfo.getline ( DiscardBuffer, 256 );


	// now we get to the good stuff
	while ( ! meminfo.eof ( ) ) {
		
		
		meminfo.get ( NameBuffer, 256, ':' );
		meminfo.get ( DiscardBuffer, 2 ); // lose the :
		meminfo.get ( ValueBuffer, 256, '\n' );
		meminfo.get ( DiscardBuffer, 2, ' ' ); // lose the \n -- specify ' ' or it will stop before the \n and not read it

		if ( NameBuffer [ 0 ] == 0 ) {
			break;
		} else {
			oMemoryMap [ NameBuffer ] = atoi ( ValueBuffer) ;
		}
		
	}

	// go back and print out what we read in to prove we got it
	for ( MemoryMap::iterator i = oMemoryMap.begin ( );
		  i != oMemoryMap.end ( );
		  i++ ) {

		printf ( "%-20s %10d\n", (*i).first.c_str ( ), (*i).second );

	}

}
