unit upofile;

{$mode delphi}

interface

uses
  Classes, uauxiliares, SysUtils, ubtree;

type
  { TPO_Head }

  TPO_Head = class
    msgid: string; // ""
    msgstr: string; // ""
    lineas: TStringList; {
"Project-Id-Version: SimSEE - Editor\n"
"POT-Creation-Date: 2011-07-09 18:49-0300\n"
"PO-Revision-Date: 2019-11-06 08:58-0300\n"
"Last-Translator: Ruben Chaer <rchaer@simsee.org>\n"
"Language-Team: SimSEE <simsee@fing.edu.uy>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
"X-Poedit-Basepath: c:/simsee/bin\n"
"Language: en\n"
"X-Generator: Poedit 2.2.4\n"
"X-Poedit-SourceCharset: UTF-8\n" }
    constructor CreateFromTXT(var f: textfile);
    procedure WriteToTxt(var f: textfile);
    destructor Destroy; override;
  end;

  { TPO_Rec }

  TPO_Rec = class
    coments: TStringList;
    msgctxt: TStringList;
    msgid: TStringList;
    msgstr: TStringList;
    flg_fuzzy: boolean;
    constructor Create;
    constructor CreateFromTxt(var f: textfile);
    procedure WriteToTxt(var f: textfile);
    destructor Destroy; override;
    function clone: TPO_Rec;
    procedure unfuzz;
  end;


  { TBasePO }

  TBasePO = class(TBTree)
    // Compare impone el orden entre los individuos y debe ser definida
    // en una clase refinada de TBTree para poder usar esta clase
    //  -1 =  b < a ; 0 = b = a ; 1 = b > a
    function Compare(a, b: TItemBTree): integer; override;
  end;



  { TPO_File }
  TPO_File = class
    Head: TPO_Head;
    recs: TList;

    constructor CreateFromArchi(archi: string);
    procedure WriteToArchi(archi: string);
    destructor Destroy; override;
  end;


procedure CopyStrLst(Dest, Org: TStringList);


implementation





procedure CopyStrLst(Dest, Org: TStringList);
var
  k: integer;
begin
  Dest.Clear;
  for k := 0 to Org.Count - 1 do
    Dest.add(Org[k]);
end;



function getNextVal(var s: string): string;
var
  i: integer;
  valor: string;
begin
  i := pos('"', s);
  if i = 0 then
  begin
    Result := '';
    exit;
  end;

  Delete(s, 1, i);
  i := length(s); //pos( '"', s );
  if s[i] <> '"' then
  begin
    Result := '';
    exit;
  end;

  valor := copy(s, 1, i - 1);
  s := '';
  Result := valor;
end;

{ TBasePO }

function TBasePO.Compare(a, b: TItemBTree): integer;
var
  sa, sb: string;
begin
  sa := TPO_Rec(a).msgid.Text;
  sb := TPO_Rec(b).msgid.Text;
 // writeln('sa$', sa, '$ sb$', sb, '$');
  if sa > sb then
    Result := -1
  else if sa = sb then
    Result := 0
  else
    Result := 1;
end;

{ TPO_File }

constructor TPO_File.CreateFromArchi(archi: string);
var
  f: textfile;
  aRec: TPO_Rec;
begin
  assignfile(f, archi);
  reset(f);
  head := TPO_Head.CreateFromTXT(f);
  recs := TList.Create;
  while not EOF(f) do
  begin
    aRec := TPO_Rec.CreateFromTxt(f);
    recs.add(aRec);
  end;
  closefile(f);
end;

procedure TPO_File.WriteToArchi(archi: string);
var
  f: textfile;
  aRec: TPO_Rec;
  k: integer;
begin
  assignfile(f, archi);
  rewrite(f);
  head.WriteToTxt(f);
  for k := 0 to recs.Count - 1 do
  begin
    aRec := TPO_Rec(recs.items[k]);
    aRec.WriteToTxt(f);
  end;
  closefile(f);
end;

destructor TPO_File.Destroy;
var
  k: integer;
begin
  for k := 0 to recs.Count - 1 do
    TPO_Rec(recs[k]).Free;
  recs.Free;
  head.Free;
  inherited Destroy;
end;


{ TPO_Rec }

constructor TPO_Rec.Create;
begin
  coments := TStringList.Create;
  msgctxt := TStringList.Create;
  msgid := TStringList.Create;
  msgstr := TStringList.Create;
end;

constructor TPO_Rec.CreateFromTxt(var f: textfile);
var
  pal, r: string;
  lst: TStringList;

  procedure Activar(aLst: TStringList; val: string);
  begin
    lst := aLst;
    // writeln( 'val: ' , val );
    lst.add(val);
  end;

begin
  Create;

  flg_fuzzy:= false;
  readln(f, r);
  while (length(r) > 0) and (r[1] = '#') do
  begin
    coments.add(r);
    if pos( '#, fuzzy', r ) = 1 then flg_fuzzy:= true;
    readln(f, r);
  end;

  while r <> '' do
  begin
    //   writeln('$'+ r +'$' );
    if r[1] = '"' then
      lst.add(getNextVal(r))
    else
    begin

      pal := getNextPalSep(r, ' ');

      if pal = 'msgctxt' then
        Activar(msgctxt, getNextVal(r))
      else if pal = 'msgid' then
        Activar(msgid, getNextVal(r))
      else if pal = 'msgstr' then
        Activar(msgstr, getNextVal(r))
      else
        raise Exception.Create('NO ESPERABA ESTO: ' + pal + ' ' + r);
      readln(f, r);
    end;
  end;
end;

procedure TPO_Rec.WriteToTxt(var f: textfile);
var
  k: integer;

  procedure print(nombre: string; lst: TStringList);
  var
    j: integer;
  begin
    if lst = nil then
      exit;
    if lst.Count <= 0 then
      exit;
    writeln(f, nombre + ' "' + lst[0] + '"');
    for j := 1 to lst.Count - 1 do
      writeln(f, '"' + lst[j] + '"');
  end;

begin
  for k := 0 to coments.Count - 1 do
    writeln(f, coments[k]);
  print('msgctxt', msgctxt);
  print('msgid', msgid);
  print('msgstr', msgstr);
  writeln(f);
end;

destructor TPO_Rec.Destroy;
begin
  coments.Free;
  msgctxt.Free;
  msgid.Free;
  msgstr.Free;
  inherited Destroy;
end;

function TPO_Rec.clone: TPO_Rec;
var
  aRec: TPO_Rec;

begin
  aRec := TPO_Rec.Create;

  copyStrLst(aRec.coments, self.coments);
  copyStrLst(aRec.msgctxt, self.msgctxt);
  copyStrLst(aRec.msgid, self.msgid);
  copyStrLst(aRec.msgstr, self.msgstr);

  Result := aRec;
end;

procedure TPO_Rec.unfuzz;
var
  k: integer;
begin
  if not flg_fuzzy then exit;
  k:= 0;
  while k < coments.Count do
    if pos( '#, fuzzy', coments[k] ) = 1 then
     coments.delete(k)
    else
      inc( k );
  flg_fuzzy:=false;;
end;

{ TPO_Head }

constructor TPO_Head.CreateFromTXT(var f: textfile);
var
  r: string;
begin
  inherited Create;
  readln(f, msgid);
  readln(f, msgstr);
  lineas := TStringList.Create;
  readln(f, r);
  r := trim(r);
  while r <> '' do
  begin
    lineas.add(r);
    readln(f, r);
  end;
end;

procedure TPO_Head.WriteToTxt(var f: textfile);
var
  k: integer;
begin
  writeln(f, msgid);
  writeln(f, msgstr);

  for k := 0 to lineas.Count - 1 do
    writeln(f, lineas[k]);
  writeln(f, '');
end;

destructor TPO_Head.Destroy;
begin
  lineas.Free;
  inherited Destroy;
end;

 (*
#: simseeedit.rseditor
msgctxt "simseeedit.rseditor"
msgid "Editor"
msgstr "Editor"

#: taltaedicionprintcronvar_compararvaloresmultiplescronvars.bagregar.caption
#, fuzzy
msgctxt "taltaedicionprintcronvar_compararvaloresmultiplescronvars.bagregar.caption"
msgid "Agregar"
msgstr "Add"

#: taltaedicionprintcronvar_compararvaloresmultiplescronvars.bauto.caption
msgctxt "taltaedicionprintcronvar_compararvaloresmultiplescronvars.bauto.caption"
msgid "Auto"
msgstr ""

   *)




end.
