blob: 098dad7ed9eedccab39d402de3253fea91b6e749 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "fileutils.h"
12
13#ifdef WIN32
14#include <direct.h>
15#define GET_CURRENT_DIR _getcwd
16#define PATH_DELIMITER "\\"
17#else
18#include <unistd.h>
19#define GET_CURRENT_DIR getcwd
20#define PATH_DELIMITER "/"
21#endif
22
23#include <cstdio>
24
25namespace webrtc {
26namespace test {
27
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000028// The file we're looking for to identify the project root dir.
29static const char* kProjectRootFileName = "DEPS";
30const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
31
32std::string GetProjectRootPath() {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000033 char path_buffer[FILENAME_MAX];
34 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
35 fprintf(stderr, "Cannot get current directory!\n");
36 return kCannotFindProjectRootDir;
37 }
38
39 // Check for our file that verifies the root dir.
40 std::string current_path(path_buffer);
41 FILE* file = NULL;
42 int path_delimiter_index = current_path.find_last_of(PATH_DELIMITER);
43 while (path_delimiter_index > -1) {
44 std::string root_filename = current_path + PATH_DELIMITER +
45 kProjectRootFileName;
46 file = fopen(root_filename.c_str(), "r");
47 if (file != NULL) {
48 return current_path + PATH_DELIMITER;
49 }
50
51 // Move up one directory in the directory tree.
52 current_path = current_path.substr(0, path_delimiter_index);
53 path_delimiter_index = current_path.find_last_of(PATH_DELIMITER);
54 }
55
56 // Reached the root directory.
57 fprintf(stderr, "Cannot find project root directory!\n");
58 return kCannotFindProjectRootDir;
59}
60
61} // namespace webrtc
62} // namespace test