Skip to content

Commit cdf513b

Browse files
Add ti_Rename and ti_RenameVar
1 parent c86e469 commit cdf513b

File tree

7 files changed

+294
-17
lines changed

7 files changed

+294
-17
lines changed

examples/fileio_read_write/src/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void printText(int8_t xpos, int8_t ypos, const char *text);
2828

2929
/* Main Function */
3030
void main(void) {
31-
char name_buffer[10];
31+
char nameBuffer[10];
3232
ti_var_t myAppVar;
3333
int x;
3434

@@ -62,10 +62,10 @@ void main(void) {
6262
if (data.var1 != VAR1_VALUE && data.var2 != VAR2_VALUE) goto err;
6363

6464
/* Ensure the name of the AppVar is correct */
65-
ti_GetName(name_buffer, myAppVar);
65+
ti_GetName(nameBuffer, myAppVar);
6666

6767
printText(0, 0, "Appvar: ");
68-
printText(8, 0, name_buffer);
68+
printText(8, 0, nameBuffer);
6969
printText(0, 1, "Read was successful");
7070
goto noerr;
7171

@@ -85,3 +85,4 @@ void printText(int8_t xpos, int8_t ypos, const char *text) {
8585
os_SetCursorPos(ypos, xpos);
8686
os_PutStrFull(text);
8787
}
88+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"rom": "84pce_515.rom",
3+
"transfer_files": [
4+
"bin/DEMO.8xp"
5+
],
6+
"target": {
7+
"name": "DEMO",
8+
"isASM": true
9+
},
10+
"sequence": [
11+
"action|launch",
12+
"delay|1000",
13+
"hash|1",
14+
"key|enter",
15+
"delay|200",
16+
"hashWait|2"
17+
],
18+
"hashes": {
19+
"1": {
20+
"description": "Assert that the rename was succesful",
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [ "94538693" ]
24+
},
25+
"2": {
26+
"description": "Back to the home screen (exit check)",
27+
"start": "vram_start",
28+
"size": "vram_16_size",
29+
"expected_CRCs": [ "FFAF89BA", "101734A5", "9DA19F44" ]
30+
}
31+
}
32+
}
33+

examples/fileio_rename/makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ----------------------------
2+
# Set NAME to the program name
3+
# Set ICON to the png icon file name
4+
# Set DESCRIPTION to display within a compatible shell
5+
# Set COMPRESSED to "YES" to create a compressed program
6+
# ----------------------------
7+
8+
NAME ?= DEMO
9+
COMPRESSED ?= NO
10+
ICON ?= iconc.png
11+
DESCRIPTION ?= "C SDK Demo"
12+
13+
# ----------------------------
14+
15+
include $(CEDEV)/include/.makefile

examples/fileio_rename/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### FileIO Rename Demo
2+
3+
This example demonstrates how to rename previously created files
4+
5+
---
6+
7+
This demo is a part of the C SDK Toolchain for use on the CE.
8+

examples/fileio_rename/src/main.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdbool.h>
2+
#include <stddef.h>
3+
#include <stdint.h>
4+
#include <tice.h>
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
#include <fileioc.h>
11+
12+
/* Function prototypes */
13+
void printText(int8_t xpos, int8_t ypos, const char *text);
14+
15+
/* Main Function */
16+
void main(void) {
17+
/* Declare some variables */
18+
const char *oldName = "OldFile";
19+
const char *newName = "NewFile";
20+
char nameBuffer[10];
21+
ti_var_t file;
22+
bool err = true;
23+
24+
/* Clear the homescreen */
25+
os_ClrHome();
26+
27+
/* Close any previously open files */
28+
ti_CloseAll();
29+
30+
/* Delete both the new and old files if they already exist */
31+
ti_Delete(oldName);
32+
ti_Delete(newName);
33+
34+
do {
35+
/* Create a file with the old name */
36+
file = ti_Open(oldName, "w");
37+
if (!file) break;
38+
39+
ti_GetName(nameBuffer, file);
40+
printText(0, 0, "Old Name: ");
41+
printText(10, 0, nameBuffer);
42+
43+
/* Rename the old file to the new file name */
44+
/* Note: This function closes all open files! */
45+
ti_Rename(oldName, newName);
46+
47+
/* Ensure the new name of the file is correct */
48+
file = ti_Open(newName, "r");
49+
if (!file) break;
50+
51+
ti_GetName(nameBuffer, file);
52+
printText(0, 1, "New Name: ");
53+
printText(10, 1, nameBuffer);
54+
55+
err = false;
56+
} while (0);
57+
58+
/* Close all open files */
59+
ti_CloseAll();
60+
61+
/* If an error occured, inform the user */
62+
if (err == true) {
63+
printText(0, 2, "An error occured");
64+
}
65+
66+
/* Wait for a keypress */
67+
while (!os_GetCSC());
68+
}
69+
70+
/* Draw text on the homescreen at the given X/Y location */
71+
void printText(int8_t xpos, int8_t ypos, const char *text) {
72+
os_SetCursorPos(ypos, xpos);
73+
os_PutStrFull(text);
74+
}
75+

src/fileioc/fileioc.asm

Lines changed: 138 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ library 'FILEIOC', 4
5252
export ti_DetectAny
5353
export ti_GetVATPtr
5454
export ti_GetName
55+
export ti_Rename
56+
export ti_RenameVar
5557

5658
;-------------------------------------------------------------------------------
5759
resizeBytes := $E30C0C
@@ -888,7 +890,7 @@ _DetectJump:
888890
add hl,bc
889891
or a,a
890892
sbc hl,bc
891-
jr nz,.detectall ; if null, then detect everything
893+
jr nz,.detectall ; if null, then detect everything
892894
ld hl,.fdetectall
893895
ld (ix+9),hl
894896
.detectall:
@@ -945,13 +947,13 @@ _DetectType := $+1
945947
cp a,$D0
946948
jr nc,.finram
947949
ld de,9
948-
add hl,de ; skip archive VAT stuff
950+
add hl,de ; skip archive VAT stuff
949951
ld e,(hl)
950952
add hl,de
951953
inc hl
952954
.finram:
953955
inc hl
954-
inc hl ; hl -> data
956+
inc hl ; hl -> data
955957
ld bc,(ix+9)
956958
.fcmp:
957959
ld a,(bc)
@@ -961,7 +963,7 @@ _DetectType := $+1
961963
inc bc
962964
inc de
963965
inc hl
964-
jr z,.fcmp ; check the string at the memory
966+
jr z,.fcmp ; check the string at the memory
965967
.fskip:
966968
pop hl
967969
call .fbypassname
@@ -982,7 +984,7 @@ _DetectType := $+1
982984
pop ix
983985
ret
984986

985-
.fbypassname: ; bypass the name in the VAT (also used for setting the string name)
987+
.fbypassname: ; bypass the name in the VAT (also used for setting the string name)
986988
ld de,OP6
987989
ld bc,-6
988990
add hl,bc
@@ -1111,7 +1113,7 @@ ti_GetName:
11111113
ld hl,(hl)
11121114
ld bc,-6
11131115
add hl,bc
1114-
ld b,(hl) ; length of name
1116+
ld b,(hl) ; length of name
11151117
dec hl
11161118
.copy:
11171119
ld a,(hl)
@@ -1120,16 +1122,136 @@ ti_GetName:
11201122
dec hl
11211123
djnz .copy
11221124
xor a,a
1123-
ld (de),a ; terminate the string
1125+
ld (de),a ; terminate the string
1126+
ret
1127+
1128+
;-------------------------------------------------------------------------------
1129+
ti_RenameVar:
1130+
; Renames a variable with a new name
1131+
; Arguments:
1132+
; arg0 : Old name pointer
1133+
; arg1 : New name pointer
1134+
; arg2 : Variable type
1135+
; Returns:
1136+
; 0 if success
1137+
; 1 if new file already exists
1138+
; 2 if old file does not exist
1139+
; 3 if other error
1140+
ld iy,0
1141+
add iy,sp
1142+
ld a,(iy+9)
1143+
ld iy,flags ; probably not needed
1144+
jr ti_Rename.start
1145+
1146+
;-------------------------------------------------------------------------------
1147+
ti_Rename:
1148+
; Renames an appvar with a new name
1149+
; Arguments:
1150+
; arg0 : Old name pointer
1151+
; arg1 : New name pointer
1152+
; Returns:
1153+
; 0 if success
1154+
; 1 if new file already exists
1155+
; 2 if old file does not exist
1156+
; 3 if other error
1157+
ld a,appVarObj ; file type
1158+
.start:
1159+
pop bc
1160+
pop hl
1161+
pop de
1162+
push de ; de -> new name
1163+
push hl ; hl -> old name
1164+
push bc
1165+
1166+
push af
1167+
call ti_CloseAll
1168+
pop af
1169+
1170+
push de ; new
1171+
push de ; new
1172+
ld de,OP1
1173+
ld (de),a
1174+
inc de
1175+
call _Mov8b
1176+
call _PushOP1 ; save old name
1177+
1178+
ld hl,_Arc_Unarc
1179+
ld (.smc_archive),hl
1180+
1181+
pop hl ; new name
1182+
ld de,OP1+1
1183+
call _Mov8b
1184+
call _ChkFindSym
1185+
push af
1186+
call _PopOP1
1187+
pop af
1188+
jr nc,.return_1 ; check if name already exists
1189+
.locate_program:
1190+
call _ChkFindSym ; find old name
1191+
jr c,.return_2
1192+
call _ChkInRam
1193+
jr nz,.in_archive
1194+
ld hl,$f8 ; $f8 = ret
1195+
ld (.smc_archive),hl
1196+
call _PushOP1
1197+
call _Arc_Unarc
1198+
call _PopOP1
1199+
jr .locate_program
1200+
.in_archive:
1201+
ex de,hl
1202+
ld de,9
1203+
add hl,de ; skip vat stuff
1204+
ld e,(hl)
1205+
add hl,de
1206+
inc hl ; size of name
1207+
call _LoadDEInd_s
1208+
pop bc ; bc -> new name
1209+
push hl
1210+
push de
1211+
push bc
1212+
call _PushOP1 ; old name
1213+
1214+
pop hl
1215+
ld de,OP1+1
1216+
call _Mov8b
1217+
call _PushOP1 ; new name
1218+
pop hl
1219+
push hl
1220+
ld a,(OP1)
1221+
call _CreateVar
1222+
inc de
1223+
inc de
1224+
pop bc
1225+
pop hl
1226+
call _ChkBCIs0
1227+
jr z,.is_zero
1228+
ldir
1229+
.is_zero:
1230+
call _PopOP1
1231+
call _Arc_Unarc
1232+
.smc_archive := $-3
1233+
call _PopOP1
1234+
call _ChkFindSym
1235+
call _DelVarArc
1236+
xor a,a
1237+
ret
1238+
.return_1:
1239+
pop de ; new name
1240+
ld a,1
1241+
ret
1242+
.return_2:
1243+
pop de ; new name
1244+
ld a,2
11241245
ret
11251246

11261247
;-------------------------------------------------------------------------------
11271248
ti_SetVar:
11281249
; Sets a variable
1129-
; Gets a pointer to a variable data struct
1250+
; Gets a pointer to a variable data structure
11301251
; Arguments:
1131-
; arg0 : Pointer to variable name string
1132-
; arg1 : Pointer to data structure pointer
1252+
; arg0 : Pointer to name of variable
1253+
; arg1 : Pointer to data to set
1254+
; arg2 : Type of variable to set
11331255
; Returns:
11341256
; Any error codes returned when storing
11351257
push ix
@@ -1160,10 +1282,12 @@ ti_SetVar:
11601282
;-------------------------------------------------------------------------------
11611283
ti_StoVar:
11621284
; Stores a variable
1163-
; Gets a pointer to a variable data struct
1285+
; Gets a pointer to a variable data structure
11641286
; Arguments:
1165-
; arg0 : Pointer to variable name string
1166-
; arg1 : Pointer to data structure pointer
1287+
; arg0 : Type of variable to store to
1288+
; arg1 : Pointer to data to store to
1289+
; arg2 : Type of variable to get from
1290+
; arg3 : Pointer to data to get from
11671291
; Returns:
11681292
; Any error codes returned when storing
11691293
ld iy,0
@@ -1230,7 +1354,7 @@ _SetVarStr:
12301354
; a - Type
12311355
ld de,OP1+1
12321356
call _Mov9b
1233-
and a,03Fh
1357+
and a,$3f
12341358
ld (OP1),a
12351359
ret
12361360

0 commit comments

Comments
 (0)