NextTrain format file library

Files at this revision

API Documentation at this revision

Comitter:
rinosh2
Date:
Thu Nov 18 17:40:15 2010 +0000
Parent:
1:f89f955130c7
Commit message:
Add delim char for option

Changed in this revision

NextTrainFile.cpp Show annotated file Show diff for this revision Revisions of this file
NextTrainFile.h Show annotated file Show diff for this revision Revisions of this file
--- a/NextTrainFile.cpp	Wed Nov 17 16:21:38 2010 +0000
+++ b/NextTrainFile.cpp	Thu Nov 18 17:40:15 2010 +0000
@@ -13,7 +13,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 // NextTrainFile
 
-NextTrainFile::NextTrainFile(const char* ntFile) : m_fp(0), m_shortopt(false) {
+NextTrainFile::NextTrainFile(const char* ntFile) : m_fp(0), m_shortopt(false), m_delim(' ') {
 	if(ntFile) open(ntFile);
 }
 
@@ -35,6 +35,7 @@
 	if(ch <= 'z')	return NextTrainFile::LINE_OPTION;
 	return NextTrainFile::LINE_COMMENT;
 }
+
 int getOptionID(char ch){
 	if(ch <  'A')	return -1;
 	if(ch <= 'Z')	return ch - 'A';
@@ -43,18 +44,17 @@
 	return -1;
 }
 
+const char* WEEK_LIST[] = { // week + holiday
+	"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT", "HOL"
+};
 int getWeekMask(char* str){
 	const char WEEK_DELIM[] = " []\t\r\n";
-	const char* WEEK_LIST[] = { // week + holiday
-		"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN", "HOL"
-	};
 	
 	int ret = 0;
 	char * p = strtok(str, WEEK_DELIM);
 	while(p){
 		for(int index = 0; index < 8 ; ++index){ // week7 + holiday
-//			if(stricmp(p, WEEK_LIST[index]) == 0){
-			if(strcmp(p, WEEK_LIST[index]) == 0){
+			if(strcmp(p, WEEK_LIST[index]) == 0){ // stricmp?
 				ret |= 1 << index;
 				break;
 			}
@@ -110,8 +110,10 @@
 			break;
 		}
 	}
+	printf("NextTrainFile::open return %d, read %d bytes\n", ret, pos);
 	return ret;
 }
+
 NextTrainFile::Status NextTrainFile::close(){
 	if(m_fp){
 		fclose(m_fp); // no error check...
@@ -191,7 +193,10 @@
 					for(; opt < p ; ++opt){
 						// check option format
 						int optID = getOptionID(*opt);
-						if(optID < 0 || m_index[optID] == 0xff) return S_INVALID_OPTION_ID;
+						if(optID < 0 || m_index[optID] == 0xff){
+							printf("NextTrainFile::search S_INVALID_OPTION_ID %x\n", (optID < 0)? -1 : m_index[optID]);
+							return S_INVALID_OPTION_ID;
+						}
 
 						// append option string
 						char* src = m_optbuf + m_index[optID];
@@ -211,12 +216,20 @@
 							len = strlen(src);
 						}
 
-						if(len > left) return S_OPTION_OVERFLOW; // MAX_OPTION is too small...
+						if(len + 1 > left){
+							printf("NextTrainFile::search S_OPTION_OVERFLOW\n");
+							return S_OPTION_OVERFLOW; // MAX_OPTION is too small...
+						}
+						if(dst != m_ni.m_option && m_delim){
+							*dst++ = m_delim;
+							left--;
+						}
 						memcpy(dst, src, len);
 						dst  += len;
 						left -= len;
 					}
 					*dst = 0;
+					printf("NextTrainFile::search %02d:%02d:%02d(%s) hit train %d:%02d\n", st->tm_hour, st->tm_min, st->tm_sec, WEEK_LIST[st->tm_wday], m_ni.m_hour, m_ni.m_min);
 					return S_SUCCESS;
 				}
 			}
@@ -232,7 +245,8 @@
 		}
 	}
 
+	printf("NextTrainFile::search %02d:%02d:%02d(%s) NoTrain\n", st->tm_hour, st->tm_min, st->tm_sec, WEEK_LIST[st->tm_wday]);
+
 	return S_NO_TRAIN; // failed
 }
 
-
--- a/NextTrainFile.h	Wed Nov 17 16:21:38 2010 +0000
+++ b/NextTrainFile.h	Thu Nov 18 17:40:15 2010 +0000
@@ -55,6 +55,7 @@
 	FILE*    m_fp;
 	NextInfo m_ni;
 	bool     m_shortopt;
+	char     m_delim;
 
 	unsigned char m_index [MAX_OPTIONID];
 	char          m_optbuf[MAX_OPTBUF];
@@ -71,6 +72,7 @@
 	Status open(const char* ntFile);
 	Status close();
 	void   use_shortopt(bool f) { m_shortopt = f; }
+	void   set_delim   (char d) { m_delim    = d; }
 	Status search(time_t dt = 0, int offset = 0);
 	const NextInfo* next() const { return &m_ni; }
 };