//version that is robust to more than 3/4 parts in a line //check incremental part by searching "***" #include #include #include #include using namespace std; #define TRIPLE_STRBUF_SIZE 1000000 bool report_err_warn = false;//do not set true for distributed computing //translated from com.hp.hpl.jena.rdf.model.impl.NTripleReader.java char strbuf[TRIPLE_STRBUF_SIZE];//for low-level string: readLang, readURI, readName char strbuf1[TRIPLE_STRBUF_SIZE];//for middle-level string: readLiteral, readResource char strbuf2[TRIPLE_STRBUF_SIZE];//for high-level string: readLiteral, readResource bool stmt_error;//++++ no exit version bool literal_read;//XXXX detect what is just read class statement { public: string subject; string predicate; string object; string context; statement() { context = "";//empty context } //====== assisting functions (called internally) ====== char readChar(const char* & head) { char cur = head[0]; if(cur != '\0') head++; return cur; } inline char nextChar(const char* & head) { return head[0]; } inline bool eol(const char* & head)//end of line { return head[0] == '\0'; } int readNum(const char* & head) { char cur = readChar(head); int pos = 0; while(isdigit(cur)) { strbuf[pos] = cur; pos++; cur = readChar(head); } strbuf[pos]='\0'; return atoi(strbuf); } void skipWhiteSpace(const char* & head) { //input: head[] is a string that ends with '\0' //output: update value of the head pointer "head" to skip white spaces char cur = nextChar(head); const char* old = head; while(cur == ' ' || cur == '\t' || cur == '\r' || cur == '#') {//'\n' is impossible as head[] is part of a line cur = readChar(head); if(eol(head)){ return;//line end is reached } if(cur == '#')//skip comments #... till line end { while(cur != '\0') cur = readChar(head); } } if(head > old) head--; } bool expect(string pattern, const char* & head) { const char* str = pattern.c_str(); int len = pattern.length(); for(int i=0; i TRIPLE_STRBUF_SIZE -1) { if(report_err_warn) cout<<"Error in readName(): strbuf overflows ! Please enlarge TRIPLE_STRBUF_SIZE"< TRIPLE_STRBUF_SIZE -1) { if(report_err_warn) cout<<"Error in readURI(): strbuf overflows ! Please enlarge TRIPLE_STRBUF_SIZE"<", head)) { if(report_err_warn) cout<<"Error in readLiteral(): datatype uri after ^^ not ended by > !"< 0) if(report_err_warn) cout<<"Warning in readLiteral(): Language tags are not permitted on typed literals"<"); return strbuf1; } else return strbuf1; } else if(cur == '\n' || cur == '\r') { if(report_err_warn) cout<<"Error in readLiteral(): Raw \\n and \\r not permitted in N-Triples data !"< !"<"); return strbuf1; } else { if(report_err_warn) cout<<"Error in readResource(): not starting with :_ or < !"<