TrainInfoLib
TrainInfo.cpp@0:475be92c8304, 2010-11-19 (annotated)
- Committer:
- rinosh2
- Date:
- Fri Nov 19 20:39:15 2010 +0000
- Revision:
- 0:475be92c8304
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rinosh2 | 0:475be92c8304 | 1 | /////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 2 | // TrainInfo: Next train and delayed information library by rinos 2010 |
rinosh2 | 0:475be92c8304 | 3 | /////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 4 | |
rinosh2 | 0:475be92c8304 | 5 | #include "TrainInfo.h" |
rinosh2 | 0:475be92c8304 | 6 | #include "HTTPClient.h" |
rinosh2 | 0:475be92c8304 | 7 | |
rinosh2 | 0:475be92c8304 | 8 | //////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 9 | // defines |
rinosh2 | 0:475be92c8304 | 10 | const int MAX_LINE_BUF = 256; |
rinosh2 | 0:475be92c8304 | 11 | |
rinosh2 | 0:475be92c8304 | 12 | //////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 13 | // TrainInfo |
rinosh2 | 0:475be92c8304 | 14 | |
rinosh2 | 0:475be92c8304 | 15 | TrainInfo::TrainInfo(const char* inifile) { |
rinosh2 | 0:475be92c8304 | 16 | m_delay[0] = 0; |
rinosh2 | 0:475be92c8304 | 17 | if(inifile) open(inifile); |
rinosh2 | 0:475be92c8304 | 18 | } |
rinosh2 | 0:475be92c8304 | 19 | |
rinosh2 | 0:475be92c8304 | 20 | TrainInfo::~TrainInfo(){ |
rinosh2 | 0:475be92c8304 | 21 | close(); |
rinosh2 | 0:475be92c8304 | 22 | } |
rinosh2 | 0:475be92c8304 | 23 | |
rinosh2 | 0:475be92c8304 | 24 | //////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 25 | // internal funcs |
rinosh2 | 0:475be92c8304 | 26 | //////////////////////////////////////////////////////////////////////////////// |
rinosh2 | 0:475be92c8304 | 27 | // Open NextTrain format file and read option data |
rinosh2 | 0:475be92c8304 | 28 | TrainInfo::Status TrainInfo::open(const char* inifile){ |
rinosh2 | 0:475be92c8304 | 29 | if(m_ini.open(inifile)) return S_INI_OPEN_ERROR; |
rinosh2 | 0:475be92c8304 | 30 | |
rinosh2 | 0:475be92c8304 | 31 | // Set parameters from INI file |
rinosh2 | 0:475be92c8304 | 32 | char tbl[MAX_LINE_BUF]; |
rinosh2 | 0:475be92c8304 | 33 | if(m_ini.get("NextTrain", tbl, sizeof(tbl))) return S_INI_FORMAT_ERROR; |
rinosh2 | 0:475be92c8304 | 34 | |
rinosh2 | 0:475be92c8304 | 35 | // Setup NextTrain information |
rinosh2 | 0:475be92c8304 | 36 | if(m_ntf.open(tbl)) return S_NEXTTRAIN_OPEN_ERROR; |
rinosh2 | 0:475be92c8304 | 37 | |
rinosh2 | 0:475be92c8304 | 38 | char delim[2]; |
rinosh2 | 0:475be92c8304 | 39 | if(m_ini.get("DelimChar", delim, 2) == S_SUCCESS) m_ntf.set_delim(delim[0]); |
rinosh2 | 0:475be92c8304 | 40 | return S_SUCCESS; |
rinosh2 | 0:475be92c8304 | 41 | } |
rinosh2 | 0:475be92c8304 | 42 | |
rinosh2 | 0:475be92c8304 | 43 | TrainInfo::Status TrainInfo::close(){ |
rinosh2 | 0:475be92c8304 | 44 | m_ntf.close(); |
rinosh2 | 0:475be92c8304 | 45 | return S_SUCCESS; |
rinosh2 | 0:475be92c8304 | 46 | } |
rinosh2 | 0:475be92c8304 | 47 | |
rinosh2 | 0:475be92c8304 | 48 | // Delay information I/F |
rinosh2 | 0:475be92c8304 | 49 | const char INET_TEMP_FILE[] = "/local/temp1.dat"; |
rinosh2 | 0:475be92c8304 | 50 | |
rinosh2 | 0:475be92c8304 | 51 | TrainInfo::Status TrainInfo::checkDelay(){ |
rinosh2 | 0:475be92c8304 | 52 | m_delay[0] = 0; |
rinosh2 | 0:475be92c8304 | 53 | |
rinosh2 | 0:475be92c8304 | 54 | // check ini configuration |
rinosh2 | 0:475be92c8304 | 55 | int delayType = 0; |
rinosh2 | 0:475be92c8304 | 56 | char buf[MAX_LINE_BUF]; |
rinosh2 | 0:475be92c8304 | 57 | if(m_ini.get("DelayType", delayType) || delayType == DTYPE_NONE) return S_DELAY_NOINFO; // no delay information |
rinosh2 | 0:475be92c8304 | 58 | if(delayType >= DTYPE_MAX) return S_DELAY_TYPE_ERROR; |
rinosh2 | 0:475be92c8304 | 59 | if(m_ini.get("DelayURL", buf, sizeof(buf))) return S_INI_FORMAT_ERROR; |
rinosh2 | 0:475be92c8304 | 60 | |
rinosh2 | 0:475be92c8304 | 61 | if(!memcmp(buf, "http:", 5)){ |
rinosh2 | 0:475be92c8304 | 62 | //#define DIRECT_READ 1 |
rinosh2 | 0:475be92c8304 | 63 | #ifdef DIRECT_READ |
rinosh2 | 0:475be92c8304 | 64 | HTTPFile cache(INET_TEMP_FILE); |
rinosh2 | 0:475be92c8304 | 65 | HTTPResult ret = http.get(buf, &cache); |
rinosh2 | 0:475be92c8304 | 66 | #else |
rinosh2 | 0:475be92c8304 | 67 | FILE* fp = fopen(INET_TEMP_FILE, "w"); |
rinosh2 | 0:475be92c8304 | 68 | if(!fp) return S_DELAY_WRITE_ERROR; |
rinosh2 | 0:475be92c8304 | 69 | HTTPClient http; |
rinosh2 | 0:475be92c8304 | 70 | int ret = http.get(buf, fp); |
rinosh2 | 0:475be92c8304 | 71 | fclose(fp); |
rinosh2 | 0:475be92c8304 | 72 | printf("HTTPClient download %d bytes\n", ret); |
rinosh2 | 0:475be92c8304 | 73 | #endif |
rinosh2 | 0:475be92c8304 | 74 | if(!ret){ |
rinosh2 | 0:475be92c8304 | 75 | printf("HTTPClient::get failed %d\n", ret); |
rinosh2 | 0:475be92c8304 | 76 | return S_DELAY_DOWNLOAD_ERROR; |
rinosh2 | 0:475be92c8304 | 77 | } |
rinosh2 | 0:475be92c8304 | 78 | strcpy(buf, INET_TEMP_FILE); |
rinosh2 | 0:475be92c8304 | 79 | } |
rinosh2 | 0:475be92c8304 | 80 | |
rinosh2 | 0:475be92c8304 | 81 | FILE* fp = fopen(buf, "rb"); |
rinosh2 | 0:475be92c8304 | 82 | if(!fp) return S_DELAY_WRITE_ERROR; |
rinosh2 | 0:475be92c8304 | 83 | |
rinosh2 | 0:475be92c8304 | 84 | int end = 0; |
rinosh2 | 0:475be92c8304 | 85 | while(!end && fgets(buf, sizeof(buf), fp)){ |
rinosh2 | 0:475be92c8304 | 86 | switch(delayType){ |
rinosh2 | 0:475be92c8304 | 87 | case DTYPE_TOKYU: // TOKYU TOP PAGE MODE |
rinosh2 | 0:475be92c8304 | 88 | { |
rinosh2 | 0:475be92c8304 | 89 | char* p = strchr(buf, '\''); |
rinosh2 | 0:475be92c8304 | 90 | if(p){ |
rinosh2 | 0:475be92c8304 | 91 | char* p2 = strrchr(++p, '\''); |
rinosh2 | 0:475be92c8304 | 92 | if(p2){ // detect! |
rinosh2 | 0:475be92c8304 | 93 | // buf is SJIS |
rinosh2 | 0:475be92c8304 | 94 | *p2 = 0; |
rinosh2 | 0:475be92c8304 | 95 | IniFile::strtrim(m_delay, p, sizeof(m_delay)); |
rinosh2 | 0:475be92c8304 | 96 | p = strstr(m_delay, "\0x95\0xBD\0x8F\0xED"); |
rinosh2 | 0:475be92c8304 | 97 | printf("DELAY %d='%s'\n", p? 1 : 0, m_delay); |
rinosh2 | 0:475be92c8304 | 98 | fclose(fp); |
rinosh2 | 0:475be92c8304 | 99 | return p? S_DELAY_NONE : S_DELAY_DETECTED; // SJIS-"HEIJO" |
rinosh2 | 0:475be92c8304 | 100 | } |
rinosh2 | 0:475be92c8304 | 101 | } |
rinosh2 | 0:475be92c8304 | 102 | } |
rinosh2 | 0:475be92c8304 | 103 | break; |
rinosh2 | 0:475be92c8304 | 104 | } |
rinosh2 | 0:475be92c8304 | 105 | } |
rinosh2 | 0:475be92c8304 | 106 | fclose(fp); |
rinosh2 | 0:475be92c8304 | 107 | // if message exist |
rinosh2 | 0:475be92c8304 | 108 | printf("DELAY: NoInfo\n"); |
rinosh2 | 0:475be92c8304 | 109 | return S_DELAY_NOINFO; |
rinosh2 | 0:475be92c8304 | 110 | } |
rinosh2 | 0:475be92c8304 | 111 | |
rinosh2 | 0:475be92c8304 | 112 | TrainInfo::Status TrainInfo::getDelayMessage(char* buf, int size){ |
rinosh2 | 0:475be92c8304 | 113 | int len = strlen(m_delay); |
rinosh2 | 0:475be92c8304 | 114 | if(len > size - 1) len = size - 1; |
rinosh2 | 0:475be92c8304 | 115 | memcpy(buf, m_delay, len); |
rinosh2 | 0:475be92c8304 | 116 | buf[len] = 0; |
rinosh2 | 0:475be92c8304 | 117 | return S_SUCCESS; |
rinosh2 | 0:475be92c8304 | 118 | } |