2019-08-14 23:24:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2019-08-14 20:29:04 +01:00
|
|
|
#include "../gfx/di.h"
|
|
|
|
#include "../gfx/gfx.h"
|
|
|
|
#include "../utils/btn.h"
|
|
|
|
#include "../utils/util.h"
|
|
|
|
#include "utils.h"
|
2019-08-14 23:24:58 +01:00
|
|
|
#include "../libs/fatfs/ff.h"
|
|
|
|
#include "../storage/sdmmc.h"
|
2019-08-16 22:57:54 +01:00
|
|
|
#include "graphics.h"
|
2019-08-14 23:24:58 +01:00
|
|
|
|
2019-08-17 00:08:00 +01:00
|
|
|
|
2019-08-14 20:29:04 +01:00
|
|
|
void utils_gfx_init(){
|
|
|
|
display_backlight_brightness(100, 1000);
|
|
|
|
gfx_clear_grey(0x1B);
|
|
|
|
gfx_con_setpos(0, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:04:35 +01:00
|
|
|
void removepartpath(char *path, char *root){
|
2019-08-15 21:16:48 +01:00
|
|
|
char *ret;
|
2019-08-19 14:04:35 +01:00
|
|
|
char temproot[20];
|
2019-08-15 21:16:48 +01:00
|
|
|
ret = strrchr(path, '/');
|
|
|
|
memset(ret, '\0', 1);
|
2019-08-19 14:04:35 +01:00
|
|
|
sprintf(temproot, "%s%s", root, "/");
|
|
|
|
if (strcmp(path, root) == 0) strcpy(path, temproot);
|
2019-08-15 21:16:48 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 14:04:35 +01:00
|
|
|
void addpartpath(char *path, char *add, char *root){
|
|
|
|
if (strcmp(path, root) != 0) strcat(path, "/");
|
2019-08-15 21:16:48 +01:00
|
|
|
strcat(path, add);
|
|
|
|
}
|
|
|
|
|
2019-08-17 00:08:00 +01:00
|
|
|
void return_readable_byte_amounts(unsigned long int size, char *in){
|
2019-08-16 20:40:04 +01:00
|
|
|
char type[3];
|
2019-08-16 22:57:54 +01:00
|
|
|
unsigned long int sizetemp = size;
|
2019-08-16 20:40:04 +01:00
|
|
|
int muhbytes = 0;
|
|
|
|
while(sizetemp > 1024){
|
|
|
|
muhbytes++;
|
|
|
|
sizetemp = sizetemp / 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(muhbytes){
|
|
|
|
case 0:
|
|
|
|
strcpy(type, "B");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
strcpy(type, "KB");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
strcpy(type, "MB");
|
|
|
|
break;
|
2019-08-16 22:57:54 +01:00
|
|
|
case 3:
|
|
|
|
strcpy(type, "GB");
|
|
|
|
break;
|
2019-08-16 20:40:04 +01:00
|
|
|
default:
|
|
|
|
strcpy(type, "GB");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sprintf(in, "%d%s", sizetemp, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getfilesize(const char *path){
|
|
|
|
FILINFO fno;
|
|
|
|
f_stat(path, &fno);
|
|
|
|
return fno.fsize;
|
|
|
|
}
|
|
|
|
|
2019-08-16 15:13:58 +01:00
|
|
|
void addchartoarray(char *add, char *items[], int spot){
|
2019-08-15 21:16:48 +01:00
|
|
|
size_t size = strlen(add) + 1;
|
|
|
|
items[spot] = (char*) malloc (size);
|
|
|
|
strlcpy(items[spot], add, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _mallocandaddfolderbit(unsigned int *muhbits, int spot, bool value){
|
|
|
|
muhbits[spot] = (unsigned int) malloc (sizeof(int));
|
|
|
|
if (value) muhbits[spot] |= (OPTION1);
|
2019-08-16 15:13:58 +01:00
|
|
|
//ff.h line 368
|
2019-08-15 21:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int readfolder(char *items[], unsigned int *muhbits, const char *path){
|
2019-08-14 23:24:58 +01:00
|
|
|
DIR dir;
|
|
|
|
FILINFO fno;
|
2019-08-15 21:16:48 +01:00
|
|
|
int i = 2;
|
2019-08-16 17:40:09 +01:00
|
|
|
addchartoarray("Current folder -> One folder up", items, 0);
|
2019-08-16 17:47:28 +01:00
|
|
|
addchartoarray("Clipboard -> Current folder", items, 1);
|
2019-08-15 21:16:48 +01:00
|
|
|
_mallocandaddfolderbit(muhbits, 0, true);
|
|
|
|
_mallocandaddfolderbit(muhbits, 1, true);
|
2019-08-14 23:24:58 +01:00
|
|
|
|
2019-08-19 22:01:47 +01:00
|
|
|
|
2019-08-14 23:24:58 +01:00
|
|
|
if (f_opendir(&dir, path)) {
|
|
|
|
gfx_printf("\nFailed to open %s", path);
|
2019-08-19 22:01:47 +01:00
|
|
|
return -1;
|
2019-08-14 23:24:58 +01:00
|
|
|
}
|
2019-08-15 21:16:48 +01:00
|
|
|
else {
|
2019-08-14 23:42:41 +01:00
|
|
|
while (!f_readdir(&dir, &fno) && fno.fname[0]){
|
2019-08-16 15:13:58 +01:00
|
|
|
addchartoarray(fno.fname, items, i);
|
2019-08-15 21:16:48 +01:00
|
|
|
_mallocandaddfolderbit(muhbits, i, fno.fattrib & AM_DIR);
|
2019-08-14 23:42:41 +01:00
|
|
|
i++;
|
|
|
|
}
|
2019-08-14 23:24:58 +01:00
|
|
|
}
|
2019-08-15 21:16:48 +01:00
|
|
|
f_closedir(&dir);
|
2019-08-14 23:24:58 +01:00
|
|
|
return i;
|
2019-08-16 15:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int copy(const char *src, const char *dst){
|
|
|
|
FIL in;
|
|
|
|
FIL out;
|
2019-08-16 22:57:54 +01:00
|
|
|
if (strcmp(src, dst) == 0){
|
|
|
|
//in and out are the same, aborting!
|
|
|
|
return 2;
|
|
|
|
}
|
2019-08-16 15:13:58 +01:00
|
|
|
if (f_open(&in, src, FA_READ) != FR_OK){
|
|
|
|
//something has gone wrong
|
2019-08-16 22:57:54 +01:00
|
|
|
return 1;
|
2019-08-16 15:13:58 +01:00
|
|
|
}
|
|
|
|
if (f_open(&out, dst, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK){
|
|
|
|
//something has gone wrong
|
2019-08-16 22:57:54 +01:00
|
|
|
return 1;
|
2019-08-16 15:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int BUFFSIZ = 32768;
|
|
|
|
u64 size = f_size(&in);
|
2019-08-16 22:57:54 +01:00
|
|
|
unsigned long totalsize = size, kbwritten = 0;
|
2019-08-16 15:13:58 +01:00
|
|
|
void *buff = malloc(BUFFSIZ);
|
2019-08-16 22:57:54 +01:00
|
|
|
int mbwritten = 0, percentage = 0;
|
|
|
|
bool abort = false;
|
|
|
|
meme_clearscreen();
|
|
|
|
gfx_printf("press VOL- to abort the file transfer!\n\n");
|
2019-08-16 15:13:58 +01:00
|
|
|
while(size > BUFFSIZ){
|
|
|
|
int res1, res2;
|
|
|
|
res1 = f_read(&in, buff, BUFFSIZ, NULL);
|
|
|
|
res2 = f_write(&out, buff, BUFFSIZ, NULL);
|
|
|
|
|
2019-08-16 22:57:54 +01:00
|
|
|
kbwritten = kbwritten + (BUFFSIZ / 1024);
|
|
|
|
mbwritten = kbwritten / 1024;
|
2019-08-19 14:04:35 +01:00
|
|
|
percentage = (mbwritten * 100) / ((totalsize / 1024) / 1024);
|
2019-08-16 15:13:58 +01:00
|
|
|
|
2019-08-16 22:57:54 +01:00
|
|
|
gfx_printf("Written %dMB [%k%d%k%%]\r", mbwritten, COLOR_GREEN, percentage, COLOR_WHITE);
|
2019-08-16 15:13:58 +01:00
|
|
|
size = size - BUFFSIZ;
|
2019-08-16 22:57:54 +01:00
|
|
|
if (btn_read() & BTN_VOL_DOWN) size = 0, abort = true;
|
2019-08-16 15:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(size != 0){
|
|
|
|
f_read(&in, buff, size, NULL);
|
|
|
|
f_write(&out, buff, size, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
f_close(&in);
|
|
|
|
f_close(&out);
|
|
|
|
|
2019-08-16 22:57:54 +01:00
|
|
|
if(abort){
|
|
|
|
f_unlink(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-16 15:13:58 +01:00
|
|
|
free(buff);
|
|
|
|
|
2019-08-16 22:57:54 +01:00
|
|
|
return 0;
|
2019-08-16 15:13:58 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 22:01:47 +01:00
|
|
|
int copywithpath(const char *src, const char *dstpath, int mode, char *app){
|
2019-08-16 15:13:58 +01:00
|
|
|
FILINFO fno;
|
|
|
|
f_stat(src, &fno);
|
2019-08-16 20:40:04 +01:00
|
|
|
char dst[PATHSIZE];
|
2019-08-16 15:13:58 +01:00
|
|
|
strcpy(dst, dstpath);
|
2019-08-19 22:01:47 +01:00
|
|
|
if (strcmp(dstpath, app) != 0) strcat(dst, "/");
|
2019-08-16 15:13:58 +01:00
|
|
|
strcat(dst, fno.fname);
|
|
|
|
int ret = -1;
|
|
|
|
if (mode == 0) ret = copy(src, dst);
|
|
|
|
if (mode == 1) f_rename(src, dst);
|
|
|
|
return ret;
|
2019-08-14 20:29:04 +01:00
|
|
|
}
|