* oacc-init.c (acc_device_lock): Add explanatory comment.
[official-gcc.git] / libcc1 / findcomp.cc
blobf02b1dfe425448d5232b22492143621ff5a29ec3
1 /* Find the correct compiler.
2 Copyright (C) 2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <string>
22 #include <dirent.h>
23 #include <stdlib.h>
25 #include "libiberty.h"
26 #include "xregex.h"
27 #include "findcomp.hh"
29 class scanner
31 public:
33 scanner (const std::string &dir)
35 m_dir = opendir (dir.c_str ());
38 ~scanner ()
40 if (m_dir != NULL)
41 closedir (m_dir);
44 const char *next ()
46 if (m_dir == NULL)
47 return NULL;
49 struct dirent *entry = readdir (m_dir);
50 if (entry == NULL)
51 return NULL;
53 return entry->d_name;
56 private:
58 DIR *m_dir;
61 static bool
62 search_dir (const regex_t &regexp, const std::string &dir, std::string *result)
64 scanner scan (dir);
65 const char *filename;
67 while ((filename = scan.next ()) != NULL)
69 if (regexec (&regexp, filename, 0, NULL, 0) == 0)
71 *result = filename;
72 return true;
76 return false;
79 class tokenizer
81 public:
83 tokenizer (const char *str)
84 : m_str (str),
85 m_pos (0)
89 bool done () const
91 return m_pos == std::string::npos;
94 std::string next ()
96 std::string::size_type last_pos = m_pos;
97 std::string::size_type colon = m_str.find(':', last_pos);
99 std::string result;
100 if (colon == std::string::npos)
102 m_pos = colon;
103 result = m_str.substr(last_pos, colon);
105 else
107 m_pos = colon + 1;
108 result = m_str.substr(last_pos, colon - last_pos);
110 if (result == "")
111 result = ".";
113 return result;
116 private:
118 std::string m_str;
119 std::string::size_type m_pos;
122 bool
123 find_compiler (const regex_t &regexp, std::string *result)
125 const char *cpath = getenv ("PATH");
127 if (cpath == NULL)
128 return false;
130 tokenizer dirs (cpath);
131 while (!dirs.done ())
133 std::string dir = dirs.next ();
134 if (search_dir (regexp, dir, result))
135 return true;
138 return false;