| 10667 | } |
| 10668 | |
| 10669 | struct GMT_POSTSCRIPT * gmtlib_read_ps (struct GMT_CTRL *GMT, void *source, unsigned int source_type, unsigned int mode) { |
| 10670 | /* Opens and reads a PostScript file. |
| 10671 | * Return the result as a GMT_POSTSCRIPT struct. |
| 10672 | * source_type can be GMT_IS_[FILE|STREAM|FDESC] |
| 10673 | * mode is not yet used. |
| 10674 | */ |
| 10675 | |
| 10676 | char ps_file[PATH_MAX] = {""}, buffer[GMT_LEN256] = {""}; |
| 10677 | int c; |
| 10678 | bool close_file = false; |
| 10679 | size_t n_alloc = 0; |
| 10680 | struct GMT_POSTSCRIPT *P = NULL; |
| 10681 | struct GMT_POSTSCRIPT_HIDDEN *PH = NULL; |
| 10682 | FILE *fp = NULL; |
| 10683 | gmt_M_unused(mode); |
| 10684 | |
| 10685 | /* Determine input source */ |
| 10686 | |
| 10687 | if (source_type == GMT_IS_FILE) { /* source is a file name */ |
| 10688 | struct stat buf; |
| 10689 | char path[PATH_MAX] = {""}; |
| 10690 | strncpy (ps_file, source, PATH_MAX-1); |
| 10691 | if (!gmt_getdatapath (GMT, ps_file, path, R_OK)) { |
| 10692 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot find PostScript file %s\n", ps_file); |
| 10693 | return (NULL); |
| 10694 | } |
| 10695 | if (stat (path, &buf)) { |
| 10696 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot determine size of PostScript file %s\n", ps_file); |
| 10697 | return (NULL); |
| 10698 | } |
| 10699 | if ((fp = fopen (ps_file, "r")) == NULL) { |
| 10700 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot open PostScript file %s\n", ps_file); |
| 10701 | return (NULL); |
| 10702 | } |
| 10703 | n_alloc = buf.st_size; /* We know what to allocate here */ |
| 10704 | close_file = true; /* We only close files we have opened here */ |
| 10705 | } |
| 10706 | else if (source_type == GMT_IS_STREAM) { /* Open file pointer given, just copy */ |
| 10707 | fp = (FILE *)source; |
| 10708 | if (fp == NULL) fp = GMT->session.std[GMT_IN]; /* Default input */ |
| 10709 | if (fp == GMT->session.std[GMT_IN]) |
| 10710 | strcpy (ps_file, "<stdin>"); |
| 10711 | else |
| 10712 | strcpy (ps_file, "<input stream>"); |
| 10713 | } |
| 10714 | else if (source_type == GMT_IS_FDESC) { /* Open file descriptor given, just convert to file pointer */ |
| 10715 | struct stat buf; |
| 10716 | int *fd = source; |
| 10717 | if (fstat (*fd, &buf)) { |
| 10718 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot determine size of PostScript file give by file descriptor %d\n", *fd); |
| 10719 | return (NULL); |
| 10720 | } |
| 10721 | if ((fp = fdopen (*fd, "r")) == NULL) { |
| 10722 | GMT_Report (GMT->parent, GMT_MSG_ERROR, "Cannot convert PostScript file descriptor %d to stream in gmtlib_read_ps\n", *fd); |
| 10723 | return (NULL); |
| 10724 | } |
| 10725 | else |
| 10726 | close_file = true; /* Since fdopen creates a FILE struct */ |
no test coverage detected