Author Topic: Development of a program for automatic search for music and movies  (Read 18097 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Development of a program for automatic search for music and movies
« Reply #45 on: June 26, 2020, 06:22:03 pm »
Yes! I was trying to get that from something like: _SHELLHIDE "Tree / >Tree.txt"
but no file was getting created. My DOS command lines is rusty, maybe some CHR$(34)'s were needed?

I forgot that you didn't get full paths on each line.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Development of a program for automatic search for music and movies
« Reply #46 on: June 26, 2020, 06:46:28 pm »
@bplus; @Petr:

Repeat after me: "I'm a big dummy.  I'm a big dummy.  I'm a big dummy."   Repeat for the next several minutes, then read WHY we're all such failures with such simple code -- yours truly included....


C strings are NOT the same as QB64 strings!!!

Everyone knows this, and yet, we all let it bite us in the ass, in this case!

Let's take for example a simple call to our function like so:

Code: [Select]
cd$ = _CWD$ 'testing in QB64 folder
MakeTree cd$

This fails to perform as expected.  (In some ways, I'm surprised to find that it performs at all!)

WHY??

Because _CWD$ returns a QB64 formatted string, and *NOT* a string as we'd normally expect to find in C.  It's *not* null terminated!!

Maketree cd$ + CHR$(0) is probably what we need to get it to work, in all honesty!  At least, playing around with my own little recursive routine, it's what was needed to (mostly) fix things.  My problem now is that I'm capturing too many recursions, and rechecking everything over and over endlessly...  The exit to my loop is broken somewhere.  :P

Anyway, I'd think if you want to sort out the bugs in your routines, you might want to null terminate your strings, and then a lot of the issues might end up going away for you guys.  It's worth a shot anyways.  I'm going to keep playing around with my own little thing, and I'll keep you updated with what I come up with as I progress.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Development of a program for automatic search for music and movies
« Reply #47 on: June 26, 2020, 06:58:12 pm »
See if this works as expected for getting the directories:

Code: QB64: [Select]
  1. OPTION _EXPLICIT 'Tree builder recursive.bas  b+ Petr 2020-06-26
  2. DEFLNG A-Z
  3.     FUNCTION load_dir& (s AS STRING)
  4.     FUNCTION has_next_entry& ()
  5.     SUB close_dir ()
  6.     SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
  7.  
  8.  
  9. SCREEN _NEWIMAGE(1280, 740, 32)
  10. _DELAY .25
  11. DIM cd$, i, w$
  12.  
  13.  
  14. MakeTree _CWD$ 'testing in QB64 folder
  15.  
  16. PRINT "Showing Tree"
  17. FOR i = 1 TO UBOUND(Tree) 'show tree
  18.     PRINT _TRIM$(STR$(i)); ": "; Tree(i)
  19.     IF i MOD 40 = 0 THEN INPUT "Press enter to continue... "; w$: CLS
  20.  
  21.  
  22. SUB GetSubDirs (SearchDirectory AS STRING)
  23.     CONST IS_DIR = 1
  24.     DIM flags AS LONG, file_size AS LONG, length, nam$
  25.     IF load_dir(SearchDirectory + CHR$(0)) THEN
  26.         DO
  27.             length = has_next_entry
  28.             IF length > -1 THEN
  29.                 nam$ = SPACE$(length)
  30.                 get_next_entry nam$, flags, file_size
  31.                 IF RIGHT$(nam$, 1) <> "." AND RIGHT$(nam$, 2) <> ".." THEN
  32.                     IF flags = IS_DIR OR _DIREXISTS(SearchDirectory + "\" + nam$) THEN
  33.                         REDIM _PRESERVE Tree(UBOUND(Tree) + 1)
  34.                         Tree(UBOUND(Tree)) = SearchDirectory + "\" + nam$
  35.                     END IF
  36.                 END IF
  37.             END IF
  38.         LOOP UNTIL length = -1
  39.     ELSE
  40.         PRINT "Dir not loaded"
  41.     END IF
  42.     close_dir
  43.  
  44. SUB MakeTree (Dir$)
  45.     DIM OnDir AS LONG
  46.     REDIM Tree(0) AS STRING
  47.     Tree(0) = Dir$
  48.     DO
  49.         GetSubDirs Tree(OnDir)
  50.         OnDir = OnDir + 1
  51.     LOOP UNTIL OnDir > UBOUND(Tree)
  52.  

Biggest and most important change??

As I pointed out above -- null terminate your strings:     IF load_dir(SearchDirectory + CHR$(0)) THEN


NOTE: This change should probably carry over to ANY routine which uses load_dir with direntry.h.  I think we've all been goofing on calling the function all this time, and I'm surprised nobody has noticed or found the glitch before now.  Multiple, repetitive calls, all in a row, is a good way to stress test these routines, I guess.  :D

Anywho....  Test it out, see if it looks and acts about like it should, and we'll debug and sort out any issues with things as we go.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Development of a program for automatic search for music and movies
« Reply #48 on: June 26, 2020, 07:42:54 pm »
Bravo! Steve! that did the trick alright.

Curiously, I get one more Directory with your code, that I like because you eliminated the need to CHDIR.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #49 on: June 26, 2020, 10:10:11 pm »
I forgot that you didn't get full paths on each line.
It doesn't matter because since you have the formatting of the tree, you always know what the path is. You would just have to write a function to parse that tree.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #50 on: June 26, 2020, 10:18:06 pm »
See if this works as expected for getting the directories:
@SMcNeill I'm thinking the tree structure isn't correct, Steve. Or maybe this is intentional but see my screenshot from running your code:
 
expected behavior question mark.png

Shouldn't all the folders in that auto\ folder be together and not separated out? Same for include\
Something seems to be amiss in the sorting, perhaps.
It appears to do that in several spots from what I can see. I would have assumed that the tree structure puts all subdirectories of a folder with the parent folder and then move on to the next folder like my version:
 
my version.png
« Last Edit: June 26, 2020, 10:24:53 pm by SpriggsySpriggs »
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Development of a program for automatic search for music and movies
« Reply #51 on: June 26, 2020, 11:17:08 pm »
Easy way to test that Spriggsy:  run both routines, sort them, and then compare line by line entries to see what's different (if anything).  To be honest, I'm not certain why there's jumps in the output like that either, yet.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #52 on: June 26, 2020, 11:20:27 pm »
Easy way to test that Spriggsy:  run both routines, sort them, and then compare line by line entries to see what's different (if anything).  To be honest, I'm not certain why there's jumps in the output like that either, yet.  ;)
Both programs give me the same number of directories. It's just a matter of the structure.
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Development of a program for automatic search for music and movies
« Reply #53 on: June 26, 2020, 11:42:43 pm »
Both programs give me the same number of directories. It's just a matter of the structure.

It's just the sorting method.  I'm thinking, if you look close, my little routine finds the directories in levels deep that it goes.

....\auto\core is 14 levels deep.
....\build\vc10 is 14 levels deep.
....\auto\core\gl is 15 levels deep.

As long as it finds all the directories properly, they can be sorted and arraigned as desired later.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Development of a program for automatic search for music and movies
« Reply #54 on: June 27, 2020, 12:12:56 am »
Well the disadvantage of not using CHDIR is having to sort later?

This looks to be in pretty good order:
Code: QB64: [Select]
  1. 'Tree builder recursive.bas  b+ Petr 2020-06-26  now with SMCNeill Fix!! add CHR$(0) to C string
  2.     FUNCTION load_dir& (s AS STRING)
  3.     FUNCTION has_next_entry& ()
  4.     SUB close_dir ()
  5.     SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
  6.  
  7. REDIM SHARED Tree(0) AS STRING '<<<<<<<<<<<<<<<<<<<<<< G(0) will remain empty
  8.  
  9. MakeTree _CWD$
  10. FOR i = 1 TO UBOUND(Tree) 'show tree
  11.     PRINT _TRIM$(STR$(i)); ": "; Tree(i)
  12.  
  13. SUB sAppend (arr() AS STRING, addItem$)
  14.     REDIM _PRESERVE arr(LBOUND(arr) TO UBOUND(arr) + 1) AS STRING
  15.     arr(UBOUND(arr)) = addItem$
  16.  
  17. SUB GetSubDirs (SearchDirectory AS STRING, DirList() AS STRING)
  18.     CONST IS_DIR = 1
  19.     DIM flags AS LONG, file_size AS LONG
  20.     IF load_dir(SearchDirectory + CHR$(0)) THEN 'Steve's fix here with CHR$(0) for C call
  21.         DO
  22.             length = has_next_entry
  23.             IF length > -1 THEN
  24.                 nam$ = SPACE$(length)
  25.                 get_next_entry nam$, flags, file_size
  26.                 IF (flags AND IS_DIR) OR _DIREXISTS(SearchDirectory + nam$) THEN
  27.                     IF nam$ <> "." AND nam$ <> ".." THEN
  28.                         DirCount = DirCount + 1
  29.                         IF DirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(UBOUND(DirList) + 100)
  30.                         DirList(DirCount) = SearchDirectory + "\" + nam$
  31.                     END IF
  32.                 END IF
  33.             END IF
  34.         LOOP UNTIL length = -1
  35.     END IF
  36.     close_dir 'Steve first fix that got navigator working
  37.     REDIM _PRESERVE DirList(DirCount)
  38.  
  39. SUB MakeTree (startDir AS STRING)
  40.     REDIM D(100) AS STRING
  41.     GetSubDirs startDir, D()
  42.     FOR i = 1 TO UBOUND(D)
  43.         sAppend Tree(), D(i)
  44.         CHDIR D(i)
  45.         MakeTree D(i)
  46.     NEXT
  47.  
  48.  

 
order of out put.PNG
« Last Edit: June 27, 2020, 12:14:39 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #55 on: June 27, 2020, 12:18:34 am »
As long as it finds all the directories properly, they can be sorted and arraigned as desired later.  ;)
Your logic is showing a file in the QB64 directory as a folder but is just a file without an extension.

\internal\c\c_compiler\licenses\libffi\LICENSE

I can run a sort on your file using a PowerShell command and the entire program takes around 6 seconds to complete from getting the directories to spitting out the sorted version.
If I run my program using the PowerShell version, it only takes 3 seconds because it is already sorted. I verified this by using the PowerShell script to sort my file and then compared it in Notepad ++. The files were identical.
EDIT: It looks like @bplus has solved the issue with using direntry
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Development of a program for automatic search for music and movies
« Reply #56 on: June 27, 2020, 12:22:40 am »
I am impressed that Steve managed it without a recursive procedure.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #57 on: June 27, 2020, 12:28:37 am »
@bplus I did a test to see which way was faster and it looks like your code and mine are quite close. Mine finished in 3.380 seconds whereas yours took 3.626 seconds. I also compared the files in Notepad++ and they were identical. Way to go! EDIT: I ran it again using only _TRIM$ on the value from the file and mine finished in 3.109 seconds. Only a half second's difference is quite impressive! I thought for sure that mine would lose against yours.
See screenshot:
 
bplus spriggsy speed comparison.png

 
slightly faster again.png
« Last Edit: June 27, 2020, 12:34:44 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Development of a program for automatic search for music and movies
« Reply #58 on: June 27, 2020, 12:39:24 am »
Quote
I thought for sure that mine would lose against yours.

Ha! mine was probably slowed down by all the CHDIR's. Your's is working under the Power of Power Shell ;-))

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Development of a program for automatic search for music and movies
« Reply #59 on: June 27, 2020, 12:42:19 am »
Ha! mine was probably slowed down by all the CHDIR's. Your's is working under the Power of Power Shell ;-))
I expected that with shelling out to PowerShell that I would lose precious time because I'm having to wait on an external process to complete. I know that when I generate my WPF Message Boxes from PowerShell they are much slower than calling a native message box (of course, mine are more graphical and the script is quite long. Eh, whatever)
Shuwatch!