Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/
Dependencies: BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem mbed-rtos mbed
HTTPFile.cpp
00001 /* HTTPFile.cpp */ 00002 #include "HTTPFile.h" 00003 00004 #include <cstring> 00005 00006 #define OK 0 00007 #define NG -1 00008 00009 using std::memcpy; 00010 using std::strncpy; 00011 using std::strlen; 00012 00013 HTTPFile::HTTPFile(const char* path) : 00014 m_fp(NULL), 00015 m_path(path), 00016 m_len(0), 00017 m_chunked(false) 00018 { 00019 } 00020 00021 HTTPFile::~HTTPFile() 00022 { 00023 closeFile(); 00024 } 00025 00026 void HTTPFile::clear() 00027 { 00028 closeFile(); 00029 //Force reopening 00030 } 00031 00032 /*virtual*/ void HTTPFile::readReset() 00033 { 00034 if(m_fp) 00035 fseek(m_fp, 0, SEEK_SET); 00036 } 00037 00038 /*virtual*/ int HTTPFile::read(char* buf, size_t len, size_t* pReadLen) 00039 { 00040 if(!openFile("r")) //File does not exist, or I/O error... 00041 return NG; 00042 00043 *pReadLen = fread(buf, 1, len, m_fp); 00044 if( feof(m_fp) ) 00045 { 00046 //File read completely, we can close it 00047 closeFile(); 00048 } 00049 return OK; 00050 } 00051 00052 /*virtual*/ void HTTPFile::writeReset() 00053 { 00054 if(m_fp) 00055 fseek(m_fp, 0, SEEK_SET); 00056 } 00057 00058 /*virtual*/ int HTTPFile::write(const char* buf, size_t len) 00059 { 00060 if(!openFile("w")) //File does not exist, or I/O error... 00061 return NG; 00062 00063 len = fwrite(buf, 1, len, m_fp); 00064 //DBG("Written %d bytes in %d\n", len, m_fp); 00065 if( (!m_chunked && (ftell(m_fp) >= m_len)) || (m_chunked && !len) ) 00066 { 00067 //File received completely, we can close it 00068 closeFile(); 00069 } 00070 return len; 00071 } 00072 00073 /*virtual*/ int HTTPFile::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header 00074 { 00075 strncpy(type, "text/plain", maxTypeLen-1); 00076 type[maxTypeLen-1] = '\0'; 00077 return OK; 00078 } 00079 00080 /*virtual*/ void HTTPFile::setDataType(const char* type) //Internet media type from Content-Type header 00081 { 00082 //Do not really care here 00083 } 00084 00085 /*virtual*/ bool HTTPFile::getIsChunked() //For Transfer-Encoding header 00086 { 00087 return false; 00088 } 00089 00090 /*virtual*/ void HTTPFile::setIsChunked(bool chunked) //For Transfer-Encoding header 00091 { 00092 m_chunked = chunked; 00093 } 00094 00095 /*virtual*/ size_t HTTPFile::getDataLen() //For Content-Length header 00096 { 00097 return m_len; 00098 } 00099 00100 /*virtual*/ void HTTPFile::setDataLen(size_t len) //For Content-Length header, or if the transfer is chunked, next chunk length 00101 { 00102 if(!m_chunked) 00103 m_len = len; //Useful so that we can close file when last byte is written 00104 } 00105 00106 bool HTTPFile::openFile(const char* mode) //true on success, false otherwise 00107 { 00108 if(m_fp) 00109 return true; 00110 00111 m_fp = fopen(m_path.c_str(), mode); 00112 if(m_fp && mode[0]=='r') 00113 { 00114 //Seek EOF to get length 00115 fseek(m_fp, 0, SEEK_END); 00116 m_len = ftell(m_fp); 00117 fseek(m_fp, 0, SEEK_SET); //Goto SOF 00118 } 00119 00120 //DBG("fd = %d\n", m_fp); 00121 00122 if(!m_fp) 00123 return false; 00124 00125 return true; 00126 } 00127 00128 void HTTPFile::closeFile() 00129 { 00130 if(m_fp) 00131 fclose(m_fp); 00132 }
Generated on Wed Jul 13 2022 10:38:55 by 1.7.2