Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fxcg/display.h>
- #include <fxcg/file.h>
- #include <fxcg/keyboard.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define CREATEMODE_FILE 1
- #define CREATEMODE_FOLDER 5
- #define READ 0
- #define READ_SHARE 1
- #define WRITE 2
- #define READWRITE 3
- #define READWRITE_SHARE 4
- void Print(unsigned char*msg) {
- Print_OS(msg, 0, 0);
- }
- typedef struct EventDate // Event date definition
- {
- unsigned day;
- unsigned month;
- unsigned year;
- } EventDate;
- typedef struct EventTime // Event time definition
- {
- unsigned hour;
- unsigned minute;
- unsigned second;
- } EventTime;
- typedef struct CalendarEvent // Defines what a calendar event contains
- {
- unsigned id; //something random like an hash.
- unsigned category;
- unsigned daterange;
- EventDate startdate;
- EventDate enddate;
- unsigned dayofweek;
- unsigned repeat;
- unsigned timed; //full-day = 0, timed = 1
- EventTime starttime;
- EventTime endtime;
- char* title;
- char* location;
- char* description;
- } CalendarEvent;
- // end of type definitions
- static unsigned int lastrandom=0x12345678;
- unsigned int random( int seed = 0 ){
- if (seed) lastrandom=seed;
- lastrandom = ( 0x41C64E6D*lastrandom ) + 0x3039;
- return ( lastrandom >> 16 );
- }
- char* concat(char* str1, char* str2) {
- int len1 = 0;
- int len2 = 0;
- for (;str1[len1] != '\0'; len1++);
- for (;str2[len2] != '\0'; len2++);
- char* str3 = (char*)malloc(len1 + len2 + 1);
- int i;
- for (i = 0; i < len1; str3[i] = str1[i], i++);
- for (i = 0; i < len2; str3[len1+i] = str2[i], i++);
- str3[len1 + len2] = 0;
- return str3;
- }
- //calendar and calendar event management
- char* calEventToChar(CalendarEvent calEvent) {
- //parses a CalendarEvent struct and turns it into a string which can be written to a MCS entry or archived in a file.
- //char buf[1000];
- char* buf = (char*)"";
- /* Field separator: 0x1D
- Event separator: 0x1E
- The first field (category) begins with no separator.
- An event doesn't begin with any separators, and the last field ends with a field separator followed by an event separator.
- id cat drange|start date|end date dow repeat|timed|start time|end time|title loc desc
- V V V V V V V V V V V V V V V V V V V V V*/
- //const char* format = (const char)"%u\x1D%u\x1D%u\x1D%u%u%u\x1D%u%u%u\x1D%u\x1D%u\x1D%u\x1D%u%u%u\x1D%u%u%u\x1D%s\x1D%s\x1D%s\x1D";
- //sprintf(buf, "%u\x1D%u\x1D%u\x1D%u%u%u\x1D%u%u%u\x1D%u\x1D%u\x1D%u\x1D%u%u%u\x1D%u%u%u\x1D%s\x1D%s\x1D%s\x1D\x1E", calEvent.id, calEvent.category, calEvent.daterange, calEvent.startdate.day, calEvent.startdate.month, calEvent.startdate.year, calEvent.enddate.day, calEvent.enddate.month, calEvent.enddate.year, calEvent.dayofweek, calEvent.repeat, calEvent.timed, calEvent.starttime.hour, calEvent.starttime.minute, calEvent.starttime.second, calEvent.endtime.hour, calEvent.endtime.minute, calEvent.endtime.second, calEvent.title, calEvent.location, calEvent.description);
- //I couldn't get sprintf to work very well... uses concat and itoa instead :-/
- //For some reason, the emulator does not do itoa
- //On the real Prizm, itoa works well, but 0x1D does not get printed to the file EXCEPT when it is followed by 0x1E!?!?
- //Doesn't matter if I use defines, if I specify the string directly on the command or if I declare a char* for it. \x1D never gets printed.
- // BUT: on the emulator, it ALWAYS works right!
- // If I change the field separator to \x1D\x1E, neither the field or the event separators get written to the file (on a real Prizm). Event separator works fine otherwise!
- unsigned char* smallbuf = (unsigned char*)"";
- #define FIELD_SEPARATOR "\x1D\x1E"
- #define EVENT_SEPARATOR "\x1D\x1E" //add 0x1D to it so that's it's easier to concat. may be changed in the future
- itoa(calEvent.id, smallbuf); buf = (char*)smallbuf; buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.category, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.daterange, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.startdate.day, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.startdate.month, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.startdate.year, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.enddate.day, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.enddate.month, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.enddate.year, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.dayofweek, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.repeat, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.timed, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.starttime.hour, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.starttime.minute, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.starttime.second, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.endtime.hour, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.endtime.minute, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- itoa(calEvent.endtime.second, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
- buf = concat(buf, calEvent.title); buf = concat(buf, (char*)FIELD_SEPARATOR);
- buf = concat(buf, calEvent.location); buf = concat(buf, (char*)FIELD_SEPARATOR);
- buf = concat(buf, calEvent.description); buf = concat(buf, (char*)EVENT_SEPARATOR);
- return buf;
- }
- int ArchiveEvent(CalendarEvent calEvent, const char* filename) {
- //Archives a calendar event on an existing calendar with specified file name.
- //If the specified file doesn't exist, it is created and the event is added to it.
- //Returns 0 on success, other values on error.
- //int fnlength = strlen(filename);
- const char* header = "PCALEVT"; //Prizm CALendar EVenT
- const char* newevent = calEventToChar(calEvent);
- int size = strlen(header) + strlen(newevent);
- unsigned short pFile[256];
- Bfile_StrToName_ncpy(pFile, (unsigned char*)filename, strlen(filename)+1);
- int hFile = Bfile_OpenFile_OS(pFile, READWRITE); // Get handle
- if(hFile < 0) // Check if it opened
- {
- // Returned error, file might not exist, so create it
- int BCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size);
- if(BCEres >= 0) // Did it create?
- {
- //open in order to write header and new event
- hFile = Bfile_OpenFile_OS(pFile, READWRITE);
- if(hFile < 0) // Still failing?
- {
- return 1;
- }
- //Write header
- Bfile_WriteFile_OS(hFile, header, strlen(header));
- //Write event
- Bfile_WriteFile_OS(hFile, newevent, strlen(newevent));
- Bfile_CloseFile_OS(hFile);
- return 0;
- }
- else
- {
- // file doesn't exist, but can't be created?
- return 2;
- }
- } else {
- /*File exists and is open.
- 0. Hope there's enough heap to store everything throughout the process.
- 1. Read its contents and size and save them.
- 2. Close, delete the file.
- 3. Create the same file with the previous size plus the size for the new event.
- 4. Open the new file.
- 5. Write header and previous contents.
- 6. Write new contents (new event).
- 7. Close file.
- It must be done this way because once a file is created, its size cannot be changed...*/
- int oldsize = Bfile_GetFileSize_OS(hFile, Bfile_TellFile_OS( hFile ));
- unsigned char* oldcontents = (unsigned char*)malloc((unsigned int)oldsize);
- if(oldsize && oldcontents) {
- Bfile_ReadFile_OS(hFile, oldcontents, oldsize, 0);
- }
- Bfile_CloseFile_OS(hFile);
- Bfile_DeleteEntry(pFile);
- // we already read the previous contents and size, closed the file and deleted it.
- // now recreate it with new size and write new contents to it.
- int newsize = oldsize + strlen(newevent);
- int nBCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &newsize);
- if(nBCEres >= 0) // Did it create?
- {
- int nFile = Bfile_OpenFile_OS(pFile, READWRITE);
- Bfile_WriteFile_OS(nFile, oldcontents, oldsize);
- Bfile_WriteFile_OS(nFile, newevent, strlen(newevent));
- Bfile_CloseFile_OS(nFile);
- free(oldcontents);
- } else {
- return 3;
- }
- }
- return 0;
- }
- void AddEvent(CalendarEvent calEvent, const char* filename) {
- //Add event to main memory
- }
- int RemoveEvent(CalendarEvent calEvent, const char* filename) {
- /*this function removes the event with the id of calEvent
- it doesn't check other elements of calEvent
- Returns 1 on error (event or file not found), 0 on success.*/
- return 0;
- }
- // main UI
- int main()
- {
- int key;
- CalendarEvent testEvent;
- EventDate startdate;
- EventDate enddate;
- EventTime starttime;
- EventTime endtime;
- while (1) {
- Bdisp_AllClr_VRAM();
- Bdisp_EnableColor(1);
- PrintXY(1,1,(char*)" F1: write event", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
- PrintXY(1,2,(char*)" F2: write another event", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
- GetKey(&key);
- switch (key) {
- case KEY_CTRL_F1:
- testEvent.id = 1234;
- testEvent.category = 1;
- testEvent.daterange = 0;
- startdate.day = 15;
- startdate.month = 8;
- startdate.year = 2012;
- testEvent.startdate = startdate;
- enddate.day = 15;
- enddate.month = 8;
- enddate.year = 2012;
- testEvent.enddate = enddate;
- testEvent.dayofweek = 0;
- testEvent.repeat = 0;
- testEvent.timed = 1;
- starttime.hour = 15;
- starttime.minute = 30;
- starttime.second = 0;
- testEvent.starttime = starttime;
- endtime.hour = 16;
- endtime.minute = 45;
- endtime.second = 0;
- testEvent.endtime = endtime;
- testEvent.title = (char*)"Test event";
- testEvent.location = (char*)"Somewhere";
- testEvent.description = (char*)"This is a description for the test event.";
- ArchiveEvent(testEvent, "\\\\fls0\\test.pce\0");
- break;
- case KEY_CTRL_F2:
- testEvent.id = 9876;
- testEvent.category = 7;
- testEvent.daterange = 0;
- startdate.day = 31;
- startdate.month = 12;
- startdate.year = 2014;
- testEvent.startdate = startdate;
- enddate.day = 5;
- enddate.month = 1;
- enddate.year = 2015;
- testEvent.enddate = enddate;
- testEvent.dayofweek = 5;
- testEvent.repeat = 1;
- testEvent.timed = 1;
- starttime.hour = 5;
- starttime.minute = 15;
- starttime.second = 0;
- testEvent.starttime = starttime;
- endtime.hour = 25;
- endtime.minute = 00;
- endtime.second = 35;
- testEvent.endtime = endtime;
- testEvent.title = (char*)"Another event";
- testEvent.location = (char*)"Some place";
- testEvent.description = (char*)"Another event starting in 2014 and ending in 2015.";
- ArchiveEvent(testEvent, "\\\\fls0\\test.pce\0");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement