Manager overhaul
This commit is contained in:
parent
d580ff5d46
commit
a228e6866b
2 changed files with 36 additions and 84 deletions
|
@ -35,55 +35,8 @@ char** split_string(const char* str, char delimiter, int* count) {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Constructors
|
||||
Manager* manager_init() {
|
||||
// Creating result in memory
|
||||
Manager* result = malloc(sizeof(Manager));
|
||||
|
||||
// Storing password
|
||||
result->page = 0;
|
||||
|
||||
// Returning manager
|
||||
return result;
|
||||
}
|
||||
|
||||
// Functions
|
||||
void manager_switchpage(Manager* manager, int dir) {
|
||||
// Switching page
|
||||
manager->page += dir;
|
||||
|
||||
// Avoiding empty pages
|
||||
if (manager->page < 0) {manager->page = 0;}
|
||||
|
||||
// Loading names
|
||||
manager_loadnames(manager, manager->page);
|
||||
|
||||
// Too far?
|
||||
if (manager->names[0][0] == '\0') {
|
||||
manager_switchpage(manager, -1);
|
||||
}
|
||||
}
|
||||
void manager_loadnames(Manager* manager, int page){
|
||||
// DEBUG
|
||||
FURI_LOG_D(TAG, "Freeing previous names");
|
||||
|
||||
// Freeing all prior names
|
||||
for (int i = 0; i < 4; i++) {
|
||||
// Checking if this name exists:
|
||||
if (manager->names[i] != NULL) {
|
||||
// DEBUG
|
||||
FURI_LOG_D(TAG, "Freed %s", manager->names[i]);
|
||||
|
||||
// Freeing name
|
||||
free(manager->names[i]);
|
||||
} else {
|
||||
// DEBUG
|
||||
FURI_LOG_E(TAG, "Name %i does not exist!", i);
|
||||
}
|
||||
}
|
||||
|
||||
// Loading the next four passwords
|
||||
void manager_loadnames(Manager* manager){
|
||||
// Loading the names of all passwords
|
||||
char* data = store_load("Data");
|
||||
|
||||
// Splitting string
|
||||
|
@ -93,47 +46,48 @@ void manager_loadnames(Manager* manager, int page){
|
|||
// Freeing data
|
||||
free(data);
|
||||
|
||||
// Temporary variables
|
||||
int i_pass = 0;
|
||||
int i_name = 0;
|
||||
// Passwords
|
||||
/*
|
||||
Doing some risky bitshifting in order to divide by 4
|
||||
This little ARMv7 processor would hate division
|
||||
*/
|
||||
int num_of_passwords = count >> 2;
|
||||
|
||||
// Allocating memory for names array
|
||||
manager->names = malloc(num_of_passwords * sizeof(char*));
|
||||
|
||||
// Going through split string
|
||||
for (int i = 0; i < count; i++) {
|
||||
// Checking if it's the 4th number
|
||||
if (i % 4 == 0) {
|
||||
// It's another password!
|
||||
i_pass++;
|
||||
|
||||
// DEBUG
|
||||
FURI_LOG_I(TAG, "(%i) Scanning password %s...", i_pass, split[i]);
|
||||
|
||||
// Is it on our page?
|
||||
if (i_pass > page * 4
|
||||
&& i_pass <= (page * 4) + 4) {
|
||||
// Adding it to the array!
|
||||
manager->names[i_name] = split[i];
|
||||
manager->names[manager->count] = split[i];
|
||||
|
||||
// Increasing name index
|
||||
i_name++;
|
||||
|
||||
// DEBUG
|
||||
FURI_LOG_D(TAG, "Added %s to names", split[i]);
|
||||
|
||||
// Too big?
|
||||
if (i_name > 3) {break;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i_name < 4) {
|
||||
for (int i = i_name; i < 4; i++) {
|
||||
manager->names[i] = "\0";
|
||||
// It's another password!
|
||||
manager->count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
free(split);
|
||||
}
|
||||
|
||||
// Constructors
|
||||
Manager* manager_init() {
|
||||
// Creating result in memory
|
||||
Manager* result = malloc(sizeof(Manager));
|
||||
|
||||
// Setting variables
|
||||
result->count = 0;
|
||||
|
||||
// Loading all names on start
|
||||
manager_loadnames(result);
|
||||
|
||||
// Returning manager
|
||||
return result;
|
||||
}
|
||||
|
||||
// Functions
|
||||
void manager_loadpass(Manager* manager, char* name) {
|
||||
// Freeing previous password
|
||||
if (manager->current != NULL) {
|
||||
|
@ -141,7 +95,7 @@ void manager_loadpass(Manager* manager, char* name) {
|
|||
}
|
||||
|
||||
// DEBUG
|
||||
FURI_LOG_I(TAG, "Loading password of name %s", name);
|
||||
FURI_LOG_D(TAG, "Loading password of name %s", name);
|
||||
|
||||
// Loading password string
|
||||
char* data = store_load("Data");
|
||||
|
@ -179,7 +133,7 @@ void manager_loadpass(Manager* manager, char* name) {
|
|||
FURI_LOG_D(TAG, "Loaded Password: %s, %s, %s", manager->current->name, manager->current->user, manager->current->phrase);
|
||||
}
|
||||
void manager_free(Manager* manager) {
|
||||
for(int i = 0; i < 4; i++) {
|
||||
for(int i = 0; i < manager->count; i++) {
|
||||
free(manager->names[i]);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,16 +13,14 @@
|
|||
typedef struct {
|
||||
Password* current;
|
||||
|
||||
char* names[4];
|
||||
int page;
|
||||
char** names;
|
||||
int count;
|
||||
} Manager;
|
||||
|
||||
// Constructors
|
||||
Manager* manager_init();
|
||||
|
||||
// Functions
|
||||
void manager_switchpage(Manager* manager, int dir);
|
||||
void manager_loadnames(Manager* manager, int page);
|
||||
void manager_loadpass(Manager* manager, char* name);
|
||||
void manager_free(Manager* manager);
|
||||
|
||||
|
|
Loading…
Reference in a new issue