|
1 |
| -// bypass C++ functions that are not CRAN-compliant |
2 |
| -#ifndef SYMBOL_INTERCEPTORS_H |
3 |
| -#define SYMBOL_INTERCEPTORS_H |
| 1 | +// Include R's C API for Windows to intercept problematic symbols |
| 2 | +#ifdef _WIN32 |
| 3 | +// Include R headers first |
| 4 | +#include <R.h> |
| 5 | +#include <Rinternals.h> |
4 | 6 |
|
5 |
| -// Only apply these redirections when building for Windows |
6 |
| -#if defined(_WIN32) |
| 7 | +// Use a namespace to avoid conflicts |
| 8 | +namespace tesseract_r_wrapper { |
| 9 | +// Safe versions that don't terminate R |
| 10 | +inline void safe_abort() { |
| 11 | + REprintf("Internal error detected in tesseract (abort call intercepted)\n"); |
| 12 | +} |
| 13 | + |
| 14 | +inline void safe_exit(int status) { |
| 15 | + REprintf("Exit requested with status %d (intercepted)\n", status); |
| 16 | +} |
7 | 17 |
|
| 18 | +inline int safe_rand() { return 0; } |
| 19 | +inline void safe_srand(unsigned int seed) {} |
| 20 | +} // namespace tesseract_r_wrapper |
| 21 | + |
| 22 | +// Only include the C++ header interceptors after defining our safe functions |
8 | 23 | #include <cstdlib>
|
9 | 24 | #include <iostream>
|
10 | 25 |
|
| 26 | +// Override problematic functions after including standard headers |
| 27 | +#ifdef abort |
| 28 | +#undef abort |
| 29 | +#endif |
| 30 | +#define abort tesseract_r_wrapper::safe_abort |
| 31 | + |
| 32 | +#ifdef exit |
| 33 | +#undef exit |
| 34 | +#endif |
| 35 | +#define exit(x) tesseract_r_wrapper::safe_exit(x) |
| 36 | + |
| 37 | +#ifdef rand |
| 38 | +#undef rand |
| 39 | +#endif |
| 40 | +#define rand tesseract_r_wrapper::safe_rand |
| 41 | + |
| 42 | +#ifdef srand |
| 43 | +#undef srand |
| 44 | +#endif |
| 45 | +#define srand(x) tesseract_r_wrapper::safe_srand(x) |
| 46 | + |
| 47 | +// Redirect cout/cerr |
11 | 48 | #define cerr \
|
12 | 49 | if (0) std::cerr
|
13 | 50 | #define cout \
|
14 | 51 | if (0) std::cout
|
15 | 52 |
|
16 |
| -inline void R_friendly_abort() { |
17 |
| - Rprintf("Internal error detected (abort intercepted)\n"); |
18 |
| -} |
19 |
| -inline void R_friendly_exit(int status) { |
20 |
| - Rprintf("Exit requested with status %d (intercepted)\n", status); |
21 |
| -} |
22 |
| - |
23 |
| -#define abort R_friendly_abort |
24 |
| -#define exit(x) R_friendly_exit(x) |
25 |
| - |
26 |
| -inline int R_friendly_rand() { return 0; } |
27 |
| -inline void R_friendly_srand(unsigned int seed) {} |
28 |
| -#define rand R_friendly_rand |
29 |
| -#define srand(x) R_friendly_srand(x) |
| 53 | +#endif // _WIN32 |
30 | 54 |
|
31 |
| -#endif |
32 |
| -#endif |
33 |
| - |
34 |
| -// Try multiple include paths for better cross-platform compatibility |
35 |
| -#if __APPLE__ |
36 | 55 | // On macOS, try multiple include paths
|
| 56 | +#if __APPLE__ |
37 | 57 | #if __has_include(<leptonica/allheaders.h>)
|
38 | 58 | #include <leptonica/allheaders.h>
|
39 | 59 | #elif __has_include(<allheaders.h>)
|
|
0 commit comments