Skip to content

Commit

Permalink
Merge pull request #83 from giordano/ascii
Browse files Browse the repository at this point in the history
Assert strings written to file are ASCII
  • Loading branch information
kbarbary committed Sep 21, 2017
2 parents d3d9af2 + 9827f84 commit d034834
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/FITSIO.jl
Expand Up @@ -44,6 +44,7 @@ using .Libcfitsio
# defined in Libcfitsio, even though it is not used there.
import .Libcfitsio: libcfitsio,
fits_assert_ok,
fits_assert_isascii,
TYPE_FROM_BITPIX

# HDU Types
Expand Down Expand Up @@ -85,7 +86,7 @@ following operations:
* `f[i]`: Return the `i`-th HDU.
* `f[name]` or `f[name, ver]`: Return the HDU containing the given the given EXTNAME
(or HDUNAME) keyword (an String), and optionally the given EXTVER (or HDUVER)
(or HDUNAME) keyword (a String), and optionally the given EXTVER (or HDUVER)
number (an Integer).
* Iteration:
for hdu in f
Expand Down
3 changes: 3 additions & 0 deletions src/header.jl
Expand Up @@ -299,6 +299,7 @@ getindex(hdr::FITSHeader, key::String) = hdr.values[hdr.map[key]]
getindex(hdr::FITSHeader, i::Integer) = hdr.values[i]

function setindex!(hdr::FITSHeader, value::Any, key::String)
fits_assert_isascii(key)
if in(key, hdr.keys)
hdr.values[hdr.map[key]] = value
else
Expand Down Expand Up @@ -328,9 +329,11 @@ get_comment(hdr::FITSHeader, i::Integer) = hdr.comments[i]
Set the comment baed on keyword or index.
"""
function set_comment!(hdr::FITSHeader, key::String, comment::String)
fits_assert_isascii(comment)
hdr.comments[hdr.map[key]] = comment
end
function set_comment!(hdr::FITSHeader, i::Integer, comment::String)
fits_assert_isascii(comment)
hdr.comments[i] = comment
end

Expand Down
14 changes: 14 additions & 0 deletions src/libcfitsio.jl
Expand Up @@ -208,6 +208,9 @@ function fits_assert_ok(status::Cint, filename = nothing)
end
end

fits_assert_isascii(str::String) =
!isascii(str) && error("FITS file format accepts ASCII strings only")

fits_get_version() = ccall((:ffvers, libcfitsio), Cfloat, (Ptr{Cfloat},), &0.)

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -468,6 +471,8 @@ Write a keyword of the appropriate data type into the CHU.
"""
function fits_write_key(f::FITSFile, keyname::String,
value::Union{Real,String}, comment::String)
fits_assert_isascii(keyname)
fits_assert_isascii(comment)
cvalue = isa(value,String) ? value :
isa(value,Bool) ? Cint[value] : reinterpret(UInt8, [value])
status = Ref{Cint}(0)
Expand All @@ -485,13 +490,15 @@ function fits_write_date(f::FITSFile)
end

function fits_write_comment(f::FITSFile, comment::String)
fits_assert_isascii(comment)
status = Ref{Cint}(0)
ccall((:ffpcom, libcfitsio), Cint, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}),
f.ptr, comment, status)
fits_assert_ok(status[])
end

function fits_write_history(f::FITSFile, history::String)
fits_assert_isascii(history)
status = Ref{Cint}(0)
ccall((:ffphis, libcfitsio), Cint, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}),
f.ptr, history, status)
Expand All @@ -505,6 +512,7 @@ for (a,T,S) in (("ffukys", :(String), :(Ptr{UInt8})),
@eval begin
function fits_update_key(f::FITSFile, key::String, value::$T,
comment::Union{String, Ptr{Void}}=C_NULL)
isa(value, String) && fits_assert_isascii(value)
status = Ref{Cint}(0)
ccall(($a, libcfitsio), Cint,
(Ptr{Void}, Ptr{UInt8}, $S, Ptr{UInt8}, Ref{Cint}),
Expand All @@ -516,6 +524,7 @@ end

function fits_update_key(f::FITSFile, key::String, value::AbstractFloat,
comment::Union{String, Ptr{Void}}=C_NULL)
isa(comment, String) && fits_assert_isascii(comment)
status = Ref{Cint}(0)
ccall(("ffukyd", libcfitsio), Cint,
(Ptr{Void}, Ptr{UInt8}, Cdouble, Cint, Ptr{UInt8}, Ref{Cint}),
Expand All @@ -525,6 +534,7 @@ end

function fits_update_key(f::FITSFile, key::String, value::Void,
comment::Union{String, Ptr{Void}}=C_NULL)
isa(comment, String) && fits_assert_isascii(comment)
status = Ref{Cint}(0)
ccall(("ffukyu", libcfitsio), Cint,
(Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}, Ref{Cint}),
Expand All @@ -538,6 +548,7 @@ end
Write a user specified keyword record into the CHU.
"""
function fits_write_record(f::FITSFile, card::String)
fits_assert_isascii(card)
status = Ref{Cint}(0)
ccall((:ffprec,libcfitsio), Cint,
(Ptr{Void},Ptr{UInt8},Ref{Cint}),
Expand Down Expand Up @@ -860,6 +871,8 @@ for (a,b) in ((:fits_create_binary_tbl, 2),
function ($a)(f::FITSFile, numrows::Integer,
coldefs::Array{ColumnDef}, extname::String)

fits_assert_isascii(extname)

# get length and convert coldefs to three arrays of Ptr{Uint8}
ntype = length(coldefs)
ttype = [pointer(x[1]) for x in coldefs]
Expand Down Expand Up @@ -1123,6 +1136,7 @@ function fits_write_col(f::FITSFile,
firstrow::Integer,
firstelem::Integer,
data::Array{String})
for el in data; fits_assert_isascii(el); end
status = Ref{Cint}(0)
ccall((:ffpcls, libcfitsio), Cint,
(Ptr{Void}, Cint, Int64, Int64, Int64,
Expand Down
2 changes: 2 additions & 0 deletions src/table.jl
Expand Up @@ -245,6 +245,7 @@ end

function fits_write_var_col(f::FITSFile, colnum::Integer,
data::Vector{String})
for el in data; fits_assert_isascii(el); end
status = Ref{Cint}(0)
buffer = Ref{Ptr{UInt8}}() # holds the address of the current row
for i=1:length(data)
Expand All @@ -268,6 +269,7 @@ function write_internal(f::FITS, colnames::Vector{String},
coldata::Vector, hdutype, name, ver, header, units,
varcols)
fits_assert_open(f.fitsfile)
for el in colnames; fits_assert_isascii(el); end

# move to last HDU; table will be added after the CHDU
nhdus = Int(fits_get_num_hdus(f.fitsfile))
Expand Down

0 comments on commit d034834

Please sign in to comment.