kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
| 28 | const std::string GetProjectRootPath() { |
| 29 | char path_buffer[FILENAME_MAX]; |
| 30 | if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) { |
| 31 | fprintf(stderr, "Cannot get current directory!\n"); |
| 32 | return kCannotFindProjectRootDir; |
| 33 | } |
| 34 | |
| 35 | // Check for our file that verifies the root dir. |
| 36 | std::string current_path(path_buffer); |
| 37 | FILE* file = NULL; |
| 38 | int path_delimiter_index = current_path.find_last_of(PATH_DELIMITER); |
| 39 | while (path_delimiter_index > -1) { |
| 40 | std::string root_filename = current_path + PATH_DELIMITER + |
| 41 | kProjectRootFileName; |
| 42 | file = fopen(root_filename.c_str(), "r"); |
| 43 | if (file != NULL) { |
| 44 | return current_path + PATH_DELIMITER; |
| 45 | } |
| 46 | |
| 47 | // Move up one directory in the directory tree. |
| 48 | current_path = current_path.substr(0, path_delimiter_index); |
| 49 | path_delimiter_index = current_path.find_last_of(PATH_DELIMITER); |
| 50 | } |
| 51 | |
| 52 | // Reached the root directory. |
| 53 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 54 | return kCannotFindProjectRootDir; |
| 55 | } |
| 56 | |
| 57 | } // namespace webrtc |
| 58 | } // namespace test |