Commit d99998d0 authored by Antonino's avatar Antonino

First working versions.

parents
# This file is machine-generated - editing it directly is not advised
julia_version = "1.11.6"
manifest_format = "2.0"
project_hash = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
[deps]
name = "GeneratedFiles"
uuid = "dd08c5e9-7c90-4908-81e2-08ae9a8bbe49"
authors = ["Antonino <anda982008@gmail.com>"]
version = "0.1.0"
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
include("types.jl")
include("filemanip.jl")
export GeneratedFile, readGF, Record,Tag
export GF_write, add_records!, remove_records!
end
const COMMENTCHAR = "%"
const START_RECORD = string(COMMENTCHAR,"-start-",COMMENTCHAR)
const END_RECORD = string(COMMENTCHAR,"-end-",COMMENTCHAR)
const TAG_REGEX = Regex("(?<=$(START_RECORD) |$(END_RECORD) )([^\\s]+)")
const POS_REGEX = Regex("(?<=$(START_RECORD) |$(END_RECORD) )([^\\s]+)(\\s*)(\\d+)")
function seekRecord(io::IOStream)
while true
eof(io) && return false
startswith(io,START_RECORD) && return true
readline(io)
end
end
function findEndRecord(io::IOStream)
p1 = position(io)
p2 = -1
while true
if eof(io)
p2 = position(io)
break;
end
if startswith(io,END_RECORD)
readline(io)
p2 = position(io)
break;
end
readline(io)
end
return p2
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)
records = Vector{Record}()
open(filename,"r") do io
while seekRecord(io)
push!(records,readRecord(io))
end
end
return GeneratedFile(filename,records)
end
function get_tag_and_pos(s::AbstractString)
ms = match(POS_REGEX,s)
isnothing(ms) && error("missing tag in Record")
pos = parse(Int64,match(r"[\d]+",ms.match).match)
tag = match(r"[^\s]+",ms.match).match
return tag,pos
end
function Record(b::IOBuffer)
tag,pos = get_tag_and_pos(readline(b))
is = position(b)
length = 0
for l in eachline(b)
if !startswith(l,END_RECORD)
length = position(b)-is
continue
end
me = match(TAG_REGEX,l)
isnothing(me) && error("missing tag at the end of a Record")
if tag != me.match
error("Record is malformed:\nIt start with $(ms.match)\nIt ends with $(me.match)")
end
break;
end
seek(b,is)
return Record(IOBuffer(read(b,length)),tag,pos)
end
add_records!(GF::GeneratedFile,r::Record...) = push!(GF.records,r...)
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)
function GF_write(io,r::Record)
println(io,START_RECORD," ",r.tag.tag," ",r.tag.pos)
print(io,String(take!(r.buffer)))
seekend(r.buffer)
seek(r.buffer,position(r.buffer)-1)
startswith(r.buffer,"\n") || print(io,"\n")
println(io,END_RECORD," ",r.tag.tag)
nothing
end
function isless_Tag(a::Tag,b::Tag)
a.pos == b.pos && return a.tag < b.tag
return a.pos<b.pos
end
isless_Record(a::Record,b::Record) = isless_Tag(a.tag, b.tag)
function resolve_negative_pos!(R::Vector{Record})
any(r.tag.pos<=0 for r in R) || return
maxP = max(1, maximum(R[i].tag.pos for i in eachindex(R))+1)
for (i,r) in enumerate(R)
r.tag.pos<=0 || continue;
R[i].tag.pos = maxP
maxP+=1
end
return
end
order_records!(R::Vector{Record}) = sort!(R,lt=isless_Record)
function GF_write(GF::GeneratedFile)
resolve_negative_pos!(GF.records)
order_records!(GF.records);
open(GF.filename,"w") do io
GF_write.(io,GF.records)
end
nothing
end
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)
print(io,"Record: ",r.tag.tag,"\nText: ",String(take!(r.buffer)))
end
%-start-% film 1
La vita è bella
%-end-% film
%-start-% film_d'animazione 2
Il re Leone
%-end-% film_d'animazione
%-start-% TV_Show 4
Modern Family
%-end-% TV_Show
%-start-% Musical 5
The Rocky Horror Picture Show
%-end-% Musical
using Revise, GeneratedFiles
GF = GeneratedFile("Cinema.txt")
add_records!(GF,Record("La vita è bella", "film", 1))
add_records!(GF,Record("Il re Leone", Tag("film_d'animazione", 2)),
Record("The Rocky Horror Picture Show\n", "Musical"),
Record("Modern Family", "TV_Show", 4))
GF_write(GF)
GF2 = readGF("Cinema.txt")
remove_records!(GF,"TV_Show")
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