MCPcopy Create free account
hub / github.com/GenericMappingTools/gmt / gmtapi_write_matrix

Function gmtapi_write_matrix

src/gmt_api.c:7041–7138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7039}
7040
7041GMT_LOCAL int gmtapi_write_matrix (struct GMT_CTRL *GMT, void *dest, unsigned int dest_type, unsigned int mode, struct GMT_MATRIX *M) {
7042 /* We write the MATRIX to fp [or stdout].
7043 * dest_type can be GMT_IS_[FILE|STREAM|FDESC]
7044 * mode is not used yet.
7045 */
7046
7047 bool close_file = false, append = false, was;
7048 uint64_t row, col, ij;
7049 unsigned int hdr;
7050 char M_file[PATH_MAX] = {""};
7051 static char *msg1[2] = {"Writing", "Appending"};
7052 FILE *fp = NULL;
7053 p_func_uint64_t GMT_2D_to_index = NULL;
7054 GMT_getfunction api_get_val = NULL;
7055 gmt_M_unused(mode);
7056
7057 if (dest_type == GMT_IS_FILE && !dest) dest_type = GMT_IS_STREAM; /* No filename given, default to stdout */
7058
7059 if (dest_type == GMT_IS_FILE) { /* dest is a file name */
7060 static char *msg2[2] = {"create", "append to"};
7061 strncpy (M_file, dest, PATH_MAX-1);
7062 append = (M_file[0] == '>'); /* Want to append to existing file */
7063 if ((fp = fopen (&M_file[append], (append) ? "a" : "w")) == NULL) {
7064 GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot %s Matrix file %s\n", msg2[append], &M_file[append]);
7065 return (GMT_ERROR_ON_FOPEN);
7066 }
7067 close_file = true; /* We only close files we have opened here */
7068 }
7069 else if (dest_type == GMT_IS_STREAM) { /* Open file pointer given, just copy */
7070 fp = (FILE *)dest;
7071 if (fp == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */
7072 if (fp == GMT->session.std[GMT_OUT])
7073 strcpy (M_file, "<stdout>");
7074 else
7075 strcpy (M_file, "<output stream>");
7076 }
7077 else if (dest_type == GMT_IS_FDESC) { /* Open file descriptor given, just convert to file pointer */
7078 int *fd = dest;
7079 if (fd && (fp = fdopen (*fd, "w")) == NULL) {
7080 GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot convert Matrix file descriptor %d to stream in gmtapi_write_matrix\n", *fd);
7081 return (GMT_ERROR_ON_FDOPEN);
7082 }
7083 if (fd == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */
7084 if (fp == GMT->session.std[GMT_OUT])
7085 strcpy (M_file, "<stdout>");
7086 else
7087 strcpy (M_file, "<output file descriptor>");
7088 close_file = true; /* since fdopen allocates space */
7089 }
7090 else {
7091 GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized source type %d in gmtapi_write_matrix\n", dest_type);
7092 return (GMT_NOT_A_VALID_METHOD);
7093 }
7094 GMT_Report (GMT->parent, GMT_MSG_DEBUG, "%s Matrix to %s\n", msg1[append], &M_file[append]);
7095
7096 /* Set index and put-value functions */
7097 if ((GMT_2D_to_index = gmtapi_get_2d_to_index (GMT->parent, M->shape, GMT_GRID_IS_REAL)) == NULL) {
7098 if (close_file) fclose (fp);

Callers 1

gmtapi_export_matrixFunction · 0.85

Calls 6

GMT_ReportFunction · 0.85
gmtapi_get_2d_to_indexFunction · 0.85
gmtlib_write_tableheaderFunction · 0.85
gmtapi_bin_input_memoryFunction · 0.85
gmt_write_segmentheaderFunction · 0.85

Tested by

no test coverage detected