Write content to a file, creating parent directories as needed. */
| 39 | |
| 40 | /* Write content to a file, creating parent directories as needed. */ |
| 41 | static inline int th_write_file(const char *path, const char *content) { |
| 42 | /* Create parent directories */ |
| 43 | char dir[1024]; |
| 44 | snprintf(dir, sizeof(dir), "%s", path); |
| 45 | char *last_slash = strrchr(dir, '/'); |
| 46 | #ifdef _WIN32 |
| 47 | char *last_bslash = strrchr(dir, '\\'); |
| 48 | if (last_bslash && (!last_slash || last_bslash > last_slash)) { |
| 49 | last_slash = last_bslash; |
| 50 | } |
| 51 | #endif |
| 52 | if (last_slash) { |
| 53 | *last_slash = '\0'; |
| 54 | cbm_mkdir_p(dir, 0755); |
| 55 | } |
| 56 | |
| 57 | FILE *f = fopen(path, "w"); |
| 58 | if (!f) { |
| 59 | return -1; |
| 60 | } |
| 61 | if (content && content[0]) { |
| 62 | fputs(content, f); |
| 63 | } |
| 64 | fclose(f); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* Append content to a file. */ |
| 69 | static inline int th_append_file(const char *path, const char *content) { |
no test coverage detected