nitrodir.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. program nitrodir;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9, filesystem;
  5. procedure dirlist(path: pchar);
  6. var
  7. MyDir: PDIR;
  8. dnbuf: pchar;
  9. pent: pdirent;
  10. statbuf: PStat;
  11. begin
  12. GetMem(MyDir, sizeof(PDIR));
  13. GetMem(pent, sizeof(dirent));
  14. GetMem(statbuf, sizeof(PStat));
  15. MyDir := opendir(path);
  16. if (MyDir <> nil) then
  17. begin
  18. while true do
  19. begin
  20. pent := readdir(MyDir);
  21. if pent = nil then
  22. exit;
  23. if (strcmp('.', pent^.d_name) <> 0) and (strcmp('..', pent^.d_name) <> 0) then
  24. begin
  25. dnbuf := malloc(strlen(pent^.d_name) + strlen(path) + 2);
  26. if (strcmp('/',path) = 0) then
  27. sprintf(dnbuf, '%s/%s', '', pent^.d_name)
  28. else
  29. sprintf(dnbuf, '%s/%s', path, pent^.d_name);
  30. _stat(dnbuf, statbuf^);
  31. if (S_ISDIR(statbuf^.st_mode)) then
  32. begin
  33. printf('%s <DIR>'#10, dnbuf);
  34. dirlist(dnbuf);
  35. end else
  36. begin
  37. printf('%s (%d)'#10, dnbuf, statbuf^.st_size);
  38. end;
  39. free(dnbuf);
  40. free(statbuf);
  41. end;
  42. end;
  43. closedir(MyDir);
  44. end else
  45. begin
  46. printf('opendir() failure.'#10);
  47. end;
  48. end;
  49. var
  50. inf: P_File;
  51. len: cint;
  52. entireFile: pcchar;
  53. begin
  54. // Initialise the console, required for printf
  55. consoleDemoInit();
  56. if nitroFSInit(nil) then
  57. begin
  58. dirlist('/');
  59. begin
  60. // now, try reading a file to make sure things are working OK.
  61. inf := fopen('file1.txt','rb');
  62. if inf <> nil then
  63. begin
  64. fseek(inf, 0, SEEK_END);
  65. len := ftell(inf);
  66. fseek(inf, 0, SEEK_SET);
  67. iprintf(#10'the following %d bytes message'#10'from file1.txt is'#10'brought to you by fread:'#10, len);
  68. begin
  69. entireFile := pcchar(malloc(len+1));
  70. entireFile[len] := 0;
  71. if (fread(entireFile, 1, len, inf) <> len) then
  72. iprintf('savage error reading the bytes from the file!'#10)
  73. else
  74. iprintf('%s'#10'-done-'#10, entireFile);
  75. free(entireFile);
  76. end;
  77. fclose(inf);
  78. end;
  79. end;
  80. iprintf('here is the dirlist once more:'#10);
  81. dirlist('/');
  82. end else
  83. iprintf('nitroFSInit failure: terminating'#10);
  84. while true do
  85. swiWaitForVBlank();
  86. end.