labels.cpp 880 Bytes
Newer Older
Mikael Salson's avatar
Mikael Salson committed
1 2

#include "labels.h"
Mikael Salson's avatar
Mikael Salson committed
3 4 5
#include <cmath>
#include <cstdlib>
#include "tools.h"
Mikael Salson's avatar
Mikael Salson committed
6

Mathieu Giraud's avatar
Mathieu Giraud committed
7

8
void load_into_map(map <string, string> &the_map, string map_file, string default_value)
Mikael Salson's avatar
Mikael Salson committed
9 10 11 12
{
  // Loads a simple file with key, values into a map
  
  if (!map_file.size())
13
    return ;
Mikael Salson's avatar
Mikael Salson committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  cout << "  <== " << map_file ;

  ifstream f(map_file.c_str());
      
  if (!f.is_open())
    {
      cout << "  [failed] " << endl ;
    }

  int nb_keys = 0 ;

  while (f.good())
    {
      string line ;
      getline (f, line);

      int i = line.find(" ");
      if (i != (int) string::npos)
	{
	  string key = line.substr(0, i);
	  string value = line.substr(i+1, string::npos);
	  
37 38 39 40 41
	  nb_keys++ ;
          if (!value.length())
            value = default_value;

	  the_map[key] = value + (the_map[key].length() ? " " : "") + the_map[key];
Mikael Salson's avatar
Mikael Salson committed
42 43 44 45 46
	}
    }

  cout << ": " << nb_keys << " elements" << endl ;
}
Mikael Salson's avatar
Mikael Salson committed
47 48