Nested TYPEs

Previous topic - Next topic

ampos

In my next proyect, I need a file list of 3rd depth :)

1-Drive
2-Folder
3-File

Each list can have any number of elements, and have various values, so I am going to use types. Each "list" (child?) belong only to his previous parent.

But I have never used nested types (types inside types), so I have a few doubts

-Can a type be defined inside a type that belongs to another type?
   a[1].b[4].c[6], for example?

-Should I define the types in inverse order?

type a3:
   name
   path$
   id
endtype

type a2:
   name
   id
   files[] as a3
endtype

type a1:
   name
   id
   folders[] as a2
endtype

-How do I get some element in the 3rd level?
  print drive[n].folder[m].file
  • .name?

    -How do I do a foreach...next loop for the bottom child?
       foreach n in drive[10].folder[3].file[]?





Kitty Hello

yes.

Code (glbasic) Select

TYPE Ta
a
ENDTYPE
TYPE Tb
a[] as Ta
ENDTYPE
TYPE Tc
b[] as Tb
ENDTYPE

LOCAL cc as Tc
FOREACH ib IN cc.b[]
   FOREACH ia IN ib.a[]
      ia.a = 5
   NEXT
NEXT




kanonet

#2
There is only one thing, that is impossible:

Code (glbasic) Select
TYPE Ta
a[] AS Ta
ENDTYPE

Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

Yes, recursion isn't allowed...

Slydog

#4
You could simplify this (or make it more complicated? depends on your view!) by having only one TYPE, plus utility functions to search that TYPE, such as:

Code (glbasic) Select
CONSTANT FILE_TYPE_DRIVE% = 1
CONSTANT FILE_TYPE_FOLDER% = 2
CONSTANT FILE_TYPE_FILE% = 3

TYPE TFile
  id%           // unique id for this entry
  id_parent%    // id of this entry's parent
  id_type%      // file type of this entry
  value$        // string value of this entry
ENDTYPE
GLOBAL files[] AS TFile


Then for example, the follow files and folders:
Code (glbasic) Select
C:\
C:\Data\
C:\Data\settings.ini
C:\Data\GLBasic\
C:\Data\GLBasic\game.gbap
C:\Data\GLBasic\main.gbas
C:\Data\GLBasic\Help\


. .. would create the following array entries:
Code (glbasic) Select
//id, id_parent, id_type, value$
1, -1, FILE_TYPE_DRIVE, "C"            // C:\
2,  1, FILE_TYPE_FOLDER, "Data"        // C:\Data\
3,  2, FILE_TYPE_FILE, "settings.ini"  // C:\Data\settings.ini
4,  2, FILE_TYPE_FOLDER, "GLBasic"     // C:\Data\GLBasic\
5,  4, FILE_TYPE_FILE, "game.gbap"     // C:\Data\GLBasic\game.gbap
6,  4, FILE_TYPE_FILE, "main.gbas"     // C:\Data\GLBasic\main.gbas
7,  4, FILE_TYPE_FOLDER, "Help"        // C:\Data\GLBasic\Help\


Then create functions to peruse that array:
Code (glbasic) Select
FUNCTION File_FindChildren: id%, BYREF results%[]
  DIM results[0]
  FOREACH file IN files
    IF file.id_parent = id
      DIMPUSH results[], file.id
    ENDIF
  NEXT
ENDFUNCTION


To get a list of files and folders for "C:\Data\GLBasic\", use:
Code (glbasic) Select
LOCAL filelist%[]
File_FindChildren(4, filelist[])

FOREACH id_file IN filelist[]
  IF files[id_file].id_type = FILE_TYPE_FOLDER
    DEBUG "Folder: " + files[id_file].value$ + "\n"
  ELSE
    DEBUG "File: " + files[id_file].value$ + "\n"
  ENDIF
NEXT


I've never tested any of this, just typed in now into the forum message, so bound to be bugs.
You just need to create a 'File_FindFiles()' command to fill the TFile array.

[Edit] Oops, the 'find' function returns file id's, not index positions, so the example at the bottom won't work.  You could create a 'File_FindIndex()' that takes an id, and returns the index position.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

ampos

Slydog, my first though was something like this. It is easier to imagine but harder to follow, or something similar.

Note that I thought my program logics in bed, in the time between laying and falling sleep  :bed:, and without so little sleeping time, I fall real fast, and in the morning I tend to forgot everything  :whistle:

fuzzy70

To me the drive type is not required as technically its just a folder (the root folder). As I am a noob regarding GLBasic & have not yet fully worked out the way types & arrays work in this version of basic, would creating an array of the folders (using DIMPUSH to add extra folders to the array) & use types to store the files names with a ID pointing to the array work????

Like I said i'm a noob to this version of Basic & the above may not work but treating the drive as just another folder means 1 less type to create
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)