1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
| with text_io,ada.Sequential_Io,ada.command_line,ada.integer_text_io;
use text_io,ada.command_line,ada.integer_text_io;
procedure compressore is
type Nodo;
type Punta is access Nodo;
type Nodo is record
Carattere: Character;
succ: Punta;
end record;
type Dato is record
Carattere:Character;
Primo:integer;
Secondo:integer;
end record;
TYPE Data IS RECORD
Byte:Character;
END RECORD;
package Dato_io is new ada.Sequential_Io(Element_Type=> Dato);
PACKAGE Data_Io IS NEW Ada.Sequential_IO(Element_Type=>Data);
USE Data_Io;
PROCEDURE Get(Filein:Data_Io.File_Type;What:IN OUT Character) IS
Temp:Data;
BEGIN
Data_Io.Read(Filein,Temp);
What:=Temp.Byte;
END Get;
PROCEDURE Put(Fileout:Data_Io.File_Type;What:IN OUT Character) IS
Temp:Data;
BEGIN
Temp.Byte:=What;
Data_Io.Write(Fileout,Temp);
END Put;
procedure put(ini:punta) is
pun:punta;
begin
pun:=ini;
while (pun/=null) loop
put(pun.carattere);
pun:=pun.succ;
end loop;
end put;
-- Procedura Inserisci
-- IN: inizio, fine (puntatori alla coda)
-- lung: lunghezza attuale della coda
-- max: grandezza massima della coda
-- chr: carattere da inserire
procedure Inserisci (Ini,Fine:in out Punta;lung,Max:in out integer;Chr:Character) is
temp,temp2:punta;
begin
if Lung=0 then -- Se la coda non esiste
Temp:=new Nodo'(Chr,null);
Ini:=Temp;
Fine:=Temp;
Lung:=1;
elsif Lung<Max then -- Se non ho raggiunto la lunghezza massima
Temp:=new Nodo'(Chr,null);
Ini.Succ:=Temp;
Ini:=Temp;
lung:=lung+1;
else -- Se ho raggiunto la lunghezza massima
Temp:=new Nodo'(Chr,null);
Ini.Succ:=Temp;
Ini:=Temp;
temp2:=Fine.Succ; -- Si spera che il Garbage Collector lo elimini
fine.succ:=null;
fine:=temp2;
end if;
end Inserisci;
-- Funzione cerca
-- IN: buffer,stringa da cercare;
-- OUT: posizione sul buffer della stringa nella posizione migliore
-- TESTATA OK!
function cerca(buf,str:punta) return integer is
buffer,stringa:punta;
i,temp:integer:=0;
ok,complete:boolean:=true;
begin
if buf = null or str = null then
return 0;
else
complete:=false;
buffer:=buf;
i:=1;
while buffer /= null loop
stringa:=str;
if buffer.carattere = stringa.carattere then
temp:=i;
complete:=false;
ok:=true;
while ok loop
buffer:=buffer.succ;
stringa:=stringa.succ;
i:=i+1;
if stringa = null or buffer = null then
ok:=false;
complete:=true;
elsif stringa.carattere /= buffer.carattere then
ok:=false;
end if;
end loop;
else
buffer:=buffer.succ;
i:=i+1;
end if;
if complete=true then
if buffer = null then
return 0;
else
return temp;
end if;
end if;
end loop;
return 0;
end if;
end cerca;
-- procedure Inserisci (Ini,Fine:in out Punta;lung,Max:in out integer;Chr:Character)
procedure ributta(inicarne,fincarne,inivitt,finvitt:in out punta;lcarn,maxcarn,lvitt:in out integer) is
temp:punta;
begin
temp:=finvitt;
while temp/= null loop
inserisci(inicarne,fincarne,lcarn,maxcarn,temp.carattere);
temp:=temp.succ;
end loop;
finvitt:=null;
inivitt:=null;
lvitt:=0;
end ributta;
-- function cerca(buf,str:punta) return integer is
-- procedure Inserisci (Ini,Fine:in out Punta;lung,Max:in out integer;Chr:Character)
procedure comprimi(fin,fout:string;maxbuf,maxwin:in out integer) is
filein:data_io.file_type;
fileout:dato_io.file_type;
bufferi,bufferf,windowi,windowf:punta;
lbuf,lwin,best:integer:=0;
tmp:character;
data:dato;
begin
data_io.open(File=>filein,mode=>in_file,name=>fin);
dato_io.create(fileout,dato_io.out_file,fout);
best:=0;
while not end_of_file(filein) loop
get(filein,tmp);
--new_line;put("Byte numero: ");put(bytes,0);new_line;
--put("Letto: ");put(tmp);new_line;
--put("Finestra: ");put(windowf);new_line;
if not end_of_file(filein) then inserisci(windowi,windowf,lwin,maxwin,tmp); end if;
if cerca(bufferf,windowf) = 0 or lwin=256 or end_of_file(filein) then
data.carattere:=tmp;
data.primo:=best;
best:=0;
if not end_of_file(filein) then
lwin:=lwin-1;
end if;
data.secondo:=lwin;
dato_io.write(fileout,data);
-- put("Scrivo ");put(data.carattere);
-- put(",");put(data.primo,0);put(",");put(data.secondo,0);new_line;
ributta(bufferi,bufferf,windowi,windowf,lbuf,maxbuf,lwin);
-- put("Buffer: ");put(bufferf);new_line;
windowi:=null; -- Cancella la finestra
windowf:=null;
lwin:=0;
else
best:=cerca(bufferf,windowf);
--put("Buffer: ");put(bufferf);new_line;
--put("Migliore posizione: ");put(best);new_line;
end if;
end loop;
close(filein);
dato_io.close(fileout);
new_line;
put("Done");
new_line;
end comprimi;
procedure decomprimi(fin,fout:string;maxbuf: in out integer) is
filein:dato_io.file_type;
fileout:data_io.file_type;
bufferi,bufferf,temp,fitempi,fitempf:punta;
lbuf,best,i,a,ltemp:integer:=0;
data:dato;
maxtemp:integer:=256;
begin
dato_io.open(filein,dato_io.in_file,fin);
data_io.create(fileout,out_file,fout);
while not dato_io.end_of_file(filein) loop
Dato_Io.Read(Filein,Data);
--New_Line;
--Put("Dato letto: primo: ");Put(Data.Primo,0);Put(" secondo:");Put(Data.Secondo,0);Put(" carattere:");Put(Data.Carattere);
--New_Line;
--Put("Buffer: ");
--put(bufferf);
if data.primo = 0 then -- Se non richiede di riprecorrere il buffer
Put(Fileout,Data.Carattere);
Inserisci(Bufferi,Bufferf,Lbuf,Maxbuf,Data.Carattere);
else
Temp:=Bufferf;
FOR I IN 1..Data.Primo-1 LOOP -- si posiziona sull'elemento
temp:=temp.succ;
END LOOP;
FOR I IN 1..Data.Secondo LOOP -- scrive il numero di caratteri indicato nel buffer
Inserisci(Fitempi,Fitempf,Ltemp,maxtemp,Temp.Carattere);
Temp:=Temp.Succ;
END LOOP;
Ributta(Bufferi,Bufferf,Fitempi,Fitempf,Lbuf,Maxbuf,Ltemp);
Inserisci(Bufferi,Bufferf,Lbuf,Maxbuf,Data.Carattere);
temp:=bufferf;
for i in 1..data.primo-1 loop -- posizionamento sull'elemento
temp:=temp.succ;
end loop;
for i in 1..data.secondo loop -- scrittura del pezzo di stringa su file
Put(Fileout,Temp.Carattere);
temp:=temp.succ;
end loop;
Put(Fileout,Data.Carattere); -- scrittura del carattere
end if;
end loop;
dato_io.close(filein);
close(fileout);
new_line;
put("Done");
new_line;
end decomprimi;
-- NOTA PER IL PASSAGGIO DI ATTRIBUTI TRAMITE LINEA DI COMANDO
-- Ottenere il numero di argomenti: Argument_count (restituisce un natural);
-- Ottenere l'argomento numero x: argument(x);
-- Ottenere l'intera stringa: command_name (restituisce una stringa);
buffer,stringa:punta:=null;
i,maxbuf,maxstringa:integer:=0;
finput,foutput:string(1..32):=(others=>' ');
lin,lout:integer:=0;
decomp:boolean:=false;
begin
if Argument_count = 0 then
new_line;
put("Error: Parameters are missing!");
new_line(2);
else
i:=1;
while i <= argument_count loop
if argument(i)="-in" then
i:=i+1;
finput(argument(i)'range):=argument(i)(argument(i)'range);
lin:=i;
elsif argument(i)="-out" then
i:=i+1;
foutput(argument(i)'range):=argument(i)(argument(i)'range);
lout:=i;
elsif argument(i)="-undo" then
decomp:=true;
end if;
i:=i+1;
end loop;
if lin=0 or lout=0 then
new_line;
if lin=0 then
put("Error: No input file specified!"); new_line;
end if;
if lout=0 then
put("Error: No output file specified!"); new_line;
end if;
new_line(2);
else
if maxbuf=0 then
new_line;
put("No buffer max length specified! Using Default (65535)");
maxbuf:=65535;
end if;
if maxstringa=0 then
new_line;
put("No window max length specified! Using Default (256)");
maxstringa:=256;
end if;
if decomp=true then
decomprimi(finput(argument(lin)'range),foutput(argument(lout)'range),maxbuf);
else
comprimi(finput(argument(lin)'range),foutput(argument(lout)'range),maxbuf,maxstringa);
end if;
end if;
end if;
end compressore; |
Partager