| 7195 | } |
| 7196 | |
| 7197 | GMT_LOCAL int gmtapi_write_vector (struct GMT_CTRL *GMT, void *dest, unsigned int dest_type, unsigned int mode, struct GMT_VECTOR *V) { |
| 7198 | /* We write the VECTOR to fp [or stdout]. |
| 7199 | * dest_type can be GMT_IS_[FILE|STREAM|FDESC] |
| 7200 | * mode is not used yet. |
| 7201 | */ |
| 7202 | |
| 7203 | bool close_file = false, append = false, was; |
| 7204 | uint64_t row, col; |
| 7205 | unsigned int hdr; |
| 7206 | char V_file[PATH_MAX] = {""}; |
| 7207 | static char *msg1[2] = {"Writing", "Appending"}; |
| 7208 | FILE *fp = NULL; |
| 7209 | GMT_getfunction *api_get_val = NULL; |
| 7210 | gmt_M_unused(mode); |
| 7211 | |
| 7212 | if (V == NULL) { |
| 7213 | GMT_Report(GMT->parent, GMT_MSG_ERROR, "GMTAPI: gmtapi_write_vector passed a NULL pointer *V\n"); |
| 7214 | return GMT_NOTSET; |
| 7215 | } |
| 7216 | if (dest_type == GMT_IS_FILE && !dest) dest_type = GMT_IS_STREAM; /* No filename given, default to stdout */ |
| 7217 | |
| 7218 | if (dest_type == GMT_IS_FILE) { /* dest is a file name */ |
| 7219 | static char *msg2[2] = {"create", "append to"}; |
| 7220 | strncpy (V_file, dest, PATH_MAX-1); |
| 7221 | append = (V_file[0] == '>'); /* Want to append to existing file */ |
| 7222 | if ((fp = fopen (&V_file[append], (append) ? "a" : "w")) == NULL) { |
| 7223 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot %s Vector file %s\n", msg2[append], &V_file[append]); |
| 7224 | return (GMT_ERROR_ON_FOPEN); |
| 7225 | } |
| 7226 | close_file = true; /* We only close files we have opened here */ |
| 7227 | } |
| 7228 | else if (dest_type == GMT_IS_STREAM) { /* Open file pointer given, just copy */ |
| 7229 | fp = (FILE *)dest; |
| 7230 | if (fp == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 7231 | if (fp == GMT->session.std[GMT_OUT]) |
| 7232 | strcpy (V_file, "<stdout>"); |
| 7233 | else |
| 7234 | strcpy (V_file, "<output stream>"); |
| 7235 | } |
| 7236 | else if (dest_type == GMT_IS_FDESC) { /* Open file descriptor given, just convert to file pointer */ |
| 7237 | int *fd = dest; |
| 7238 | if (fd && (fp = fdopen (*fd, "a")) == NULL) { |
| 7239 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot convert Vector file descriptor %d to stream in gmtapi_write_vector\n", *fd); |
| 7240 | return (GMT_ERROR_ON_FDOPEN); |
| 7241 | } |
| 7242 | if (fd == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 7243 | if (fp == GMT->session.std[GMT_OUT]) |
| 7244 | strcpy (V_file, "<stdout>"); |
| 7245 | else |
| 7246 | strcpy (V_file, "<output file descriptor>"); |
| 7247 | close_file = true; /* since fdopen allocates space */ |
| 7248 | } |
| 7249 | else { |
| 7250 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized source type %d in gmtapi_write_vector\n", dest_type); |
| 7251 | return (GMT_NOT_A_VALID_METHOD); |
| 7252 | } |
| 7253 | GMT_Report (GMT->parent, GMT_MSG_DEBUG, "%s Vector to %s\n", msg1[append], &V_file[append]); |
| 7254 |
no test coverage detected