Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.85 KB | None | 0 0
  1.  
  2. #include <dryos.h>
  3. #include <module.h>
  4. #include <bmp.h>
  5. #include <menu.h>
  6.  
  7. #define MODULES_DIR "B:/ML/MODULES"
  8.  
  9. /* download url to the file specified */
  10. int32_t net_get_file(char *url, char *dst_file);
  11. int32_t net_get_data(char *url, void (*cbr)(void *ctx, char *data, uint32_t length));
  12.  
  13.  
  14. /* this is not perfect, as .Mo and .mO aren't detected. important? */
  15. static int module_valid_filename(char* filename)
  16. {
  17.     int n = strlen(filename);
  18.     if ((n > 3) && (streq(filename + n - 3, ".MO") || streq(filename + n - 3, ".mo")) && (filename[0] != '.') && (filename[0] != '_'))
  19.         return 1;
  20.     return 0;
  21. }
  22.  
  23. static const char * format_date(unsigned timestamp )
  24. {
  25.     static char datestr[17];
  26.     int year=1970;                   // Unix Epoc begins 1970-01-01
  27.     int month=11;                    // This will be the returned MONTH NUMBER.
  28.     int day;                         // This will be the returned day number.
  29.     int hour;                        
  30.     int minute;                      
  31.     int second;                      
  32.     int dayInSeconds=86400;          // 60secs*60mins*24hours
  33.     int daysInYear=365;              // Non Leap Year
  34.     int daysInLYear=daysInYear+1;    // Leap year
  35.     int days=timestamp/dayInSeconds; // Days passed since UNIX Epoc
  36.     int sec_of_day=timestamp % dayInSeconds;
  37.     int tmpDays=days+1;              // If passed (timestamp < dayInSeconds), it will return 0, so add 1
  38.  
  39.     while(tmpDays>=daysInYear)       // Start adding years to 1970
  40.     {      
  41.         year++;
  42.         if ((year)%4==0&&((year)%100!=0||(year)%400==0)) tmpDays-=daysInLYear; else tmpDays-=daysInYear;
  43.     }
  44.  
  45.     int monthsInDays[12] = {-1,30,59,90,120,151,181,212,243,273,304,334};
  46.     if (!(year)%4==0&&((year)%100!=0||(year)%400==0))  // The year is not a leap year
  47.     {
  48.         monthsInDays[0] = 0;
  49.         monthsInDays[1] =31;
  50.     }
  51.  
  52.     while (month>0)
  53.     {
  54.         if (tmpDays>monthsInDays[month]) break;       // month+1 is now the month number.
  55.         month--;
  56.     }
  57.     day=tmpDays-monthsInDays[month];                  // Setup the date
  58.     month++;                                          // Increment by one to give the accurate month
  59.     if (day==0) {year--; month=12; day=31;}           // Ugly hack but it works, eg. 1971.01.00 -> 1970.12.31
  60.    
  61.     hour = (sec_of_day / (60 * 60)) % 24;
  62.     minute = (sec_of_day / 60) % 60;
  63.     second = sec_of_day % 60;
  64.    
  65.     snprintf(datestr, sizeof(datestr), "%04d%02d%02d_%02d%02d%02d", year, month, day, hour, minute, second);
  66.  
  67.     return datestr;
  68. }
  69.  
  70. uint32_t date_cbr_done = 0;
  71. char *date_cbr_data = 0;
  72. uint32_t date_cbr_length = 0;
  73. void modmgr_date_cbr(void *ctx, char *buf, uint32_t length)
  74. {
  75.     date_cbr_data = buf;
  76.     date_cbr_length = length;
  77.     date_cbr_done = 1;
  78. }
  79.  
  80. static void modmgr_task()
  81. {
  82.     console_show();
  83.     beep();
  84.     msleep(1000);
  85.    
  86.     printf("Scanning modules...\n");
  87.    
  88.     struct fio_file file;
  89.     struct fio_dirent * dirent = FIO_FindFirstEx(MODULES_DIR, &file );
  90.     if( IS_ERROR(dirent) )
  91.     {
  92.         NotifyBox(2000, "Module dir missing" );
  93.         return;
  94.     }
  95.  
  96.     do
  97.     {
  98.         if (file.mode & ATTR_DIRECTORY) continue; // is a directory
  99.         if (module_valid_filename(file.name))
  100.         {
  101.             printf(" File '%s'\n", file.name);
  102.             printf("   Date '%s'\n", format_date(file.timestamp));
  103.        
  104.             /* make a lower case filename */
  105.             char lower[64];
  106.             strcpy(lower, file.name);
  107.             for(int pos = 0; pos < strlen(lower); pos++)
  108.             {
  109.                 if(lower[pos] >= 'A' && lower[pos] <= 'Z')
  110.                 {
  111.                     lower[pos] += 0x20;
  112.                 }
  113.             }
  114.            
  115.             char url[64];
  116.             snprintf(url, sizeof(url), "http://ml.g3gg0.de/modules/%s/latest_date", lower);
  117.             printf("   Fetch '%s'\n", url);
  118.            
  119.             date_cbr_done = 0;
  120.             net_get_data(url, modmgr_date_cbr);
  121.             while(!date_cbr_done)
  122.             {
  123.                 msleep(50);
  124.             }
  125.            
  126.             if(date_cbr_length)
  127.             {
  128.                 printf("   Curr '%s'\n", date_cbr_data);
  129.                 if(!strcmp(date_cbr_data, format_date(file.timestamp)))
  130.                 {
  131.                     printf("   --- is latest ---\n");
  132.                 }
  133.                 else
  134.                 {
  135.                     printf("   === NEED UPDATE ===\n");
  136.                    
  137.                     char dl_url[64];
  138.                     char dl_filename[64];
  139.                     snprintf(dl_url, sizeof(dl_url), "http://ml.g3gg0.de/modules/%s/%s", lower, lower);
  140.                     snprintf(dl_filename, sizeof(dl_filename), MODULES_DIR"/%s", file.name);
  141.                     printf("   Fetch '%s'\n", dl_url);
  142.                     net_get_file(dl_url, dl_filename);
  143.                    
  144.                     printf("   ===== UPDATED =====\n");
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 printf("   Curr FAILED (no such module?)\n");
  150.             }
  151.         }
  152.     } while( FIO_FindNextEx( dirent, &file ) == 0);
  153.     FIO_FindClose(dirent);
  154. }
  155.  
  156.  
  157. static struct menu_entry modmgr_menu[] =
  158. {
  159.     {
  160.         .name = "Module Manager",
  161.         .help = "",
  162.         .submenu_width = 710,
  163.         .children = (struct menu_entry[])
  164.         {
  165.             {
  166.                 .name = "Check modules",
  167.                 .priv = modmgr_task,
  168.                 .select = run_in_separate_task,
  169.             },
  170.             MENU_EOL,
  171.         },
  172.     },
  173. };
  174.  
  175.  
  176. static unsigned int modmgr_init()
  177. {
  178.     /* still need to find a suitable menu */
  179.     menu_add("Audio", modmgr_menu, COUNT(modmgr_menu));
  180.     return 0;
  181. }
  182.  
  183. static unsigned int modmgr_deinit()
  184. {
  185.     return 0;
  186. }
  187.  
  188. MODULE_INFO_START()
  189.     MODULE_INIT(modmgr_init)
  190.     MODULE_DEINIT(modmgr_deinit)
  191. MODULE_INFO_END()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement