1 /*
2 *  fitsiowrap.cpp
3 *  PHD Guiding
4 *
5 *  Created by Andy Galasso
6 *  Copyright (c) 2014 Andy Galasso
7 *  All rights reserved.
8 *
9 *  This source code is distributed under the following "BSD" license
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions are met:
12 *    Redistributions of source code must retain the above copyright notice,
13 *     this list of conditions and the following disclaimer.
14 *    Redistributions in binary form must reproduce the above copyright notice,
15 *     this list of conditions and the following disclaimer in the
16 *     documentation and/or other materials provided with the distribution.
17 *    Neither the name of Craig Stark, Stark Labs,
18 *     Bret McKee, Dad Dog Development, Ltd, nor the names of its
19 *     contributors may be used to endorse or promote products derived from
20 *     this software without specific prior written permission.
21 *
22 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 *  POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 
36 #include "phd.h"
37 
38 class FitsFname
39 {
40 #ifdef __WINDOWS__
41     char *m_str;
42 #else
43     wxCharBuffer m_str;
44 #endif
45 
46 public:
47     FitsFname(const wxString& str, bool create, bool clobber);
48 
~FitsFname()49     ~FitsFname() {
50 #ifdef __WINDOWS__
51         delete[] m_str;
52 #endif
53     }
54 
operator const char*()55     operator const char *() { return m_str; }
56 };
57 
FitsFname(const wxString & path,bool create,bool clobber)58 FitsFname::FitsFname(const wxString& path, bool create, bool clobber)
59 {
60 #ifdef __WINDOWS__
61 
62     if (create)
63     {
64         if (!clobber && wxFileExists(path))
65         {
66             m_str = new char[1];
67             *m_str = 0;
68             return;
69         }
70 
71         int fd = wxOpen(path, O_BINARY | O_WRONLY | O_CREAT, wxS_DEFAULT);
72         wxClose(fd);
73     }
74 
75     // use the short DOS 8.3 path name to avoid problems converting UTF-16 filenames to the ANSI filenames expected by CFITTSIO
76 
77     DWORD shortlen = GetShortPathNameW(path.wc_str(), 0, 0);
78 
79     if (shortlen)
80     {
81         LPWSTR shortpath = new WCHAR[shortlen];
82         GetShortPathNameW(path.wc_str(), shortpath, shortlen);
83         int slen = WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortpath, shortlen, 0, 0, 0, 0);
84         m_str = new char[slen + 1];
85         char *str = m_str;
86         if (create)
87             *str++ = '!';
88         WideCharToMultiByte(CP_OEMCP, WC_NO_BEST_FIT_CHARS, shortpath, shortlen, str, slen, 0, 0);
89         delete[] shortpath;
90     }
91     else
92     {
93         m_str = new char[1];
94         *m_str = 0;
95     }
96 
97 #else // __WINDOWS__
98 
99     if (clobber)
100         m_str = (wxT("!") + path).fn_str();
101     else
102         m_str = path.fn_str();
103 
104 #endif // __WINDOWS__
105 }
106 
107 
PHD_fits_open_diskfile(fitsfile ** fptr,const wxString & filename,int iomode,int * status)108 int PHD_fits_open_diskfile(fitsfile **fptr, const wxString& filename, int iomode, int *status)
109 {
110     return fits_open_diskfile(fptr, FitsFname(filename, false, false), iomode, status);
111 }
112 
PHD_fits_create_file(fitsfile ** fptr,const wxString & filename,bool clobber,int * status)113 int PHD_fits_create_file(fitsfile **fptr, const wxString& filename, bool clobber, int *status)
114 {
115     return fits_create_file(fptr, FitsFname(filename, true, clobber), status);
116 }
117 
PHD_fits_close_file(fitsfile * fptr)118 void PHD_fits_close_file(fitsfile *fptr)
119 {
120     int status = 0;
121     fits_close_file(fptr, &status);
122 }
123