Advertisement
Guest User

Untitled

a guest
Aug 16th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. #include <fxcg/display.h>
  2. #include <fxcg/file.h>
  3. #include <fxcg/keyboard.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define CREATEMODE_FILE 1
  8. #define CREATEMODE_FOLDER 5
  9. #define READ 0
  10. #define READ_SHARE 1
  11. #define WRITE 2
  12. #define READWRITE 3
  13. #define READWRITE_SHARE 4
  14.  
  15. void Print(unsigned char*msg) {
  16. Print_OS(msg, 0, 0);
  17. }
  18.  
  19. typedef struct EventDate // Event date definition
  20. {
  21. unsigned day;
  22. unsigned month;
  23. unsigned year;
  24. } EventDate;
  25.  
  26. typedef struct EventTime // Event time definition
  27. {
  28. unsigned hour;
  29. unsigned minute;
  30. unsigned second;
  31. } EventTime;
  32.  
  33. typedef struct CalendarEvent // Defines what a calendar event contains
  34. {
  35. unsigned id; //something random like an hash.
  36. unsigned category;
  37. unsigned daterange;
  38. EventDate startdate;
  39. EventDate enddate;
  40. unsigned dayofweek;
  41. unsigned repeat;
  42. unsigned timed; //full-day = 0, timed = 1
  43. EventTime starttime;
  44. EventTime endtime;
  45. char* title;
  46. char* location;
  47. char* description;
  48. } CalendarEvent;
  49. // end of type definitions
  50.  
  51. static unsigned int lastrandom=0x12345678;
  52. unsigned int random( int seed = 0 ){
  53. if (seed) lastrandom=seed;
  54. lastrandom = ( 0x41C64E6D*lastrandom ) + 0x3039;
  55. return ( lastrandom >> 16 );
  56. }
  57. char* concat(char* str1, char* str2) {
  58. int len1 = 0;
  59. int len2 = 0;
  60. for (;str1[len1] != '\0'; len1++);
  61. for (;str2[len2] != '\0'; len2++);
  62.  
  63. char* str3 = (char*)malloc(len1 + len2 + 1);
  64. int i;
  65. for (i = 0; i < len1; str3[i] = str1[i], i++);
  66. for (i = 0; i < len2; str3[len1+i] = str2[i], i++);
  67. str3[len1 + len2] = 0;
  68.  
  69. return str3;
  70. }
  71.  
  72. //calendar and calendar event management
  73. char* calEventToChar(CalendarEvent calEvent) {
  74. //parses a CalendarEvent struct and turns it into a string which can be written to a MCS entry or archived in a file.
  75. //char buf[1000];
  76. char* buf = (char*)"";
  77. /* Field separator: 0x1D
  78. Event separator: 0x1E
  79. The first field (category) begins with no separator.
  80. An event doesn't begin with any separators, and the last field ends with a field separator followed by an event separator.
  81. id cat drange|start date|end date dow repeat|timed|start time|end time|title loc desc
  82. V V V V V V V V V V V V V V V V V V V V V*/
  83. //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";
  84. //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);
  85.  
  86.  
  87. //I couldn't get sprintf to work very well... uses concat and itoa instead :-/
  88. //For some reason, the emulator does not do itoa
  89. //On the real Prizm, itoa works well, but 0x1D does not get printed to the file EXCEPT when it is followed by 0x1E!?!?
  90. //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.
  91. // BUT: on the emulator, it ALWAYS works right!
  92. // 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!
  93. unsigned char* smallbuf = (unsigned char*)"";
  94. #define FIELD_SEPARATOR "\x1D\x1E"
  95. #define EVENT_SEPARATOR "\x1D\x1E" //add 0x1D to it so that's it's easier to concat. may be changed in the future
  96. itoa(calEvent.id, smallbuf); buf = (char*)smallbuf; buf = concat(buf, (char*)FIELD_SEPARATOR);
  97. itoa(calEvent.category, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  98. itoa(calEvent.daterange, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  99. itoa(calEvent.startdate.day, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  100. itoa(calEvent.startdate.month, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  101. itoa(calEvent.startdate.year, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  102. itoa(calEvent.enddate.day, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  103. itoa(calEvent.enddate.month, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  104. itoa(calEvent.enddate.year, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  105. itoa(calEvent.dayofweek, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  106. itoa(calEvent.repeat, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  107. itoa(calEvent.timed, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  108. itoa(calEvent.starttime.hour, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  109. itoa(calEvent.starttime.minute, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  110. itoa(calEvent.starttime.second, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  111. itoa(calEvent.endtime.hour, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  112. itoa(calEvent.endtime.minute, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  113. itoa(calEvent.endtime.second, smallbuf); buf = concat(buf, (char*)smallbuf); buf = concat(buf, (char*)FIELD_SEPARATOR);
  114. buf = concat(buf, calEvent.title); buf = concat(buf, (char*)FIELD_SEPARATOR);
  115. buf = concat(buf, calEvent.location); buf = concat(buf, (char*)FIELD_SEPARATOR);
  116. buf = concat(buf, calEvent.description); buf = concat(buf, (char*)EVENT_SEPARATOR);
  117. return buf;
  118. }
  119. int ArchiveEvent(CalendarEvent calEvent, const char* filename) {
  120. //Archives a calendar event on an existing calendar with specified file name.
  121. //If the specified file doesn't exist, it is created and the event is added to it.
  122. //Returns 0 on success, other values on error.
  123. //int fnlength = strlen(filename);
  124. const char* header = "PCALEVT"; //Prizm CALendar EVenT
  125. const char* newevent = calEventToChar(calEvent);
  126. int size = strlen(header) + strlen(newevent);
  127. unsigned short pFile[256];
  128. Bfile_StrToName_ncpy(pFile, (unsigned char*)filename, strlen(filename)+1);
  129. int hFile = Bfile_OpenFile_OS(pFile, READWRITE); // Get handle
  130. if(hFile < 0) // Check if it opened
  131. {
  132. // Returned error, file might not exist, so create it
  133. int BCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size);
  134. if(BCEres >= 0) // Did it create?
  135. {
  136. //open in order to write header and new event
  137. hFile = Bfile_OpenFile_OS(pFile, READWRITE);
  138. if(hFile < 0) // Still failing?
  139. {
  140. return 1;
  141. }
  142. //Write header
  143. Bfile_WriteFile_OS(hFile, header, strlen(header));
  144. //Write event
  145. Bfile_WriteFile_OS(hFile, newevent, strlen(newevent));
  146.  
  147. Bfile_CloseFile_OS(hFile);
  148. return 0;
  149. }
  150. else
  151. {
  152. // file doesn't exist, but can't be created?
  153. return 2;
  154. }
  155. } else {
  156. /*File exists and is open.
  157. 0. Hope there's enough heap to store everything throughout the process.
  158. 1. Read its contents and size and save them.
  159. 2. Close, delete the file.
  160. 3. Create the same file with the previous size plus the size for the new event.
  161. 4. Open the new file.
  162. 5. Write header and previous contents.
  163. 6. Write new contents (new event).
  164. 7. Close file.
  165. It must be done this way because once a file is created, its size cannot be changed...*/
  166. int oldsize = Bfile_GetFileSize_OS(hFile, Bfile_TellFile_OS( hFile ));
  167. unsigned char* oldcontents = (unsigned char*)malloc((unsigned int)oldsize);
  168. if(oldsize && oldcontents) {
  169. Bfile_ReadFile_OS(hFile, oldcontents, oldsize, 0);
  170. }
  171. Bfile_CloseFile_OS(hFile);
  172. Bfile_DeleteEntry(pFile);
  173. // we already read the previous contents and size, closed the file and deleted it.
  174. // now recreate it with new size and write new contents to it.
  175. int newsize = oldsize + strlen(newevent);
  176. int nBCEres = Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &newsize);
  177. if(nBCEres >= 0) // Did it create?
  178. {
  179. int nFile = Bfile_OpenFile_OS(pFile, READWRITE);
  180. Bfile_WriteFile_OS(nFile, oldcontents, oldsize);
  181. Bfile_WriteFile_OS(nFile, newevent, strlen(newevent));
  182. Bfile_CloseFile_OS(nFile);
  183. free(oldcontents);
  184. } else {
  185. return 3;
  186. }
  187.  
  188. }
  189. return 0;
  190. }
  191.  
  192. void AddEvent(CalendarEvent calEvent, const char* filename) {
  193. //Add event to main memory
  194.  
  195. }
  196.  
  197. int RemoveEvent(CalendarEvent calEvent, const char* filename) {
  198. /*this function removes the event with the id of calEvent
  199. it doesn't check other elements of calEvent
  200. Returns 1 on error (event or file not found), 0 on success.*/
  201. return 0;
  202. }
  203.  
  204. // main UI
  205.  
  206. int main()
  207. {
  208. int key;
  209. CalendarEvent testEvent;
  210. EventDate startdate;
  211. EventDate enddate;
  212. EventTime starttime;
  213. EventTime endtime;
  214. while (1) {
  215. Bdisp_AllClr_VRAM();
  216. Bdisp_EnableColor(1);
  217.  
  218. PrintXY(1,1,(char*)" F1: write event", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
  219. PrintXY(1,2,(char*)" F2: write another event", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
  220.  
  221. GetKey(&key);
  222. switch (key) {
  223. case KEY_CTRL_F1:
  224. testEvent.id = 1234;
  225. testEvent.category = 1;
  226. testEvent.daterange = 0;
  227.  
  228. startdate.day = 15;
  229. startdate.month = 8;
  230. startdate.year = 2012;
  231. testEvent.startdate = startdate;
  232.  
  233. enddate.day = 15;
  234. enddate.month = 8;
  235. enddate.year = 2012;
  236. testEvent.enddate = enddate;
  237.  
  238. testEvent.dayofweek = 0;
  239. testEvent.repeat = 0;
  240. testEvent.timed = 1;
  241.  
  242. starttime.hour = 15;
  243. starttime.minute = 30;
  244. starttime.second = 0;
  245. testEvent.starttime = starttime;
  246.  
  247. endtime.hour = 16;
  248. endtime.minute = 45;
  249. endtime.second = 0;
  250. testEvent.endtime = endtime;
  251.  
  252. testEvent.title = (char*)"Test event";
  253. testEvent.location = (char*)"Somewhere";
  254. testEvent.description = (char*)"This is a description for the test event.";
  255. ArchiveEvent(testEvent, "\\\\fls0\\test.pce\0");
  256. break;
  257. case KEY_CTRL_F2:
  258. testEvent.id = 9876;
  259. testEvent.category = 7;
  260. testEvent.daterange = 0;
  261.  
  262. startdate.day = 31;
  263. startdate.month = 12;
  264. startdate.year = 2014;
  265. testEvent.startdate = startdate;
  266.  
  267. enddate.day = 5;
  268. enddate.month = 1;
  269. enddate.year = 2015;
  270. testEvent.enddate = enddate;
  271.  
  272. testEvent.dayofweek = 5;
  273. testEvent.repeat = 1;
  274. testEvent.timed = 1;
  275.  
  276. starttime.hour = 5;
  277. starttime.minute = 15;
  278. starttime.second = 0;
  279. testEvent.starttime = starttime;
  280.  
  281. endtime.hour = 25;
  282. endtime.minute = 00;
  283. endtime.second = 35;
  284. testEvent.endtime = endtime;
  285.  
  286. testEvent.title = (char*)"Another event";
  287. testEvent.location = (char*)"Some place";
  288. testEvent.description = (char*)"Another event starting in 2014 and ending in 2015.";
  289. ArchiveEvent(testEvent, "\\\\fls0\\test.pce\0");
  290. break;
  291. }
  292. }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement