About simple parsing a text file with objects

Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
edited 2008-01-16 in NAV Tips & Tricks
Hi All,

Below you can find some C/AL code for fast parsing text file with NAV objects.

OBJECT Table 50000 C/AL Code
{
  OBJECT-PROPERTIES
  {
    Date=28-11-07;
    Time=09:47:38;
    Modified=Yes;
    Version List=Research;
  }
  PROPERTIES
  {
  }
  FIELDS
  {
    { 1   ;   ;Line No.            ;Integer        }
    { 2   ;   ;Line Text           ;Text250        }
    { 3   ;   ;Nesting             ;Integer        }
    { 4   ;   ;Line Blob           ;BLOB           }
    { 5   ;   ;Object Name         ;Text250        }
  }
  KEYS
  {
    {    ;Line No.                                ;Clustered=Yes }
  }
  CODE
  {

    BEGIN
    END.
  }
}

OBJECT Form 50000 C/AL Code
{
  OBJECT-PROPERTIES
  {
    Date=28-11-07;
    Time=11:20:52;
    Modified=Yes;
    Version List=Research;
  }
  PROPERTIES
  {
    Width=9790;
    Height=6710;
    InsertAllowed=No;
    TableBoxID=1;
    SourceTable=Table50000;
    OnOpenForm=BEGIN
                 CASE Mode OF
                   Mode::Object:
                    BEGIN
                     FILTERGROUP := 2;
                     SETRANGE(Nesting,0);
                     SETFILTER("Line Text",'<>%1&<>%2&<>%3','','{','}');
                     FILTERGROUP := 0;
                      CurrForm.btnFile.VISIBLE := TRUE;
                      CurrForm.BtnObject.VISIBLE := TRUE;
                    END;
                   Mode::Source:
                    BEGIN
                     CurrForm.btnFile.VISIBLE := FALSE;
                     CurrForm.BtnObject.VISIBLE := FALSE;
                    END;
                 END;

                 IF FIND('-') THEN ;
               END;

  }
  CONTROLS
  {
    { 1   ;TableBox     ;220  ;220  ;9350 ;5500 ;HorzGlue=Both;
                                                 VertGlue=Both;
                                                 InlineEditing=Yes }
    { 2   ;TextBox      ;0    ;0    ;1693 ;0    ;Visible=No;
                                                 ParentControl=1;
                                                 InColumn=Yes;
                                                 SourceExpr="Line No.";
                                                 OnFormat=BEGIN
                                                            CurrForm."Line No.".UPDATEFONTBOLD((Nesting = 0) AND ("Line Text" <>''));
                                                          END;
                                                           }
    { 3   ;Label        ;0    ;0    ;0    ;0    ;ParentControl=2;
                                                 InColumnHeading=Yes }
    { 4   ;TextBox      ;0    ;0    ;4400 ;0    ;HorzGlue=Both;
                                                 ParentControl=1;
                                                 InColumn=Yes;
                                                 SourceExpr="Line Text";
                                                 OnFormat=BEGIN
                                                            CurrForm."Line Text".UPDATEFONTBOLD((Nesting = 0) AND ("Line Text" <>''));
                                                          END;
                                                           }
    { 5   ;Label        ;0    ;0    ;0    ;0    ;ParentControl=4;
                                                 InColumnHeading=Yes }
    { 1060000;TextBox   ;6086 ;1980 ;4400 ;440  ;ParentControl=1;
                                                 InColumn=Yes;
                                                 SourceExpr="Object Name" }
    { 1060001;Label     ;0    ;0    ;0    ;0    ;ParentControl=1060000;
                                                 InColumnHeading=Yes }
    { 6   ;TextBox      ;0    ;0    ;1700 ;0    ;Visible=No;
                                                 ParentControl=1;
                                                 InColumn=Yes;
                                                 SourceExpr=Nesting;
                                                 OnFormat=BEGIN
                                                            CurrForm.Nesting.UPDATEFONTBOLD((Nesting = 0) AND ("Line Text" <>''));
                                                          END;
                                                           }
    { 7   ;Label        ;0    ;0    ;0    ;0    ;ParentControl=6;
                                                 InColumnHeading=Yes }
    { 10  ;CommandButton;7370 ;5940 ;2200 ;550  ;HorzGlue=Right;
                                                 VertGlue=Bottom;
                                                 PushAction=FormHelp }
    { 8   ;MenuButton   ;2750 ;5940 ;2200 ;550  ;Name=btnFile;
                                                 HorzGlue=Right;
                                                 VertGlue=Bottom;
                                                 CaptionML=ENU=&File;
                                                 Menu=MENUITEMS
                                                 {
                                                   { ID=9;
                                                     CaptionML=ENU=Import...;
                                                     OnPush=BEGIN
                                                              CALCode.Import(SDialog.OpenFile('','',1,'',0));
                                                            END;
                                                             }
                                                   { ID=11;
                                                     CaptionML=ENU=Export...;
                                                     OnPush=BEGIN
                                                              CurrForm.SETSELECTIONFILTER(recObj);
                                                              CALCode.Export(SDialog.OpenFile('','',1,'',1),recObj);
                                                            END;
                                                             }
                                                 }
                                                  }
    { 12  ;MenuButton   ;5060 ;5940 ;2200 ;550  ;Name=BtnObject;
                                                 HorzGlue=Right;
                                                 VertGlue=Bottom;
                                                 CaptionML=ENU=Object;
                                                 Menu=MENUITEMS
                                                 {
                                                   { ID=13;
                                                     CaptionML=ENU=Show;
                                                     OnPush=VAR
                                                              StartPos@1001 : Integer;
                                                            BEGIN
                                                              StartPos := "Line No.";
                                                              recObj.RESET;
                                                              recObj.SETFILTER("Line No.",'>=%1',"Line No.");
                                                              IF recObj.FIND('-') THEN
                                                              REPEAT
                                                              UNTIL (recObj.NEXT = 0) OR ((recObj.Nesting = 0) AND (recObj."Line Text" = ''));
                                                              recObj.FILTERGROUP := 2;
                                                              recObj.SETFILTER("Line No.",'%1..%2',StartPos,recObj."Line No.");
                                                              recObj.FILTERGROUP := 0;
                                                              CLEAR(frmObj);
                                                              frmObj.SETTABLEVIEW(recObj);
                                                              frmObj.SetMode(Mode::Source);
                                                              frmObj.RUN;
                                                            END;
                                                             }
                                                 }
                                                  }
    { 1060002;CommandButton;220;5940;880  ;660  ;VertGlue=Bottom;
                                                 ShowCaption=No;
                                                 BitmapPos=Center;
                                                 Bitmap=18;
                                                 OnPush=BEGIN
                                                          FILTERGROUP := 2;
                                                          IF HASFILTER THEN
                                                            BEGIN
                                                              RESET;
                                                              CurrForm.btnFile.VISIBLE := TRUE;
                                                              CurrForm.BtnObject.VISIBLE := FALSE;
                                                            END
                                                          ELSE
                                                           BEGIN
                                                              SETRANGE(Nesting,0);
                                                              SETFILTER("Line Text",'<>%1&<>%2&<>%3','','{','}');
                                                              CurrForm.btnFile.VISIBLE := TRUE;
                                                              CurrForm.BtnObject.VISIBLE := TRUE;
                                                           END;
                                                          FILTERGROUP := 0;
                                                        END;
                                                         }
  }
  CODE
  {
    VAR
      CALCode@1000 : Codeunit 50000;
      SDialog@1001 : Codeunit 412;
      frmObj@1002 : Form 50000;
      recObj@1003 : Record 50000;
      Mode@1004 : 'Object,Source';

    PROCEDURE SetMode@1(pMode@1000 : 'Object,Source');
    BEGIN
      Mode := pMode;
    END;

    BEGIN
    END.
  }
}

OBJECT Codeunit 50000 C/AL Code
{
  OBJECT-PROPERTIES
  {
    Date=28-11-07;
    Time=11:58:27;
    Modified=Yes;
    Version List=Research;
  }
  PROPERTIES
  {
    OnRun=BEGIN
          END;

  }
  CODE
  {
    VAR
      recObj@1000 : Record 50000;
      Window@1001 : Dialog;

    PROCEDURE Import@1(Filename@1000 : Text[250]);
    VAR
      fObj@1001 : File;
      strLine@1002 : Text[1024];
      i@1003 : Integer;
      j@1004 : Integer;
      outs@1005 : OutStream;
      k@1006 : Integer;
      strObjectName@1060000 : Text[250];
      ins@1060001 : InStream;
    BEGIN
      IF Filename <> '' THEN
       WITH recObj DO BEGIN
         RESET;
         IF FIND('-') THEN
          DELETEALL;
        Window.OPEN('Progress@1@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
        fObj.TEXTMODE := TRUE;
        IF fObj.OPEN(Filename) THEN BEGIN
          fObj.CREATEINSTREAM(ins);
          WHILE fObj.POS <> fObj.LEN DO BEGIN
            Window.UPDATE(1,ROUND(10000*(fObj.POS/fObj.LEN),1));
            ins.READTEXT(strLine,1024);
            INIT;
            "Line No." := i + 1;
            "Line Text" := COPYSTR(strLine,1,250);

            "Line Blob".CREATEOUTSTREAM(outs);
            outs.WRITETEXT(strLine);

            Nesting := j;
            IF STRLEN(strLine) > 0 THEN
             FOR k := 1 TO STRLEN(strLine) DO BEGIN
              CASE strLine[k] OF
                '{': BEGIN Nesting := j; j += 1; END;
                '}': BEGIN j -= 1; Nesting := j; END;
              END;
             END;

            IF (Nesting = 0) AND NOT ("Line Text" IN ['{','}','']) THEN
              strObjectName := COPYSTR("Line Text",8);

            "Object Name" := strObjectName;

            IF fObj.POS <> fObj.LEN THEN
              INSERT;

            i += 1;
          END;
          fObj.CLOSE;
          Window.CLOSE;
        END;
       END;
    END;

    PROCEDURE Export@2(Filename@1000 : Text[250];VAR pRecObj@1003 : Record 50000);
    VAR
      fObj@1001 : File;
      ins@1002 : InStream;
      outs@1004 : OutStream;
      NewLine@1007 : Text[2];
      flag@1006 : Boolean;
    BEGIN
      IF Filename <> '' THEN
        WITH pRecObj DO BEGIN
          NewLine[1] := 13;
          NewLine[2] := 10;
          fObj.WRITEMODE := TRUE;
          fObj.CREATE(Filename);
          fObj.CREATEOUTSTREAM(outs);
          IF FIND('-') THEN
            REPEAT
              flag := FALSE;
              recObj.SETFILTER("Line No.",'>=%1',"Line No.");
              IF recObj.FIND('-') THEN
                REPEAT
                  recObj.CALCFIELDS("Line Blob");
                  IF recObj."Line Blob".HASVALUE THEN BEGIN
                    recObj."Line Blob".CREATEINSTREAM(ins);
                    COPYSTREAM(outs,ins);
                  END;
                  outs.WRITETEXT(NewLine);
                  IF (recObj."Line Text" = '}') AND (recObj.Nesting = 0) THEN
                    flag := TRUE;
                UNTIL (recObj.NEXT = 0) OR flag;
              outs.WRITETEXT(NewLine);
            UNTIL NEXT = 0;
          fObj.CLOSE;
        END;
    END;

    PROCEDURE LTrim@3(str@1000 : Text[1024]) : Text[1024];
    BEGIN
      WHILE (STRLEN(str) > 0) AND (str[1] = ' ') DO
        str := DELSTR(str,1,1);
      EXIT(str);
    END;

    PROCEDURE More@4(Sym@1000 : Char;Count@1001 : Integer) Result : Text[1024];
    BEGIN
      WHILE Count > 0 DO BEGIN
        Result := Result + FORMAT(Sym);
        Count -= 1;
      END;
    END;

    BEGIN
    END.
  }
}

Sign In or Register to comment.