commit c822c36

Devine Lu Linvega  ·  2025-01-22 22:51:00 +0000 UTC
parent 7ded406
(file.c) Cleanup
1 files changed,  +39, -46
+39, -46
  1@@ -85,14 +85,14 @@ put_statfile(Uint8 *dest, Uint16 len, const char *filepath, const char *basename
  2 }
  3 
  4 static Uint16
  5-put_fdir(UxnFile *c, Uint8 *dest, Uint16 len)
  6+put_fdir(UxnFile *uf, Uint8 *dest, Uint16 len)
  7 {
  8 	Uint8 *_dest = dest;
  9 	static char pathname[0x2000];
 10-	struct dirent *de = readdir(c->dir);
 11-	for(; de != NULL; de = readdir(c->dir)) {
 12+	struct dirent *de = readdir(uf->dir);
 13+	for(; de != NULL; de = readdir(uf->dir)) {
 14 		if(de->d_name[0] == '.' && de->d_name[1] == 0) continue;
 15-		snprintf(pathname, 0x2000, c->filepath[0] == '.' ? "%s/%s" : "%s%s", c->filepath, de->d_name);
 16+		snprintf(pathname, 0x2000, uf->filepath[0] == '.' ? "%s/%s" : "%s%s", uf->filepath, de->d_name);
 17 		dest += put_statfile(dest, len, pathname, de->d_name);
 18 	}
 19 	return dest - _dest;
 20@@ -116,7 +116,7 @@ dir_exists(char *p)
 21 }
 22 
 23 static int
 24-ensure_parent_dirs(char *p)
 25+file_write_dir(char *p)
 26 {
 27 	int ok = 1;
 28 	char c, *s = p;
 29@@ -131,80 +131,73 @@ ensure_parent_dirs(char *p)
 30 }
 31 
 32 static void
 33-file_reset(UxnFile *c)
 34+file_reset(UxnFile *uf)
 35 {
 36-	if(c->f != NULL) fclose(c->f), c->f = NULL;
 37-	if(c->dir != NULL) closedir(c->dir), c->dir = NULL;
 38-	c->state = IDLE;
 39+	if(uf->f != NULL) fclose(uf->f), uf->f = NULL;
 40+	if(uf->dir != NULL) closedir(uf->dir), uf->dir = NULL;
 41+	uf->state = IDLE;
 42 }
 43 
 44 static Uint16
 45-file_init(UxnFile *c, Uint16 addr)
 46+file_init(UxnFile *uf, Uint16 addr)
 47 {
 48-	file_reset(c);
 49-	c->filepath = (char *)&uxn.ram[addr];
 50+	file_reset(uf);
 51+	uf->filepath = (char *)&uxn.ram[addr];
 52 	return 0;
 53 }
 54 
 55 static Uint16
 56-file_read(UxnFile *c, Uint16 addr, int len)
 57+file_read(UxnFile *uf, Uint16 addr, int len)
 58 {
 59 	void *dest = &uxn.ram[addr];
 60-	if(len > 0x10000 - addr)
 61-		len = 0x10000 - addr;
 62-	if(c->state != FILE_READ && c->state != DIR_READ) {
 63-		file_reset(c);
 64-		if((c->dir = opendir(c->filepath)) != NULL)
 65-			c->state = DIR_READ;
 66-		else if((c->f = fopen(c->filepath, "rb")) != NULL)
 67-			c->state = FILE_READ;
 68+	if(uf->state != FILE_READ && uf->state != DIR_READ) {
 69+		file_reset(uf);
 70+		if((uf->dir = opendir(uf->filepath)) != NULL)
 71+			uf->state = DIR_READ;
 72+		else if((uf->f = fopen(uf->filepath, "rb")) != NULL)
 73+			uf->state = FILE_READ;
 74 	}
 75-	if(c->state == FILE_READ)
 76-		return fread(dest, 1, len, c->f);
 77-	if(c->state == DIR_READ)
 78-		return put_fdir(c, dest, len);
 79+	if(uf->state == FILE_READ)
 80+		return fread(dest, 1, len, uf->f);
 81+	if(uf->state == DIR_READ)
 82+		return put_fdir(uf, dest, len);
 83 	return 0;
 84 }
 85 
 86 static Uint16
 87-file_write(UxnFile *c, Uint16 addr, Uint16 len, Uint8 flags)
 88+file_write(UxnFile *uf, Uint16 addr, Uint16 len, Uint8 flags)
 89 {
 90-	void *src = &uxn.ram[addr];
 91 	Uint16 ret = 0;
 92-	if(len > 0x10000 - addr)
 93-		len = 0x10000 - addr;
 94-	ensure_parent_dirs(c->filepath);
 95-	if(c->state != FILE_WRITE && c->state != DIR_WRITE) {
 96-		file_reset(c);
 97-		if(is_dir_path(c->filepath))
 98-			c->state = DIR_WRITE;
 99-		else if((c->f = fopen(c->filepath, (flags & 0x01) ? "ab" : "wb")) != NULL)
100-			c->state = FILE_WRITE;
101+	file_write_dir(uf->filepath);
102+	if(uf->state != FILE_WRITE && uf->state != DIR_WRITE) {
103+		file_reset(uf);
104+		if(is_dir_path(uf->filepath))
105+			uf->state = DIR_WRITE;
106+		else if((uf->f = fopen(uf->filepath, (flags & 0x01) ? "ab" : "wb")) != NULL)
107+			uf->state = FILE_WRITE;
108 	}
109-	if(c->state == FILE_WRITE) {
110-		if((ret = fwrite(src, 1, len, c->f)) > 0 && fflush(c->f) != 0)
111+	if(uf->state == FILE_WRITE)
112+		if((ret = fwrite(&uxn.ram[addr], 1, len, uf->f)) > 0 && fflush(uf->f) != 0)
113 			ret = 0;
114-	}
115-	if(c->state == DIR_WRITE) {
116-		ret = dir_exists(c->filepath);
117-	}
118+	if(uf->state == DIR_WRITE)
119+		ret = dir_exists(uf->filepath);
120 	return ret;
121 }
122 
123 static Uint16
124-file_stat(UxnFile *c, Uint16 addr, Uint16 len)
125+file_stat(UxnFile *uf, Uint16 addr, Uint16 len)
126 {
127 	int err, dir;
128 	struct stat st;
129-	err = stat(c->filepath, &st);
130+	err = stat(uf->filepath, &st);
131 	dir = S_ISDIR(st.st_mode);
132 	return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
133 }
134 
135 static Uint16
136-file_delete(UxnFile *c)
137+file_delete(UxnFile *uf)
138 {
139-	return unlink(c->filepath);
140+	return unlink(uf->filepath);
141 }
142 
143 /* file registers */