string GetTranslate(string Key) { dict_t::iterator it=Lang.find(Key); if(it==Lang.end()) return string(" Unknown_word "); else return it->second; }
//------------------------------------------------------------------------------ // getFirstTableName is used to take the first Source std::string getFirstTableName(dict_t& dictionary) { std::string firstTable = (*dictionary.begin()).first; int minValue = (*dictionary.begin()).second; for(dict_t::iterator idx = dictionary.begin(); idx != dictionary.end(); ++idx ) { if ((*idx).second < minValue) { minValue = (*idx).second; firstTable = (*idx).first; } } return firstTable; }
CDictionary(const string &i_dictfile) { //Reader=CReadPair(i_dictfile.c_str()); string key,translate; while(Reader.GetPair(key,translate)) { Lang.insert(std::make_pair(key,translate)); } };
//------------------------------------------------------------------------------ void getTablesListAndOccurence(std::vector<connectionLine>& connectionArray, dict_t& tablesDictionary) { bool t1EQt2; for( size_t idx = 0; idx < connectionArray.size(); ++idx ) { connectionLine& CL = connectionArray[idx]; t1EQt2 = (CL.table1 == CL.table2); if(tablesDictionary.find(CL.table1) == tablesDictionary.end() ) tablesDictionary[CL.table1] = 0; if(tablesDictionary.find(CL.table2) == tablesDictionary.end() ) tablesDictionary[CL.table2] = 0; if (!t1EQt2) { tablesDictionary[CL.table1]++; tablesDictionary[CL.table2]++; } } }
int main(int argc, char* argv[]) { std::mt19937 eng(time(NULL)); string_t line; string_t search(argv[1] ? argv[1] : "好好学习"); const int WORD_SIZE = 12; if (argc < 2 || search.size() < WORD_SIZE) { std::cout << "Usage: ./idiom 好好学习" << std::endl; return 1; } m_file.open("dict.txt"); while (m_file >> line) { if (line.size() < WORD_SIZE) continue; string_t lineFirstStr = line.substr(0, 3); if (m_dict.find(lineFirstStr) != m_dict.end()) { m_dict[lineFirstStr].push_back(line); } else { array_t words; words.push_back(line); m_dict[lineFirstStr] = words; } } m_file.close(); string_t searchStr = search.substr(9, 12); std::cout << searchStr << std::endl; if (m_dict.find(searchStr) != m_dict.end()) { std::uniform_int_distribution<int> uniform_int_index(0, m_dict[searchStr].size() - 1); std::cout << m_dict[searchStr][uniform_int_index(eng)] << std::endl; } return 0; }
int Exiv2::http(dict_t& request,dict_t& response,std::string& errors) { if ( !request.count("verb") ) request["verb" ] = "GET"; if ( !request.count("header") ) request["header" ] = "" ; if ( !request.count("version")) request["version"] = "1.0"; if ( !request.count("port") ) request["port" ] = "" ; std::string file; errors = ""; int result = 0; //////////////////////////////////// // Windows specific code #ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); #endif const char* servername = request["server" ].c_str(); const char* page = request["page" ].c_str(); const char* verb = request["verb" ].c_str(); const char* header = request["header" ].c_str(); const char* version = request["version"].c_str(); const char* port = request["port" ].c_str(); const char* servername_p = servername; const char* port_p = port ; std::string url = std::string("http://") + request["server"] + request["page"]; // parse and change server if using a proxy const char* PROXI = "HTTP_PROXY"; const char* proxi = "http_proxy"; const char* PROXY = getenv(PROXI); const char* proxy = getenv(proxi); bool bProx = PROXY || proxy; const char* prox = bProx ? (proxy?proxy:PROXY):""; Exiv2::Uri Proxy = Exiv2::Uri::Parse(prox); // find the dictionary of no_proxy servers const char* NO_PROXI = "NO_PROXY"; const char* no_proxi = "no_proxy"; const char* NO_PROXY = getenv(NO_PROXI); const char* no_proxy = getenv(no_proxi); bool bNoProxy = NO_PROXY||no_proxy; std::string no_prox = std::string(bNoProxy?(no_proxy?no_proxy:NO_PROXY):""); Exiv2::dict_t noProxy= stringToDict(no_prox + ",localhost,127.0.0.1"); // if the server is on the no_proxy list ... ignore the proxy! if ( noProxy.count(servername) ) bProx = false; if ( bProx ) { servername_p = Proxy.Host.c_str(); port_p = Proxy.Port.c_str(); page = url.c_str(); std::string p(proxy?proxi:PROXI); // std::cerr << p << '=' << prox << " page = " << page << std::endl; } if ( !port [0] ) port = "80"; if ( !port_p[0] ) port_p = "80"; //////////////////////////////////// // open the socket int sockfd = (int) socket(AF_INET , SOCK_STREAM,IPPROTO_TCP) ; if ( sockfd < 0 ) return error("unable to create socket\n",NULL,NULL,0) ; // connect the socket to the server int server = -1 ; // fill in the address struct sockaddr_in serv_addr ; int serv_len = sizeof(serv_addr); memset((char *)&serv_addr,0,serv_len); serv_addr.sin_addr.s_addr = inet_addr(servername_p); serv_addr.sin_family = AF_INET ; serv_addr.sin_port = htons(atoi(port_p)); // convert unknown servername into IP address // http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzab6/rzab6uafinet.htm if (serv_addr.sin_addr.s_addr == (unsigned long)INADDR_NONE) { struct hostent* host = gethostbyname(servername_p); if ( !host ) return error("no such host",servername_p,NULL,0); memcpy(&serv_addr.sin_addr,host->h_addr,sizeof(serv_addr.sin_addr)); } makeNonBlocking(sockfd) ; //////////////////////////////////// // and connect server = connect(sockfd, (const struct sockaddr *) &serv_addr, serv_len) ; if ( server == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK ) return error(errors,"error - unable to connect to server = %s port = %s wsa_error = %d",servername_p,port_p,WSAGetLastError()); char buffer[32*1024+1]; size_t buff_l= sizeof buffer - 1 ; //////////////////////////////////// // format the request #ifdef MSDEV_2003 int n = sprintf(buffer,httpTemplate,verb,page,version,servername,header) ; #else int n = snprintf(buffer,buff_l,httpTemplate,verb,page,version,servername,header) ; #endif buffer[n] = 0 ; response["requestheaders"]=std::string(buffer,n); //////////////////////////////////// // send the header (we'll have to wait for the connection by the non-blocking socket) while ( sleep_ >= 0 && send(sockfd,buffer,n,0) == SOCKET_ERROR /* && WSAGetLastError() == WSAENOTCONN */ ) { Sleep(snooze) ; sleep_ -= snooze ; } if ( sleep_ < 0 ) return error(errors,"error - timeout connecting to server = %s port = %s wsa_error = %d",servername,port,WSAGetLastError()); int end = 0 ; // write position in buffer bool bSearching = true ; // looking for headers in the response int status= 200 ; // assume happiness //////////////////////////////////// // read and process the response int err ; n=forgive(recv(sockfd,buffer,(int)buff_l,0),err) ; while ( n >= 0 && OK(status) ) { if ( n ) { end += n ; buffer[end] = 0 ; size_t body = 0 ; // start of body if ( bSearching ) { // search for the body for ( size_t b = 0 ; bSearching && b < lengthof(blankLines) ; b++ ) { if ( strstr(buffer,blankLines[b]) ) { bSearching = false ; body = (int) ( strstr(buffer,blankLines[b]) - buffer ) + strlen(blankLines[b]) ; status = atoi(strchr(buffer,' ')) ; } } // parse response headers char* h = buffer; char C = ':' ; char N = '\n'; int i = 0 ; // initial byte in buffer while(buffer[i] == N ) i++; h = strchr(h+i,N)+1; response[""]=std::string(buffer+i).substr(0,h-buffer-2); result = atoi(strchr(buffer,' ')); char* c = strchr(h,C); char* n = strchr(h,N); while ( c && n && c < n && h < buffer+body ) { std::string key(h); std::string value(c+1); key = key.substr(0,c-h); value = value.substr(0,n-c-1); response[key]=value; h = n+1; c = strchr(h,C); n = strchr(h,N); } } // if the bufffer's full and we're still searching - give up! // this handles the possibility that there are no headers if ( bSearching && buff_l-end < 10 ) { bSearching = false ; body = 0 ; } if ( !bSearching && OK(status) ) { flushBuffer(buffer,body,end,file); } } n=forgive(recv(sockfd,buffer+end,(int)(buff_l-end),0),err) ; if ( !n ) { Sleep(snooze) ; sleep_ -= snooze ; if ( sleep_ < 0 ) n = FINISH ; } } if ( n != FINISH || !OK(status) ) { #ifdef MSDEV_2003 sprintf(buffer,"wsa_error = %d,n = %d,sleep_ = %d status = %d" , WSAGetLastError() , n , sleep_ , status ) ; #else snprintf(buffer,sizeof buffer,"wsa_error = %d,n = %d,sleep_ = %d status = %d" , WSAGetLastError() , n , sleep_ , status ) ; #endif error(errors,buffer,NULL,NULL,0) ; } else if ( bSearching && OK(status) ) { if ( end ) { // we finished OK without finding headers, flush the buffer flushBuffer(buffer,0,end,file) ; } else { return error(errors,"error - no response from server = %s port = %s wsa_error = %d",servername,port,WSAGetLastError()); } } //////////////////////////////////// // close sockets closesocket(server) ; closesocket(sockfd) ; response["body"]=file; return result; }
iterator end() const { return iterator(dict.end()); }
iterator begin() const { return iterator(dict.begin()); }
inline size_t size() const { return dict.size(); }
inline bool contains(const TKey key) const { return dict.contains(key); }
inline bool remove(const TKey key) { return dict.remove(key); }
inline bool insert(const TKey key) { return dict.insert(key, true); }