aboutsummaryrefslogtreecommitdiff
path: root/odb/profile.cxx
blob: 393c953c96783aad79029eb705c32e8c0b782e06 (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
// 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;

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.
  p += ".options";

  struct stat info;
  paths::const_iterator i (ps.begin ()), end (ps.end ());

  for (; i != end; ++i)
  {
    // First check in the search directory itself and then try the odb/
    // subdirectory.
    //
    r = *i / p;

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

    r = *i / odb / p;

    if (stat (r.string ().c_str (), &info) == 0 && S_ISREG (info.st_mode))
      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 ();
}