| 10768 | } |
| 10769 | |
| 10770 | int gmtlib_write_ps (struct GMT_CTRL *GMT, void *dest, unsigned int dest_type, unsigned int mode, struct GMT_POSTSCRIPT *P) { |
| 10771 | /* We write the PostScript file to fp [or stdout]. |
| 10772 | * dest_type can be GMT_IS_[FILE|STREAM|FDESC] |
| 10773 | * mode is not used yet. |
| 10774 | */ |
| 10775 | |
| 10776 | bool close_file = false, append = false; |
| 10777 | char ps_file[PATH_MAX] = {""}; |
| 10778 | static char *msg1[2] = {"Writing", "Appending"}; |
| 10779 | FILE *fp = NULL; |
| 10780 | gmt_M_unused(mode); |
| 10781 | |
| 10782 | if (dest_type == GMT_IS_FILE && !dest) dest_type = GMT_IS_STREAM; /* No filename given, default to stdout */ |
| 10783 | |
| 10784 | if (dest_type == GMT_IS_FILE) { /* dest is a file name */ |
| 10785 | static char *msg2[2] = {"create", "append to"}; |
| 10786 | strncpy (ps_file, dest, PATH_MAX-1); |
| 10787 | append = (ps_file[0] == '>'); /* Want to append to existing file */ |
| 10788 | if ((fp = fopen (&ps_file[append], (append) ? "a" : "w")) == NULL) { |
| 10789 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot %s PostScript file %s\n", msg2[append], &ps_file[append]); |
| 10790 | return (GMT_ERROR_ON_FOPEN); |
| 10791 | } |
| 10792 | close_file = true; /* We only close files we have opened here */ |
| 10793 | } |
| 10794 | else if (dest_type == GMT_IS_STREAM) { /* Open file pointer given, just copy */ |
| 10795 | fp = (FILE *)dest; |
| 10796 | if (fp == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 10797 | if (fp == GMT->session.std[GMT_OUT]) |
| 10798 | strcpy (ps_file, "<stdout>"); |
| 10799 | else |
| 10800 | strcpy (ps_file, "<output stream>"); |
| 10801 | } |
| 10802 | else if (dest_type == GMT_IS_FDESC) { /* Open file descriptor given, just convert to file pointer */ |
| 10803 | int *fd = dest; |
| 10804 | if (fd && (fp = fdopen (*fd, "a")) == NULL) { |
| 10805 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot convert PostScript file descriptor %d to stream in gmtlib_write_ps\n", *fd); |
| 10806 | return (GMT_ERROR_ON_FDOPEN); |
| 10807 | } |
| 10808 | if (fd == NULL) fp = GMT->session.std[GMT_OUT]; /* Default destination */ |
| 10809 | if (fp == GMT->session.std[GMT_OUT]) |
| 10810 | strcpy (ps_file, "<stdout>"); |
| 10811 | else |
| 10812 | strcpy (ps_file, "<output file descriptor>"); |
| 10813 | close_file = true; /* since fdopen allocates space */ |
| 10814 | } |
| 10815 | else { |
| 10816 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized source type %d in gmtlib_write_ps\n", dest_type); |
| 10817 | return (GMT_NOT_A_VALID_METHOD); |
| 10818 | } |
| 10819 | GMT_Report (GMT->parent, GMT_MSG_DEBUG, "%s PostScript to %s\n", msg1[append], &ps_file[append]); |
| 10820 | |
| 10821 | /* Start writing PostScript to fp */ |
| 10822 | |
| 10823 | if (fwrite (P->data, 1U, P->n_bytes, fp) != P->n_bytes) { |
| 10824 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Failure while %s PostScript to %s\n", msg1[append], &ps_file[append]); |
| 10825 | if (close_file) fclose (fp); |
| 10826 | return (GMT_DATA_WRITE_ERROR); |
| 10827 | } |
no test coverage detected