Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HTTP_SERVER by
FileHandler.cpp
00001 #include "FileHandler.h" 00002 //#ifndef DEBUG 00003 #define FILE_HANDLER_DEBUG 00004 //#endif 00005 LocalFileSystem local("local"); 00006 00007 namespace FILE_HANDLER 00008 { 00009 void DEBUG_PRINT_NAME() 00010 { 00011 #ifdef FILE_HANDLER_DEBUG 00012 printf("(DEBUG LINE: FILE_HANDLER) "); 00013 #endif 00014 } 00015 00016 void DEBUG_PRINT_LINE(const char* arg_line) 00017 { 00018 #ifdef FILE_HANDLER_DEBUG 00019 DEBUG_PRINT_NAME(); 00020 printf(arg_line); 00021 printf("\r\n"); 00022 #endif 00023 } 00024 template<typename T> 00025 void DEBUG_PRINT_LINE(const char* arg_line, T arg_t) 00026 { 00027 #ifdef FILE_HANDLER_DEBUG 00028 DEBUG_PRINT_NAME(); 00029 printf(arg_line, arg_t); 00030 printf("\r\n"); 00031 #endif 00032 } 00033 template<typename T1, typename T2> 00034 void DEBUG_PRINT_LINE(const char* arg_line, T1 arg_t1, T2 arg_t2) 00035 { 00036 #ifdef FILE_HANDLER_DEBUG 00037 DEBUG_PRINT_NAME(); 00038 printf(arg_line, arg_t1, arg_t2); 00039 printf("\r\n"); 00040 #endif 00041 } 00042 } 00043 using namespace FILE_HANDLER; 00044 00045 FileHandler::FileHandler() 00046 { 00047 fullpath = NULL; 00048 filename = NULL; 00049 suffix = NULL; 00050 fp = NULL; 00051 file_size = 0; 00052 } 00053 FileHandler::~FileHandler() 00054 { 00055 if (fullpath != NULL) free(fullpath); 00056 if (fp != NULL) fclose(fp); 00057 } 00058 00059 FILE* FileHandler::open 00060 ( const char* arg_filepath, 00061 const char* arg_mode 00062 ) 00063 { 00064 FILE *tmp; 00065 00066 DEBUG_PRINT_LINE("fp: %d", fp); 00067 if (fullpath != NULL) free(fullpath); 00068 // Allocating memory for fullpath; 00069 // [[+ strlen("index.htm") + 1]] at the end of line is ONLY a MARGIN allowed for concatenating the string. 00070 fullpath = (char*)malloc(sizeof(char) * (strlen("/local/") + strlen(arg_filepath) + strlen("index.htm") + 1)); 00071 00072 // Path formatting 00073 if (arg_filepath[0] == '/') { 00074 sprintf(fullpath, "/local/%s", arg_filepath + 1); 00075 } else { 00076 sprintf(fullpath, "/local/%s", arg_filepath); 00077 } 00078 // if the argument has no file name but directory, defalt settiing. 00079 if (fullpath[strlen(fullpath) - 1] == '/') 00080 strcat(fullpath, "index.htm"); 00081 DEBUG_PRINT_LINE("full-file-path: %s", fullpath); 00082 // store the file name part into a pointer 00083 filename = strrchr(fullpath, '/'); 00084 // remove '/' and just get only the file name. 00085 if(filename != NULL) filename++; 00086 // store the suffix part to a pointer 00087 suffix = strchr(filename, '.'); 00088 // remove '.' and just get only the suffix. 00089 if(suffix != NULL) suffix++; 00090 DEBUG_PRINT_LINE("!!Public function check..."); 00091 DEBUG_PRINT_LINE(" full path: %s", getFullpath()); 00092 DEBUG_PRINT_LINE(" filename : %s", getFilename()); 00093 DEBUG_PRINT_LINE(" suffix : %s", getSuffix()); 00094 DEBUG_PRINT_LINE("...DONE!!"); 00095 DEBUG_PRINT_LINE("fopen(...)"); 00096 DEBUG_PRINT_LINE(" path: %s", fullpath); 00097 DEBUG_PRINT_LINE(" mode: %s", arg_mode); 00098 fp = fopen(fullpath, arg_mode); 00099 if(fp != NULL) { 00100 DEBUG_PRINT_LINE("file opened"); 00101 DEBUG_PRINT_LINE("fp: %d", fp); 00102 } else { 00103 DEBUG_PRINT_LINE("Something is wrong @fopen(...)"); 00104 } 00105 // mesure file size 00106 file_size = 0; 00107 tmp = fp; 00108 if(tmp != NULL ) { 00109 ////printf("\r\nfile content\r\n"); 00110 int ctmp; 00111 while(1) { 00112 ctmp = fgetc(tmp); 00113 if(ctmp != EOF) { 00114 //////printf("%c", ctmp); 00115 file_size++; 00116 } else { 00117 //////printf("[EOF]\r\n"); 00118 break; 00119 } 00120 } 00121 ////printf("file size: %d\r\n", file_size); 00122 if(fseek(tmp, 0L, SEEK_SET) != 0) { 00123 //////printf("fseek failed\r\n"); 00124 } 00125 } else { 00126 file_size = 0; 00127 } 00128 00129 return fp; 00130 } 00131 int FileHandler::close() 00132 { 00133 int tmp; 00134 00135 if(fp != NULL) { 00136 tmp = fclose(fp); 00137 fp = NULL; 00138 return tmp; 00139 } else { 00140 return 1; 00141 } 00142 } 00143 00144 int FileHandler::getc() 00145 { 00146 int tmp = fgetc(fp); 00147 #ifdef DEBUG 00148 if(0x20 < tmp && tmp < 0x7e) 00149 printf("%c", tmp); 00150 else if (tmp == '\r') 00151 printf("\r"); 00152 else if (tmp == '\n') 00153 printf("\n"); 00154 else 00155 printf("@"); 00156 #endif 00157 return tmp; 00158 } 00159 bool FileHandler::arrival() 00160 { 00161 return (bool)fp; 00162 } 00163 bool FileHandler::atEOF() 00164 { 00165 return (bool)feof(fp); 00166 } 00167 bool FileHandler::hasError() 00168 { 00169 return (bool)ferror(fp); 00170 } 00171 char *FileHandler::getFullpath() 00172 { 00173 return fullpath; 00174 } 00175 char *FileHandler::getFilename() 00176 { 00177 return filename; 00178 } 00179 char *FileHandler::getSuffix() 00180 { 00181 return suffix; 00182 } 00183 int FileHandler::getFileSize() 00184 { 00185 return file_size; 00186 }
Generated on Fri Jul 15 2022 08:17:44 by
1.7.2
