! . */
| 9569 | |
| 9570 | /*! . */ |
| 9571 | int gmtlib_write_cpt (struct GMT_CTRL *GMT, void *dest, unsigned int dest_type, unsigned int cpt_flags, struct GMT_PALETTE *P) { |
| 9572 | /* We write the CPT to fp [or stdout]. |
| 9573 | * dest_type can be GMT_IS_[FILE|STREAM|FDESC] |
| 9574 | * cpt_flags is a combination of: |
| 9575 | * GMT_CPT_NO_BNF = Do not write BFN |
| 9576 | * GMT_CPT_EXTEND_BNF = Make B and F equal to low and high color |
| 9577 | */ |
| 9578 | |
| 9579 | unsigned int i, append = 0, hinge_mode = 0; |
| 9580 | bool close_file = false, use[3] = {true, true, true}; |
| 9581 | double cmyk[5], z_hinge; |
| 9582 | char format[GMT_BUFSIZ] = {""}, cpt_file[PATH_MAX] = {""}, code[3] = {'B', 'F', 'N'}; |
| 9583 | char lo[GMT_LEN64] = {""}, hi[GMT_LEN64] = {""}, kind[3] = {'L', 'U', 'B'}; |
| 9584 | static char *msg1[2] = {"Writing", "Appending"}; |
| 9585 | FILE *fp = NULL; |
| 9586 | struct CPT_Z_SCALE *Z = NULL; /* For unit manipulations */ |
| 9587 | struct GMT_PALETTE_HIDDEN *PH = NULL; |
| 9588 | |
| 9589 | if (cpt_flags & GMT_CPT_GRAY_SET && !P->is_gray) |
| 9590 | GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Colors in the CPT file %s were converted to gray via the YIQ-translation.\n", &cpt_file[append]); |
| 9591 | |
| 9592 | /* When writing the CPT to file it is no longer a normalized CPT with a hinge */ |
| 9593 | P->has_range = P->has_hinge = 0; |
| 9594 | |
| 9595 | if (dest_type == GMT_IS_FILE && !dest) dest_type = GMT_IS_STREAM; /* No filename given, default to stdout */ |
| 9596 | |
| 9597 | if (dest_type == GMT_IS_FILE) { /* dest is a file name */ |
| 9598 | static char *msg2[2] = {"create", "append to"}; |
| 9599 | strncpy (cpt_file, dest, PATH_MAX-1); |
| 9600 | append = (cpt_file[0] == '>'); /* Want to append to existing file */ |
| 9601 | if ((Z = gmtsupport_cpt_parse (GMT, &cpt_file[append], GMT_OUT, &hinge_mode, &z_hinge))) { |
| 9602 | gmtsupport_cpt_z_scale (GMT, P, Z, GMT_OUT); |
| 9603 | gmt_M_free (GMT, Z); |
| 9604 | } |
| 9605 | if ((fp = fopen (&cpt_file[append], (append) ? "a" : "w")) == NULL) { |
| 9606 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot %s file %s\n", msg2[append], &cpt_file[append]); |
| 9607 | return (GMT_ERROR_ON_FOPEN); |
| 9608 | } |
| 9609 | close_file = true; /* We only close files we have opened here */ |
| 9610 | } |
| 9611 | else if (dest_type == GMT_IS_STREAM) { /* Open file pointer given, just copy */ |
| 9612 | fp = (FILE *)dest; |
| 9613 | if (fp == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 9614 | if (fp == GMT->session.std[GMT_OUT]) |
| 9615 | strcpy (cpt_file, "<stdout>"); |
| 9616 | else |
| 9617 | strcpy (cpt_file, "<output stream>"); |
| 9618 | } |
| 9619 | else if (dest_type == GMT_IS_FDESC) { /* Open file descriptor given, just convert to file pointer */ |
| 9620 | int *fd = dest; |
| 9621 | if (fd && (fp = fdopen (*fd, "a")) == NULL) { |
| 9622 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot convert file descriptor %d to stream in gmtlib_write_cpt\n", *fd); |
| 9623 | return (GMT_ERROR_ON_FDOPEN); |
| 9624 | } |
| 9625 | else |
| 9626 | close_file = true; /* fdopen allocates memory */ |
| 9627 | if (fd == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 9628 | if (fp == GMT->session.std[GMT_OUT]) |
no test coverage detected