/
| 72 | |
| 73 | /**************************************************************************************/ |
| 74 | static char *get_init_string (PJ_CONTEXT *ctx, const char *name) { |
| 75 | /*************************************************************************************** |
| 76 | Read a section of an init file. Return its contents as a plain character string. |
| 77 | It is the duty of the caller to free the memory allocated for the string. |
| 78 | ***************************************************************************************/ |
| 79 | #define MAX_LINE_LENGTH 1000 |
| 80 | size_t current_buffer_size = 5 * (MAX_LINE_LENGTH + 1); |
| 81 | char *fname, *section; |
| 82 | const char *key; |
| 83 | char *buffer = nullptr; |
| 84 | size_t n; |
| 85 | |
| 86 | fname = static_cast<char*>(malloc (MAX_PATH_FILENAME+ID_TAG_MAX+3)); |
| 87 | if (nullptr==fname) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
| 91 | /* Support "init=file:section", "+init=file:section", and "file:section" format */ |
| 92 | key = strstr (name, "init="); |
| 93 | if (nullptr==key) |
| 94 | key = name; |
| 95 | else |
| 96 | key += 5; |
| 97 | if (MAX_PATH_FILENAME + ID_TAG_MAX + 2 < strlen (key)) { |
| 98 | free (fname); |
| 99 | return nullptr; |
| 100 | } |
| 101 | memmove (fname, key, strlen (key) + 1); |
| 102 | |
| 103 | /* Locate the name of the section we search for */ |
| 104 | section = strrchr(fname, ':'); |
| 105 | if (nullptr==section) { |
| 106 | pj_log(ctx, PJ_LOG_ERROR, _("Missing colon in +init")); |
| 107 | proj_context_errno_set (ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE); |
| 108 | free (fname); |
| 109 | return nullptr; |
| 110 | } |
| 111 | *section = 0; |
| 112 | section++; |
| 113 | n = strlen (section); |
| 114 | pj_log (ctx, PJ_LOG_TRACE, |
| 115 | "get_init_string: searching for section [%s] in init file [%s]", |
| 116 | section, fname); |
| 117 | |
| 118 | auto file = NS_PROJ::FileManager::open_resource_file(ctx, fname); |
| 119 | if (nullptr==file) { |
| 120 | pj_log(ctx, PJ_LOG_ERROR, _("Cannot open %s"), fname); |
| 121 | proj_context_errno_set (ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE); |
| 122 | free (fname); |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | /* Search for section in init file */ |
| 127 | std::string line; |
| 128 | for (;;) { |
| 129 | |
| 130 | bool eofReached = false; |
| 131 | bool maxLenReached = false; |