@@ -44,7 +44,7 @@ ply_type_name(::Int64) = "int64"
4444ply_type_name (:: Float32 ) = " float32"
4545ply_type_name (:: Float64 ) = " float64"
4646
47- typealias PlyNativeType Union{UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64,Float32,Float64}
47+ @compat const PlyNativeType = Union{UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64,Float32,Float64}
4848
4949ply_type_name {T<:PlyNativeType} (A:: AbstractArray{T} ) = ply_type_name (T)
5050
@@ -56,50 +56,66 @@ const _host_is_little_endian = (ENDIAN_BOM == 0x04030201)
5656
5757
5858function read_header (ply_file)
59- @assert readline (ply_file) == " ply\n "
59+ firstline = Compat. readline (ply_file)
60+ if firstline != " ply"
61+ throw (ErrorException (" Expected \" ply\" header, got \" $firstline \" " ))
62+ end
6063 element_name = " "
6164 element_numel = 0
6265 element_props = Vector {AbstractVector} ()
6366 elements = PlyElement[]
6467 comments = PlyComment[]
6568 format = nothing
6669 while true
67- line = strip (readline (ply_file))
68- if line == " end_header"
70+ line = Compat. readline (ply_file)
71+ if line == " " && eof (ply_file)
72+ throw (ErrorException (" Unexpected end of file reading ply header" ))
73+ elseif line == " end_header"
6974 break
7075 elseif startswith (line, " comment" )
7176 push! (comments, PlyComment (strip (line[8 : end ]), length (elements)+ 1 ))
72- elseif startswith (line, " format" )
73- tok, format_type, format_version = split (line)
74- @assert tok == " format"
75- @assert format_version == " 1.0"
76- format = format_type == " ascii" ? Format_ascii :
77- format_type == " binary_little_endian" ? Format_binary_little :
78- format_type == " binary_big_endian" ? Format_binary_big :
79- error (" Unknown ply format $format_type " )
80- elseif startswith (line, " element" )
81- if ! isempty (element_name)
82- push! (elements, PlyElement (element_name, element_numel, element_props))
83- element_props = Vector {AbstractVector} ()
84- end
85- tok, element_name, element_numel = split (line)
86- @assert tok == " element"
87- element_numel = parse (Int,element_numel)
88- elseif startswith (line, " property" )
77+ else
8978 tokens = split (line)
90- @assert tokens[1 ] == " property"
91- if tokens[2 ] == " list"
92- count_type_name, type_name, prop_name = tokens[3 : end ]
93- count_type = ply_type (count_type_name)
94- type_ = ply_type (type_name)
95- push! (element_props, ListProperty (prop_name, ply_type (count_type_name), ply_type (type_name)))
79+ length (tokens) > 2 || throw (ErrorException (" Bad ply header, line: \" $line \" " ))
80+ if tokens[1 ] == " format"
81+ length (tokens) == 3 || throw (ErrorException (" Bad ply header, line: \" $line \" " ))
82+ _, format_type, format_version = tokens
83+ if format_version != " 1.0"
84+ throw (ErrorException (" Expected ply version 1.0, got $format_version " ))
85+ end
86+ format = format_type == " ascii" ? Format_ascii :
87+ format_type == " binary_little_endian" ? Format_binary_little :
88+ format_type == " binary_big_endian" ? Format_binary_big :
89+ error (" Unknown ply format $format_type " )
90+ elseif tokens[1 ] == " element"
91+ if ! isempty (element_name)
92+ push! (elements, PlyElement (element_name, element_numel, element_props))
93+ element_props = Vector {AbstractVector} ()
94+ end
95+ length (tokens) == 3 || throw (ErrorException (" Bad ply header, line: \" $line \" " ))
96+ _, element_name, element_numel = tokens
97+ element_numel = parse (Int,element_numel)
98+ elseif tokens[1 ] == " property"
99+ ! isempty (element_name) || throw (ErrorException (" Bad ply header: property before first element" ))
100+ if tokens[2 ] == " list"
101+ length (tokens) == 5 || throw (ErrorException (" Bad ply header, line: \" $line \" " ))
102+ count_type_name, type_name, prop_name = tokens[3 : end ]
103+ count_type = ply_type (count_type_name)
104+ type_ = ply_type (type_name)
105+ push! (element_props, ListProperty (prop_name, ply_type (count_type_name), ply_type (type_name)))
106+ else
107+ length (tokens) == 3 || throw (ErrorException (" Bad ply header, line: \" $line \" " ))
108+ type_name, prop_name = tokens[2 : end ]
109+ push! (element_props, ArrayProperty (prop_name, ply_type (type_name)))
110+ end
96111 else
97- type_name, prop_name = tokens[2 : end ]
98- push! (element_props, ArrayProperty (prop_name, ply_type (type_name)))
112+ throw (ErrorException (" Bad ply header, line: \" $line \" " ))
99113 end
100114 end
101115 end
102- push! (elements, PlyElement (element_name, element_numel, element_props))
116+ if ! isempty (element_name)
117+ push! (elements, PlyElement (element_name, element_numel, element_props))
118+ end
103119 elements, format, comments
104120end
105121
0 commit comments