Jens Van Hoof / Mbed 2 deprecated SmartRemote

Dependencies:   EthernetInterface HTTPServer RemoteIR SDFileSystem mbed-rpc mbed-rtos mbed

Fork of SmartRemote by Sam Kirsch

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers db.cpp Source File

db.cpp

00001 #include "mbed.h"
00002 #include "db.h"
00003 #include "SDFileSystem.h"
00004 
00005 void db_insert_tuple(char *new_name, char *new_code, char *bitlength, char *format)
00006 {
00007 
00008     char tuple_code[128];
00009     char tuple_name[128];
00010     char tuple_id[128];
00011     char tuple_bitlength[128];
00012     char tuple_format[128];
00013     int id = 0;
00014 
00015     // create file if it does not exist
00016     FILE *ftemp = fopen("/sd/SmartRemote/db.txt", "a");
00017     fclose(ftemp);
00018 
00019     // read file to get last id
00020     FILE *fdb = fopen("/sd/SmartRemote/db.txt", "r");
00021     while ( !feof(fdb) ) {
00022         db_get_tuple(fdb, tuple_id, tuple_name, tuple_code, tuple_bitlength, tuple_format);
00023         if (strlen(tuple_name) != 0) {
00024             printf("Tuple: |%s| \t |%s| \t |%s| \t |%s| \t |%s|\n", tuple_id, tuple_name, tuple_code, tuple_bitlength, tuple_format);
00025             id = atoi(tuple_id);
00026         }
00027     }
00028     fclose(fdb);
00029 
00030     // id of new tuple
00031     id++;
00032     sprintf(tuple_id, "%d", id);
00033 
00034     // write new tuple to file
00035     char new_row[128];
00036     FILE *fwrite = fopen("/sd/SmartRemote/db.txt", "a");
00037 
00038     if (fwrite == NULL) {                                                   // if error
00039         printf("Could not open file for write\n");
00040     }
00041     
00042     // create new row
00043     sprintf(new_row, "%s\n%s\n%s\n%s\n%s\n", tuple_id, new_name, new_code, bitlength, format);
00044 
00045     fprintf(fwrite, new_row);                                               // write to file
00046     fclose(fwrite);                                                         // close file
00047 
00048     printf("Record Added: %s\n", new_row);
00049 }
00050 
00051 void db_print_all(FILE *fread)
00052 {
00053     char c;
00054     while ( !feof(fread) ) {
00055         c=fgetc(fread);
00056         printf("%c", c);
00057     }
00058 }
00059 
00060 void db_find_tuple(int id_to_find, char *tuple_name, char *tuple_code, char *tuple_bitlength, char *tuple_format)
00061 {
00062     FILE *fread = fopen("/sd/SmartRemote/db.txt", "r");
00063     
00064     char tuple_id[128];
00065     int id;
00066 
00067     while ( !feof(fread) ) {
00068         db_get_tuple(fread, tuple_id, tuple_name, tuple_code, tuple_bitlength, tuple_format);
00069         if (strlen(tuple_name) != 0) {
00070             id = atoi(tuple_id);
00071             if (id == id_to_find) {
00072                 break;
00073             }
00074         }
00075         else {
00076             sprintf(tuple_id, "%d", 0);
00077             sprintf(tuple_name, "%s", "");
00078             sprintf(tuple_code, "%s", "");
00079             sprintf(tuple_bitlength, "%s", "");
00080             sprintf(tuple_format, "%s", "");
00081         }
00082     }
00083 
00084     printf("Tuple Found: |%s| \t |%s| \t |%s| \t |%s| \t |%s|\n", tuple_id, tuple_name, tuple_code, tuple_bitlength, tuple_format);
00085     
00086     fclose(fread);
00087 }
00088 
00089 void db_get_tuple(FILE *fread, char *id_str, char *name_str, char *code_str, char *bitlength_str, char *format_str)
00090 {
00091 
00092     unsigned char c;
00093     int index;
00094 
00095     // read ID;
00096     index = 0;
00097     do {
00098         c = fgetc(fread);
00099         id_str[index] = c;
00100         index++;
00101     } while (c != '\n' &&  !feof(fread) );
00102     id_str[index-1] = 0;
00103 
00104     // read NAME;
00105     index = 0;
00106     do {
00107         c = fgetc(fread);
00108         name_str[index] = c;
00109         index++;
00110     } while (c != '\n' &&  !feof(fread) );
00111     name_str[index-1] = 0;
00112 
00113     // read CODE
00114     index = 0;
00115     do {
00116         c = fgetc(fread);
00117         code_str[index] = c;
00118         index++;
00119     } while (c != '\n' &&  !feof(fread) );
00120     code_str[index-1] = 0;
00121 
00122     // read BITLENGTH
00123     index = 0;
00124     do {
00125         c = fgetc(fread);
00126         bitlength_str[index] = c;
00127         index++;
00128     } while (c != '\n' &&  !feof(fread) );
00129     bitlength_str[index-1] = 0;
00130 
00131     // read FORMAT
00132     index = 0;
00133     do {
00134         c = fgetc(fread);
00135         format_str[index] = c;
00136         index++;
00137     } while (c != '\n' &&  !feof(fread) );
00138     format_str[index-1] = 0;
00139 }