Commit 07bbfcdb authored by Antonino's avatar Antonino

Added StringViews and write(::Records). Removed emacs files

parent d99998d0
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
julia_version = "1.11.6" julia_version = "1.11.6"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709" project_hash = "bd39c77bc66a0e20a3905bc4afca19c6a1650ebb"
[deps] [[deps.StringViews]]
git-tree-sha1 = "f2dcb92855b31ad92fe8f079d4f75ac57c93e4b8"
uuid = "354b36f9-a18e-4713-926e-db85100087ba"
version = "1.3.7"
...@@ -2,3 +2,9 @@ name = "GeneratedFiles" ...@@ -2,3 +2,9 @@ name = "GeneratedFiles"
uuid = "dd08c5e9-7c90-4908-81e2-08ae9a8bbe49" uuid = "dd08c5e9-7c90-4908-81e2-08ae9a8bbe49"
authors = ["Antonino <anda982008@gmail.com>"] authors = ["Antonino <anda982008@gmail.com>"]
version = "0.1.0" version = "0.1.0"
[deps]
StringViews = "354b36f9-a18e-4713-926e-db85100087ba"
[compat]
StringViews = "1.3.7"
mutable struct Tag
tag::String
#=
Order records by position. if pos<0 they are put at the end of the list.
=#
pos::Int64 ##
Tag(s, pos) = new(s,pos)
Tag(s) = Tag(s,-1)
end
mutable struct Record
buffer::IOBuffer
tag::Tag
Record(b::IOBuffer,tag::Tag) = new(b,tag)
Record(b::IOBuffer,tag::AbstractString, pos::Int64 = -1) = new(b,Tag(tag,pos))
Record(b,tag::Tag) = new(IOBuffer(b),tag)
Record(b,tag::AbstractString,pos::Int64=-1) = new(IOBuffer(b),Tag(tag,pos))
end
mutable struct GeneratedFile
filename::String
records::Vector{Record}
GeneratedFile(io::String, r::Vector{Record} = Record[]) = new(io,r)
end
import Base: show
function show(io::IO,gf::GeneratedFile)
nr = length(gf.records)
print(io,"GeneratedFile: $(gf.filename) with $nr records")
end
show(io::IO,r::Record) = show(io,r.tag)
show(io::IO,t::Tag) = print(io,"Tag: ", t.tag," position: ", t.pos)
import Base: print
function Base.print(io::IO,r::Record)
nchar = div(r.buffer.size,sizeof(Char))
nchar >80
print(io,"Record: ",r.tag.tag,"\nText: ",String(take!(r.buffer)))
end
nuccio@nuccio-Lenovo-IdeaPad-S340-15API.41142:1776683147
\ No newline at end of file
module GeneratedFiles module GeneratedFiles
using StringViews
include("types.jl") include("types.jl")
include("filemanip.jl") include("filemanip.jl")
......
...@@ -30,18 +30,11 @@ function findEndRecord(io::IOStream) ...@@ -30,18 +30,11 @@ function findEndRecord(io::IOStream)
return p2 return p2
end end
function readRecord(io)
is = position(io)
ie = findEndRecord(io)
seek(io, is)
buffer = IOBuffer(read(io,ie-is))
return Record(buffer)
end
function readGF(filename) function readGF(filename)
records = Vector{Record}() records = Vector{Record}()
open(filename,"r") do io open(filename,"r") do io
while seekRecord(io) while seekRecord(io)
push!(records,readRecord(io)) push!(records,read_record(io))
end end
end end
return GeneratedFile(filename,records) return GeneratedFile(filename,records)
...@@ -55,7 +48,7 @@ function get_tag_and_pos(s::AbstractString) ...@@ -55,7 +48,7 @@ function get_tag_and_pos(s::AbstractString)
return tag,pos return tag,pos
end end
function Record(b::IOBuffer) function read_record(b::IO)
tag,pos = get_tag_and_pos(readline(b)) tag,pos = get_tag_and_pos(readline(b))
is = position(b) is = position(b)
length = 0 length = 0
...@@ -67,7 +60,7 @@ function Record(b::IOBuffer) ...@@ -67,7 +60,7 @@ function Record(b::IOBuffer)
me = match(TAG_REGEX,l) me = match(TAG_REGEX,l)
isnothing(me) && error("missing tag at the end of a Record") isnothing(me) && error("missing tag at the end of a Record")
if tag != me.match if tag != me.match
error("Record is malformed:\nIt start with $(ms.match)\nIt ends with $(me.match)") error("Record is malformed:\nIt start with $(tag)\nIt ends with $(me.match)")
end end
break; break;
end end
...@@ -75,18 +68,37 @@ function Record(b::IOBuffer) ...@@ -75,18 +68,37 @@ function Record(b::IOBuffer)
return Record(IOBuffer(read(b,length)),tag,pos) return Record(IOBuffer(read(b,length)),tag,pos)
end end
add_records!(GF::GeneratedFile,r::Record...) = push!(GF.records,r...) import Base.isequal
Base.isequal(a::Tag,b::Tag) = isequal(a.tag,b.tag)
function add_records!(GF::GeneratedFile,r::Record...)
idx = fill(true, length(r))
for (i,_r) in enumerate(r)
_idx = findfirst(x->isequal(x.tag, _r.tag), GF.records)
!isnothing(_idx) || continue
GF.records[_idx] = _r
idx[i] = false
end
push!(GF.records,r[idx]...)
end
remove_records!(GF::GeneratedFile,tag::String) = filter!(r->r.tag.tag != tag, GF.records) remove_records!(GF::GeneratedFile,tag::String) = filter!(r->r.tag.tag != tag, GF.records)
remove_records!(GF::GeneratedFile,tag::Tag) = remove_record(GF,tag.tag) remove_records!(GF::GeneratedFile,tag::Tag) = remove_record(GF,tag.tag)
import Base.write
function Base.write(r::Record,x...)
r.buffer.writable || error("Cannot modify record $(r.tag.tag)")
Base.write(r.buffer, x...)
end
function GF_write(io,r::Record) function GF_write(io,r::Record)
println(io,START_RECORD," ",r.tag.tag," ",r.tag.pos) println(io,START_RECORD," ",r.tag.tag," ",r.tag.pos)
print(io,String(take!(r.buffer))) sw = StringView(r.buffer.data[1:r.buffer.size])
seekend(r.buffer) print(io,sw)
seek(r.buffer,position(r.buffer)-1) sw[end]== '\n' || println(io)
startswith(r.buffer,"\n") || print(io,"\n")
println(io,END_RECORD," ",r.tag.tag) println(io,END_RECORD," ",r.tag.tag)
nothing nothing
end end
...@@ -97,6 +109,8 @@ function isless_Tag(a::Tag,b::Tag) ...@@ -97,6 +109,8 @@ function isless_Tag(a::Tag,b::Tag)
end end
isless_Record(a::Record,b::Record) = isless_Tag(a.tag, b.tag) isless_Record(a::Record,b::Record) = isless_Tag(a.tag, b.tag)
function resolve_negative_pos!(R::Vector{Record}) function resolve_negative_pos!(R::Vector{Record})
any(r.tag.pos<=0 for r in R) || return any(r.tag.pos<=0 for r in R) || return
maxP = max(1, maximum(R[i].tag.pos for i in eachindex(R))+1) maxP = max(1, maximum(R[i].tag.pos for i in eachindex(R))+1)
......
...@@ -4,17 +4,27 @@ mutable struct Tag ...@@ -4,17 +4,27 @@ mutable struct Tag
Order records by position. if pos<0 they are put at the end of the list. Order records by position. if pos<0 they are put at the end of the list.
=# =#
pos::Int64 ## pos::Int64 ##
Tag(s, pos) = new(s,pos) Tag(s="", pos=-1) = new(s,pos)
Tag(s) = Tag(s,-1)
end end
mutable struct Record mutable struct Record
buffer::IOBuffer buffer::IOBuffer
tag::Tag tag::Tag
Record(b::IOBuffer,tag::Tag) = new(b,tag) Record(b::IOBuffer,tag::Tag) = new(b,tag)
Record(b::IOBuffer,tag::AbstractString, pos::Int64 = -1) = new(b,Tag(tag,pos)) Record(b::IOBuffer,tag::AbstractString = "", pos::Int64 = -1) = new(b,Tag(tag,pos))
Record(b,tag::Tag) = new(IOBuffer(b),tag) function Record(b,tag::Tag)
Record(b,tag::AbstractString,pos::Int64=-1) = new(IOBuffer(b),Tag(tag,pos)) buf = IOBuffer()
write(buf,b)
new(buf,tag)
end
function Record(b,tag::AbstractString = "",pos::Int64=-1)
buf = IOBuffer()
write(buf,b)
t = Tag(tag,pos)
new(buf, t)
end
Record(tag::Tag) = new(IOBuffer(),tag)
Record(tag::AbstractString = "", pos::Int64=-1) = new(IOBuffer(),Tag(tag,pos))
end end
mutable struct GeneratedFile mutable struct GeneratedFile
...@@ -37,5 +47,7 @@ show(io::IO,t::Tag) = print(io,"Tag: ", t.tag," position: ", t.pos) ...@@ -37,5 +47,7 @@ show(io::IO,t::Tag) = print(io,"Tag: ", t.tag," position: ", t.pos)
import Base: print import Base: print
function Base.print(io::IO,r::Record) function Base.print(io::IO,r::Record)
nchar = div(r.buffer.size,sizeof(Char))
nchar >80
print(io,"Record: ",r.tag.tag,"\nText: ",String(take!(r.buffer))) print(io,"Record: ",r.tag.tag,"\nText: ",String(take!(r.buffer)))
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment