aboutsummaryrefslogtreecommitdiff
path: root/odb/profile.cxx
blob: fae249067e219abab4c0292f69064228c37d9007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// file      : odb/profile.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <unistd.h>    // stat
#include <sys/types.h> // stat
#include <sys/stat.h>  // stat

#include <iostream>

#include <odb/profile.hxx>

using namespace std;



static bool
exist (profile_data::path const& p)
{
  struct stat info;

  // Just check that the file exist without checking for permissions, etc.
  //
  return stat (p.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode);
}

string
profile_search (char const* prof, void* arg)
{
  typedef profile_data::path path;
  typedef profile_data::paths paths;

  profile_data* pd (static_cast<profile_data*> (arg));
  paths const& ps (pd->search_paths);

  path p (prof), odb ("odb"), r;
  p.normalize (); // Convert '/' to the canonical path separator form.
  path p_db (p);
  p_db += "-";
  p_db += pd->db.string ();
  p += ".options";
  p_db += ".options";

  paths::const_iterator i (ps.begin ()), end (ps.end ());
  for (; i != end; ++i)
  {
    // First check for the database-specific version in the search directory
    // itself and then try the odb/ subdirectory.
    //
    if (exist (r = *i / p_db))
      break;

    if (exist (r = *i / odb / p_db))
      break;

    // Then try the same with the database-independent version.
    //
    if (exist (r = *i / p))
      break;

    if (exist (r = *i / odb / p))
      break;
  }

  if (i == end)
  {
    cerr << pd->name << ": error: unable to locate options file for profile '"
         << prof << "'" << endl;
    throw profile_failure ();
  }

  if (pd->loaded.find (r) != pd->loaded.end ())
    return string ();

  pd->loaded.insert (r);
  return r.string ();
}

string
profile_search_ignore (char const*, void*)
{
  return string ();
}