Skip to content

Commit

Permalink
add util/file, move file utils there
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jan 19, 2023
1 parent f054f1b commit a579b64
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 53 deletions.
44 changes: 0 additions & 44 deletions src/core/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,50 +60,6 @@ uint8_t slow_key(left_dial_t key,uint8_t* state,uint8_t* cnt)
return 0;
}

///////////////////////////////////////////////////////////////////////////////
// file compare
bool file_compare(char* f1,char* f2)
{
FILE* fp1;
FILE* fp2;
char c1,c2;
bool ret;

fp1 = fopen(f1,"r");
if(!fp1) return false;

fp2 = fopen(f2,"r");
if(!fp2) {
fclose(fp1);
return false;
}

ret = true;
while(!feof(fp1)) {
fread(&c1,1,1,fp1);
fread(&c2,1,1,fp2);
if(c1 != c2) {
ret = false;
break;
}
}
fclose(fp1);
fclose(fp2);
return ret;
}

bool file_exists(const char* filename) {
return access(filename, F_OK) == 0;
}

long file_get_size(const char* filename) {
struct stat st;
if (stat(filename, &st) != 0) {
return 0;
}
return st.st_size;
}

///////////////////////////////////////////////////////////////////////////////
// GPIO
void gpio_init()
Expand Down
15 changes: 6 additions & 9 deletions src/core/common.hh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#ifndef __COMMON_HH__
#define __COMMON_HH__

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include <lvgl/lvgl.h>

#include "defines.h"
#include "lvgl/lvgl.h"

#define DIAL_KEY_UP 1
#define DIAL_KEY_DOWN 2
Expand All @@ -22,10 +24,6 @@ extern pthread_mutex_t lvgl_mutex;

uint8_t slow_key(left_dial_t key,uint8_t* state,uint8_t* cnt);

bool file_compare(char* f1,char* f2);
bool file_exists(const char* filename);
long file_get_size(const char* filename);

void gpio_init();
void open_gpio(int port_num);
void set_gpio(int port_num, int isHigh);
Expand All @@ -34,4 +32,3 @@ void beep_n(int dur_us);
#define beep() beep_n(500)

#endif //__COMMON_HH__

1 change: 1 addition & 0 deletions src/ui/page_playback.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ui/page_common.h"
#include "ui/ui_player.h"
#include "ui/ui_style.h"
#include "util/file.h"

LV_IMG_DECLARE(img_arrow1);

Expand Down
1 change: 1 addition & 0 deletions src/ui/page_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ui/page_common.h"
#include "ui/ui_main_menu.h"
#include "ui/ui_style.h"
#include "util/file.h"

static lv_coord_t col_dsc[] = {160, 160, 160, 160, 160, 160, 160, LV_GRID_TEMPLATE_LAST};
static lv_coord_t row_dsc[] = {60, 60, 60, 60, 60, 60, 60, 60, 60, 60, LV_GRID_TEMPLATE_LAST};
Expand Down
65 changes: 65 additions & 0 deletions src/util/file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "file.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

bool file_compare(char *f1, char *f2) {
FILE *fp1;
FILE *fp2;
char c1, c2;
bool ret;

fp1 = fopen(f1, "r");
if (!fp1)
return false;

fp2 = fopen(f2, "r");
if (!fp2) {
fclose(fp1);
return false;
}

ret = true;
while (!feof(fp1)) {
fread(&c1, 1, 1, fp1);
fread(&c2, 1, 1, fp2);
if (c1 != c2) {
ret = false;
break;
}
}
fclose(fp1);
fclose(fp2);
return ret;
}

bool file_exists(const char *filename) {
return access(filename, F_OK) == 0;
}

bool file_printf(const char *filename, const char *fmt, ...) {
FILE *fp = fopen(filename, "w");
if (!fp) {
return false;
}

va_list args;
va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);

fclose(fp);

return true;
}

long file_get_size(const char* filename) {
struct stat st;
if (stat(filename, &st) != 0) {
return 0;
}
return st.st_size;
}
8 changes: 8 additions & 0 deletions src/util/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>

bool file_compare(char *f1, char *f2);
bool file_exists(const char *filename);
bool file_printf(const char *filename, const char *fmt, ...);
long file_get_size(const char* filename);

0 comments on commit a579b64

Please sign in to comment.