Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <3ds.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/dirent.h>
  9.  
  10. #define MAX_LINE 10
  11. typedef struct {
  12.     char files[512][512];
  13.     int count;
  14.     int now;
  15. } picker_s;
  16.  
  17. picker_s *picker;
  18.  
  19. bool end_with(const char *str, const char c)
  20. {
  21.     return (str && *str && str[strlen(str) - 1] == c) ? true : false;
  22. }
  23.  
  24. void get_dir(const char *path)
  25. {
  26.     DIR *fd;
  27.     struct dirent *file;
  28.  
  29.     if (NULL == (fd = opendir (path)))
  30.     {
  31.         printf("\xb[3;1HDirectory empty...");
  32.         return;
  33.     }
  34.  
  35.     while ((file = readdir(fd)))
  36.     {
  37.         if (!strcmp (file->d_name, "."))
  38.             continue;
  39.         if (!strcmp (file->d_name, ".."))
  40.             continue;
  41.  
  42.         if(file->d_type != DT_DIR )
  43.         {
  44.             strncpy(picker->files[picker->count], file->d_name, 512);
  45.             picker->count++;
  46.         }
  47.     }
  48. }
  49.  
  50. void pick_file(char *picked, const char *path)
  51. {
  52.     picker = malloc(sizeof(picker_s));
  53.     memset(picker, 0, sizeof(picker_s));
  54.  
  55.     get_dir(path);
  56.     consoleClear();
  57.  
  58.     while (aptMainLoop())
  59.     {
  60.         hidScanInput();
  61.         u32 kDown = hidKeysDown();
  62.         if (kDown & KEY_B) break;
  63.  
  64.         if(kDown & KEY_DOWN)
  65.         {
  66.             picker->now++;
  67.             if(picker->now >= picker->count)
  68.                 picker->now = 0;
  69.             consoleClear();
  70.         }
  71.  
  72.         if(kDown & KEY_UP)
  73.         {
  74.             picker->now--;
  75.             if(picker->now < 0)
  76.                 picker->now = picker->count-1;
  77.             consoleClear();
  78.         }
  79.  
  80.         if(kDown & KEY_A)
  81.         {
  82.             if(end_with(path, '/'))
  83.                 snprintf(picked, 512, "%s%s", path, picker->files[picker->now]);
  84.             else
  85.                 snprintf(picked, 512, "%s/%s", path, picker->files[picker->now]);
  86.             break;
  87.         }
  88.  
  89.         int i;
  90.         for(i=0; i<MAX_LINE; i++)
  91.         {
  92.             int index = i+picker->now;
  93.             if(index >= picker->count) break;
  94.  
  95.             if(i==0)
  96.                 printf("\x1b[%i;1H\x1b[31m%s\x1b[0m", i+1, picker->files[index]);
  97.             else
  98.                 printf("\x1b[%i;1H%s\n", i+1, picker->files[index]);
  99.         }
  100.  
  101.         gfxFlushBuffers();
  102.         gfxSwapBuffers();
  103.         gspWaitForVBlank();
  104.     }
  105.    
  106.     free(picker);
  107.     consoleClear();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement