| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2015-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #ifdef _WIN32 |
| 22 | #include <direct.h> |
| 23 | #else |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | |
| 27 | namespace opencc { |
| 28 | |
| 29 | inline char* portable_getcwd() { |
| 30 | #ifdef _WIN32 |
| 31 | return _getcwd(nullptr, 0); |
| 32 | #else |
| 33 | return getcwd(nullptr, 0); |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | inline int portable_chdir(const char* path) { |
| 38 | #ifdef _WIN32 |
| 39 | return _chdir(path); |
| 40 | #else |
| 41 | return chdir(path); |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | inline void portable_putenv(const char* key, const char* value) { |
| 46 | #ifdef _WIN32 |
| 47 | _putenv_s(key, value); |
| 48 | #else |
| 49 | setenv(key, value, 1); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | inline void portable_unsetenv(const char* key) { |
| 54 | #ifdef _WIN32 |
| 55 | _putenv_s(key, ""); |
| 56 | #else |
| 57 | unsetenv(key); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | } // namespace opencc |