/* bool KDirectory::isDirEmpty( const KData& fullDir ) { KData dtDir = fullDir; dtDir.makePath( true ); DIR* dir; if ( (dir=opendir(dtDir.getData())) == NULL ) return false; struct dirent *pDirent; while( ( pDirent = readdir(dir) ) != NULL ) { struct stat statbuf; KData dtPath = dtDir; dtPath += pDirent->d_name; if ( stat(dtPath.getData(),&statbuf) == -1 ) continue; if ( S_ISDIR(statbuf.st_mode) ) { if ( pDirent->d_name && strcmp(pDirent->d_name,".") && strcmp(pDirent->d_name,"..") ) { if ( !isDirEmpty(dtPath) ) { closedir( dir ); return false; } } } else { closedir( dir ); return false; } } closedir( dir ); return true; } */ bool KDirectory::open(const KData& directory, bool create) { if (directory.isEmpty()) { return false; } KData dir = directory; int pos; KData dtKData = "\\"; while ((pos = dir.find(dtKData)) != -1) { dir.replace(pos, 1, "/"); } _directory.erase(); bool bDirExist = isDirectoryExist(dir); if (!bDirExist) { if (create) { if (!createDir(dir)) return false; } else { return false; } } _directory = dir; _directory.makePath(); return true; }
bool KDirectory::rename(const KData& from, const KData& to) { if (rename(from.getData(), to.getData()) == 0) return true; else return false; }
void vCpLog( int pri, const char* file, int line, const char* fmt, va_list ap ) { assert( pri >= 0 && pri <= LAST_PRIORITY ); char datebuf[ DATEBUF_SIZE ]; memset(datebuf, 0, DATEBUF_SIZE); int datebufCharsRemaining; struct timeval tv; struct timezone tz; int result = gettimeofday( &tv, &tz ); if( result == -1 ) { datebuf [0] = '\0'; } else { const time_t timeInSeconds = (time_t) tv.tv_sec; strftime( datebuf, DATEBUF_SIZE, "%m-%d %H:%M:%S", localtime ( &timeInSeconds ) ); } char msbuf[10]; sprintf( msbuf, ".%3.3d", (int)(tv.tv_usec/1000) ); datebufCharsRemaining = DATEBUF_SIZE - strlen( datebuf ); strncat( datebuf, msbuf, datebufCharsRemaining - 1 ); datebuf[DATEBUF_SIZE - 1] = '\0'; if( bConnServer ) { char outBuff[2048]; memset(outBuff, 0, 2048); KData ret = "["; ret = ret + KData(datebuf) + KData("]") + KData(priNameShort[pri]) + KData(" "); vsprintf( outBuff, fmt, ap ); ret += outBuff; LogPack *pLogPack = (LogPack*)outBuff; pLogPack->lLogMsgSize = htonl( ret.length() ) ; strcpy( pLogPack->strLogMsg, ret.getDataBuf() ); spClientSock->transmit( (char*)pLogPack, sizeof( long )+ret.length() ); } if( mainLogFunc == NULL ) { fprintf( cpLogFd, "[%s] %s: ", datebuf, priNameShort[pri] ); vfprintf( cpLogFd, fmt, ap ); fprintf( cpLogFd, "\n" ); fflush( cpLogFd ); } else { char outBuff[2048]; memset(outBuff, 0, 2048); KData ret = "["; ret = ret + KData(datebuf) + "]" + priNameShort[pri] + " "; vsprintf( outBuff, fmt, ap ); ret += outBuff; mainLogFunc( ret, pri ); } }
bool AnimalFeed::calculate(const KCalculationInfo& ci, const KLocation & loc, KDataArray * calcResult) { Q_UNUSED(loc); Q_UNUSED(ci); qreal fp = userInputs()->numericValueOf(Srs19::AnnualPastureFraction); KData Cvi = _inpPorts.at(0)->data(Srs19::ConcentrationInVegetation); KData Cpi = _inpPorts.at(1)->data(Srs19::ConcentrationInStoredAnimalFeed); if (Cvi.isValid()) { for(int k = 0; k < Cvi.count(); k++) { const KDataItem & CviItem = Cvi.at(k); qreal vCvi = CviItem.numericValue(); qreal vCpi = Cpi.numericValue(CviItem.name()); qreal Cai = fp * vCvi + (1-fp)*vCpi; calcResult->appendOrMerge(&Srs19::ConcentrationInAnimalFeed, CviItem.name(), Cai, KData::Radionuclide); } } else if (Cpi.isValid()) { for(int k = 0; k < Cpi.count(); k++) { const KDataItem & CpiItem = Cvi.at(k); qreal vCvi = Cvi.numericValue(CpiItem.name()); qreal vCpi = CpiItem.numericValue(); qreal Cai = fp * vCvi + (1-fp)*vCpi; calcResult->appendOrMerge(&Srs19::ConcentrationInAnimalFeed, CpiItem.name(), Cai, KData::Radionuclide); } } return true; }
void KFile::replaceAll(KData& str) { int pos; KData dtKData = "\\"; while ((pos = str.find(dtKData)) != -1) { str.replace(pos, 1, "/"); } }
int KFile::write(const KData& data) { const char* pKData = data.getDataBuf(); int len = data.length(); int iWrited = 0; while (iWrited < len) { int iRet = write((unsigned char*) (pKData + iWrited), len - iWrited); if (iRet <= 0) break; iWrited += iRet; } return iWrited; }
bool KFile::deleteFile(const KData& fullFilePath) { if (remove(fullFilePath.getData()) == 0) return true; else return false; }
bool KDirectory::chDir(const KData& dir) { if (chdir(dir.getData()) == 0) return true; else return false; }
bool KFile::setFile(KData fullFilePath, int mode) { if (m_bIsOpen) { fclose (m_hFile); m_bIsOpen = false; } #ifndef WIN32 replaceAll(fullFilePath); #endif if (fullFilePath.find("/") == -1) //斜杠…… { m_kFilePath = "./"; m_kFileName = fullFilePath; } else { KData tStr = fullFilePath; if (tStr[tStr.length() - 1] == '/') { return false; } else { int pos = tStr.findlast("/"); m_kFilePath = tStr.substr(0, pos + 1); m_kFileName = tStr.substr(pos + 1, tStr.length() - pos - 1); } } return open(mode); }
bool KDirectory::createDir(const KData& dir) { int pos = 0; while (true) { if ((pos = dir.find("/", pos)) == -1) pos = dir.length(); KData dtDir = dir.substr(0, pos); pos++; if (!dtDir.isEmpty()) { if (!isDirectoryExist(dtDir)) { /* if ( -1 == mkdir(dtDir.getData(),S_IRWXU|S_IRWXG|S_IRWXO) ) { return false; } */ if (MKDIR(dtDir.getData()) != 0) { return false; } } } if (pos >= (int) dir.length()) break; } return true; }
int KHttp::getHttpFile(const KData& fullUrl, const KData& savefile, int startpos) { string strPath = fullUrl; Trim(strPath); KData dtServer; KData dtFile; KData kDTFullUrl = strPath; LOGD("kDTFullUrl is %s",kDTFullUrl.getDataBuf()); if (isEqualNoCase(kDTFullUrl.substr(0, 7), "http://")) { kDTFullUrl = kDTFullUrl.substr(7); } int nPos = kDTFullUrl.find("/"); if (nPos == -1) { LOGERROR("nPos = -1"); return 0; } else { dtServer = kDTFullUrl.substr(0, nPos); dtFile = kDTFullUrl.substr(nPos); } return getHttpFile(dtServer, dtFile, savefile, startpos); }
void DischargeItemTable::setData(const KData& d) { if (d.isEmpty()) return; //set row count this->setRowCount(d.count()+1); //setup data for(int k = 0; k < d.count(); k++) { const KDataItem &item = d.at(k); QTableWidgetItem * cell = new QTableWidgetItem(item.name()); setItem(k, 0, cell); cell = new QTableWidgetItem(item.value().toString()); setItem(k, 1, cell); } }
bool KDirectory::isExcludeFile(const KData& file) { for (vector<KData>::iterator iter = _vecExcludeFile.begin(); iter != _vecExcludeFile.end(); iter++) { if (file.isEndWith(*iter, false)) return true; } return false; }
bool KDirectory::isDirectoryExist(const KData& dir) { struct stat statbuf; if (stat(dir.getData(), &statbuf) == -1) return false; if (S_ISDIR(statbuf.st_mode)) return true; else return false; }
double TechUtils::CalulateAMA(const std::vector<KData>& data, const KData& current, size_t mins){ double totalExchangePrice = current.TurnOver(); long long totalVolume = current.Volume(); long long leftedge = current.Timestamp() - mins * 60 - 1; for (auto it = data.rbegin(); it != data.rend(); it++) { if (it->Timestamp() > leftedge){ totalExchangePrice += it->TurnOver(); totalVolume += it->Volume(); } else{ break; } } //assert(totalVolume != 0); //assert(totalExchangePrice >= 0.0); return totalExchangePrice / totalVolume; }
bool KDirectory::createFileDir(const KData& dir) { int pos = 0; while (true) { if ((pos = dir.find("/", pos)) == -1) break; KData dtDir = dir.substr(0, pos); pos++; if (!dtDir.isEmpty()) { if (!isDirectoryExist(dtDir)) { if (MKDIR(dtDir.getData()) != 0) { return false; } } } } return true; }
double TechUtils::CalulateMA(const std::vector<KData>& data, const KData& current, size_t mins){ //datetime to timestamp double totalExchangeLastPrice = current.LastPrice(); long long count = 1; long long leftedge = current.Timestamp() - mins * 60 - 1; for (auto it = data.rbegin(); it != data.rend(); it++) { if (it->Timestamp() > leftedge){ totalExchangeLastPrice += it->LastPrice(); ++count; } else{ break; } } //assert(totalVolume != 0); //assert(totalExchangePrice >= 0.0); return totalExchangeLastPrice / count; }
void ConstantValue::_calculate(const Indicator& data) { double value = getParam<double>("value"); int discard = getParam<int>("discard"); size_t total = 0; if (isLeaf()) { //叶子节点 KData k = getContext(); if (k.getStock().isNull()) { _readyBuffer(1, 1); if (discard < 1) { m_discard = 0; _set(value, 0, 0); } else { m_discard = 1; } return; } total = k.size(); if (0 == total) { return; } _readyBuffer(total, 1); } else { //非叶子节点 total = data.size(); discard = data.discard() > discard ? data.discard() : discard; } m_discard = discard > total ? total : discard; for (size_t i = m_discard; i < total; ++i) { _set(value, i, 0); } }
void KNetworkAddress::setHostName( const KData& theAddress ) { const char* cstrAddress; KData sAddress, sPort; unsigned int length = theAddress.length(); unsigned int colonPos = length; cstrAddress = theAddress.getData(); for ( int i = 0; *cstrAddress != '\0'; cstrAddress++, i++ ) { if ( *cstrAddress == ':' ) { colonPos = i; break; } } if ( colonPos != length ) { sAddress = theAddress.substr( 0, colonPos ); sPort = theAddress.substr( colonPos+1, length-colonPos-1 ); colonPos = atoi( sPort.getData() ); if ( colonPos ) { aPort = colonPos; } else { } } else // No ':' in input string; { sAddress = theAddress; } rawHostName = sAddress; ipAddressSet = false; }
int KHttp::getHttpFile(const KData& kServer, const KData& kHttpFile, const KData& kSaveFile, int nStartPos) { LOGD("Entry getHttpFile"); m_dtHttpServer = kServer; m_dtHttpFile = kHttpFile; m_bChunked = false; m_iWriteLen = nStartPos; m_iLength = 0; m_clientSock.close(); m_kCnnect.close(); _respHeadMap.clear(); m_iNotifyPos = 0; m_iNotifyGap = 0; m_iContentLength = 0; m_iStatusCode = 0; bool bGet = (_paramMap.size() == 0); KFile kFile; KData kHttpRequest; KData kDataPost; if (bGet) { kHttpRequest = "GET "; } else { kHttpRequest = "POST "; map<KData, KData>::iterator iter; for (iter = _paramMap.begin(); iter != _paramMap.end(); iter++) { if (iter != _paramMap.begin()) kDataPost += "&"; kDataPost += iter->first; kDataPost += "="; kDataPost += iter->second; } } kHttpRequest += kHttpFile; kHttpRequest += " HTTP/1.1"; kHttpRequest += CRLF; kHttpRequest += "Host: "; kHttpRequest += kServer; kHttpRequest += CRLF; kHttpRequest += "Accept: */*"; kHttpRequest += CRLF; if (!m_dtUserAgent.isEmpty()) { kHttpRequest += "User-Agent: "; kHttpRequest += m_dtUserAgent; kHttpRequest += CRLF; } if (nStartPos > 0) { kHttpRequest += "RANGE: bytes="; kHttpRequest += nStartPos; kHttpRequest += "-"; kHttpRequest += CRLF; } kHttpRequest += "Pragma: no-cache"; kHttpRequest += CRLF; kHttpRequest += "Cache-Control: no-cache"; kHttpRequest += CRLF; kHttpRequest += "Connection: close"; kHttpRequest += CRLF; if (!bGet) { kHttpRequest += "Content-Type: application/x-www-form-urlencoded"; kHttpRequest += CRLF; kHttpRequest += "Content-Length: "; kHttpRequest += KData((int) kDataPost.length()); kHttpRequest += CRLF; } kHttpRequest += CRLF; char szBuffer[MTU] = {0}; if (m_dtHttpProxy.isEmpty()) { LOGD("Connect to Server: %s", kServer.getData() ); m_clientSock.setServer(kServer, 80); } else { LOGD("Connect to Server: %s", m_dtHttpProxy.getData() ); m_clientSock.setServer(m_dtHttpProxy, 80); } m_clientSock.initSocket(); if (!m_clientSock.connect()) { LOGERROR("m_clientSock.connect() failed"); return -1; } m_kCnnect = m_clientSock.getConn(); m_kCnnect.setTimeout(m_timeout); if (m_kCnnect.writeData(kHttpRequest) != (int) kHttpRequest.length()) { LOGERROR(" m_conn.writeData(httpRequest) != (int)httpRequest.length() failed"); return -1; } m_iWritedBytes += kHttpRequest.length(); if (!bGet) { if (m_kCnnect.writeData(kDataPost) != (int) kDataPost.length()) { LOGERROR("m_conn.writeData(dtPost) != (int)dtPost.length() failed"); return -1; } m_iWritedBytes += kDataPost.length(); } int iRead = 0; m_iStatusCode = 0; bool bRun = true; memset(szBuffer, 0, MTU); if ((iRead = m_kCnnect.readLine(szBuffer, MTU)) <= 0) { LOGERROR( "Read command line err: %d", iRead ); m_iReadedBytes += iRead; return 0; } KData dtKData; KData dtLine(szBuffer, iRead); if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { LOGERROR("Read command line mactch space err" ); return 0; } if (dtKData != "HTTP/1.1" && dtKData != "HTTP/1.0") { LOGERROR( "GET HTTP HEAD ERR" ); return 0; } if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { LOGERROR("Read command line mactch space 2 err" ); return 0; } m_iStatusCode = (int)dtKData; LOGD("Ready to while ( (iRead = m_conn.readLine(buff,MTU)) > 0 )"); while ((iRead = m_kCnnect.readLine(szBuffer, MTU)) > 0) { m_iReadedBytes += iRead; KData dtLine(szBuffer, iRead); KData dtBefVal; if (FOUND == dtLine.match(":", &dtBefVal, true)) { dtBefVal.removeSpaces(); dtLine.removeSpaces(); if (isEqualNoCase(dtBefVal, "Content-Length")) { m_iLength = (int) dtLine + nStartPos; if (m_iNotifyPercent > 0 && m_iNotifyPercent <= 100) { m_iNotifyPos = m_iNotifyGap = m_iLength / (100 / m_iNotifyPercent); } } else if (isEqualNoCase(dtBefVal, "Transfer-Encoding")) { if (isEqualNoCase(dtLine, "chunked")) { m_bChunked = true; } } _respHeadMap[dtBefVal] = dtLine; } } if (iRead < 0) { LOGERROR("read err" ); return -1; } if (m_iStatusCode != 200 && m_iStatusCode != 206) { if (m_iStatusCode == 302) { KData dtRedirectUrl = getRespFieldValue("Location"); LOGD("Ready to return getHttpFile( dtRedirectUrl, savefile, startpos );"); return getHttpFile(dtRedirectUrl, kSaveFile, nStartPos); } LOGERROR(" m_iStatusCode!=200 && m_iStatusCode!=206"); return 0; } FILE* pkFile = 0; string strFile = kSaveFile.getData(); if (nStartPos <= 0) { pkFile = fopen(strFile.c_str(),"wb+"); } else { pkFile = fopen(strFile.c_str(), "wb+"); fclose(pkFile); pkFile = fopen(strFile.c_str(),"r+b"); if (0 == pkFile) { return -1; } fseek(pkFile, nStartPos, SEEK_SET); } if (m_bChunked) { LOGD("m_bChunked is true!"); unsigned char* pBuff = new unsigned char[MTU]; int iBuffLen = MTU; while ((iRead = m_kCnnect.readLine(szBuffer, MTU)) > 0) { m_iReadedBytes += iRead; if (iRead > 8) { return -1; } int nLength = KData(szBuffer, iRead).HexToInt(); if (nLength <= 0) { delete [] pBuff; return m_iWriteLen; } if (nLength > iBuffLen) { delete[] pBuff; iBuffLen = nLength; pBuff = new unsigned char[iBuffLen]; } int iReaded = 0; memset(pBuff, 0, nLength); m_kCnnect.readData(pBuff, nLength, iReaded); m_iReadedBytes += iReaded; kFile.write(pBuff, iReaded); m_iWriteLen += iReaded; if (m_iLength > 0) { if (m_iWriteLen >= m_iLength) { if (m_iNotifyGap > 0) { m_pNotifyCallback(m_pNotifyParam, 100, m_iWriteLen, m_iLength); } break; } if (m_iNotifyGap > 0 && m_iWriteLen > m_iNotifyPos) { int nPercent = int((m_iWriteLen / (float) m_iLength) * 100); m_iNotifyPos += m_iNotifyGap; m_pNotifyCallback(m_pNotifyParam, nPercent, m_iWriteLen, m_iLength); } } if (iReaded != nLength) { delete[] pBuff; return m_iWriteLen; } if (m_kCnnect.readLine(szBuffer, MTU) != 0) { return m_iWriteLen; } } delete[] pBuff; return m_iWriteLen; } else { string strFile = kSaveFile.getData(); while ((iRead = m_kCnnect.readn(szBuffer, MTU)) > 0 && bRun) { m_iReadedBytes += iRead; unsigned int uiWriteSize = fwrite((unsigned char*) szBuffer,1,iRead,pkFile); //kFile.write((unsigned char*) szBuffer, iRead); m_iWriteLen += iRead; if (m_iLength > 0) { if (m_iWriteLen >= m_iLength) { if (m_iNotifyGap > 0) { m_pNotifyCallback(m_pNotifyParam, 100, m_iWriteLen, m_iLength); } break; } if (m_iNotifyGap > 0 && m_iWriteLen > m_iNotifyPos) { int nPercent = int((m_iWriteLen / (float) m_iLength) * 100); m_iNotifyPos += m_iNotifyGap; m_pNotifyCallback(m_pNotifyParam, nPercent, m_iWriteLen, m_iLength); } } while (m_bPause) { vsleep(1000); } } //fclose(pkFile); } fclose(pkFile); m_kCnnect.close(); kFile.closeFile(); LOGD("return m_iWriteLen;"); return m_iWriteLen; }
bool KNetworkAddress::is_valid_ip_addr( const KData& addr, char* retaddr ) { unsigned long maskcheck = ~255; const char *advancer = addr.getData(); unsigned long octet; char *nextchar; char ipAddr[ IPV4_LENGTH ]; if ( ( *(advancer) == 0 ) || (*(advancer) == ' ') || (*(advancer) == '\t') ) { return false; } octet = strtoul( advancer, &nextchar, 10 ); if( (*nextchar != '.') || (octet & maskcheck) || (octet == 0) ) { return false; } assert( octet < 256 ); ipAddr[0] = (char)octet; advancer = nextchar + 1; if( (*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t') ) { return false; } octet = strtoul( advancer, &nextchar, 10 ); if( (*nextchar != '.') || (octet & maskcheck) ) { return false; } assert( octet < 256 ); ipAddr[1] = (char)octet; advancer = nextchar+1; if( (*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t') ) { return false; } octet = strtoul(advancer, &nextchar, 10); if((*nextchar != '.') || (octet & maskcheck)) { return false; } assert( octet < 256 ); ipAddr[2] = (char)octet; advancer = nextchar+1; if ((*(advancer) == 0) || (*(advancer) == ' ') || (*(advancer) == '\t')) { return false; } octet = strtoul(advancer, &nextchar, 10); if ( (*nextchar) || (octet & maskcheck) || (octet == 0) ) { return false; } assert( octet < 256 ); ipAddr[3] = (char)octet; if ( NULL != retaddr ) { memcpy( retaddr, ipAddr, IPV4_LENGTH ); } return true; }
int KHttp::getHttpFileLength(const KData & fullUrl) { KData server; KData httpFile; KData dtFullUrl = fullUrl; if (isEqualNoCase(dtFullUrl.substr(0, 7), "http://")) { dtFullUrl = dtFullUrl.substr(7); } int pos = dtFullUrl.find("/"); if (pos == -1) { return -1; } else { server = dtFullUrl.substr(0, pos); httpFile = dtFullUrl.substr(pos); } m_bChunked = false; m_iWriteLen = 0; m_iLength = 0; m_clientSock.close(); m_kCnnect.close(); _respHeadMap.clear(); m_iNotifyPos = 0; m_iNotifyGap = 0; m_iContentLength = 0; m_iStatusCode = 0; bool bGet = (_paramMap.size() == 0); KFile file; KData httpRequest; KData dtPost; if (bGet) { httpRequest = "GET "; } else { httpRequest = "POST "; map<KData, KData>::iterator iter; for (iter = _paramMap.begin(); iter != _paramMap.end(); iter++) { if (iter != _paramMap.begin()) dtPost += "&"; dtPost += iter->first; dtPost += "="; dtPost += iter->second; } } httpRequest += httpFile; httpRequest += " HTTP/1.1"; httpRequest += CRLF; httpRequest += "Host: "; httpRequest += server; httpRequest += CRLF; httpRequest += "Accept: */*"; httpRequest += CRLF; if (!m_dtUserAgent.isEmpty()) { httpRequest += "User-Agent: "; httpRequest += m_dtUserAgent; httpRequest += CRLF; } httpRequest += "Pragma: no-cache"; httpRequest += CRLF; httpRequest += "Cache-Control: no-cache"; httpRequest += CRLF; httpRequest += "Connection: close"; httpRequest += CRLF; if (!bGet) { httpRequest += "Content-Type: application/x-www-form-urlencoded"; httpRequest += CRLF; httpRequest += "Content-Length: "; httpRequest += KData((int) dtPost.length()); httpRequest += CRLF; } httpRequest += CRLF; char buff[MTU] = { 0 }; m_clientSock.setServer(server, 80); if (!m_clientSock.connect()) { return 0; } m_kCnnect = m_clientSock.getConn(); m_kCnnect.setTimeout(m_timeout); if (m_kCnnect.writeData(httpRequest) != (int) httpRequest.length()) return 0; m_iWritedBytes += httpRequest.length(); if (!bGet) { if (m_kCnnect.writeData(dtPost) != (int) dtPost.length()) return 0; m_iWritedBytes += dtPost.length(); } int iRead; m_iStatusCode = 0; memset(buff, 0, MTU); if ((iRead = m_kCnnect.readLine(buff, MTU)) <= 0) { m_iReadedBytes += iRead; return 0; } KData dtKData; KData dtLine(buff, iRead); if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { return 0; } if (dtKData != "HTTP/1.1" && dtKData != "HTTP/1.0") { return 0; } if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { return 0; } m_iStatusCode = (int) dtKData; while ((iRead = m_kCnnect.readLine(buff, MTU)) > 0) { m_iReadedBytes += iRead; KData dtLine(buff, iRead); KData dtBefVal; if (FOUND == dtLine.match(":", &dtBefVal, true)) { dtBefVal.removeSpaces(); dtLine.removeSpaces(); if (isEqualNoCase(dtBefVal, "Content-Length")) { return (int) dtLine; } } } return 0; }
bool KFile::renameFile(const KData& from, const KData& to, bool replace) { return rename(from.getData(), to.getData()); }
int KHttp::getHttpFileMemory(const KData& server, const KData& httpfile, char** ppKData) { char* pKData = NULL; KData httpRequest = "GET "; httpRequest += httpfile; httpRequest += " HTTP/1.1"; httpRequest += CRLF; httpRequest += "Host: "; httpRequest += server; httpRequest += CRLF; httpRequest += CRLF; unsigned char buff[MTU]; KTcpClientSocket clientSock(server, 80); clientSock.connect(); KConnection &conn = clientSock.getConn(); string stringTmp = httpRequest.getData(); conn.writeData(stringTmp); int iRead = 0; bool bFound = false; KData dtHead; int iLength = 0; int iWrite = 0; int m_iStatusCode = 0; bool bRun = true; while ((iRead = conn.readn(buff, MTU)) > 0 && bRun) { if (!bFound) { for (int i = 0; i < iRead - 4; i++) { if (buff[i] == 0x0d && buff[i + 1] == 0x0a && buff[i + 2] == 0x0d && buff[i + 3] == 0x0a) { KData dtLine; dtHead = KData((char*) buff, i + 4); KData dtRequestLine, dtCode; if (NOT_FOUND == dtHead.match(CRLF, &dtRequestLine, false)) { return -1; } if (NOT_FOUND == dtRequestLine.match("HTTP/1.1", NULL, true) && NOT_FOUND == dtRequestLine.match("HTTP/1.0", NULL, true)) { return -1; } if (NOT_FOUND == dtRequestLine.match(SP, NULL, true)) { return -1; } if (NOT_FOUND == dtRequestLine.match(SP, &dtCode, true)) { return -1; } if ((int) dtCode == 0 && (int) dtCode != 200) { return -1; } while (NOT_FOUND != dtHead.match(CRLF, &dtLine, true)) { KData dtBefVal; if (FOUND == dtLine.match(":", &dtBefVal, true)) { dtBefVal.removeSpaces(); if (isEqualNoCase(dtBefVal, "Content-Length")) { dtLine.removeSpaces(); iLength = (int) dtLine; } } else if (NOT_FOUND != dtLine.match("HTTP/1.1", NULL, true)) { dtLine.removeSpaces(); dtLine.match(" ", &dtBefVal, true); m_iStatusCode = (int) dtBefVal; } } if (iLength <= 0) { return -1; } else if (m_iStatusCode != 200) { return -1; } else { pKData = new char[iLength]; } memcpy(pKData, &buff[i + 4], iRead - i - 4); iWrite += (iRead - i - 4); bFound = true; if (iLength > 0 && iWrite == iLength) { bRun = false; break; } } } } else { memcpy(&pKData[iWrite], buff, iRead); iWrite += iRead; if (iWrite == iLength) { break; } } } conn.close(); *ppKData = pKData; return iWrite; }
Indicator HKU_API AmaSpecial(const Block& block, KQuery query, Indicator ama) { Indicator result; StockManager& sm = StockManager::instance(); //计算每日股票总数 DatetimeList dateList = sm.getTradingCalendar(query, "SH"); size_t dayTotal = dateList.size(); if (dayTotal == 0) { result = PRICELIST(PriceList()); result.name("POS"); return result; } vector<size_t> numberPerDay(dayTotal); for (size_t i = 0; i < dayTotal; ++i) { numberPerDay[i] = 0; for (auto stk_iter = block.begin(); stk_iter != block.end(); ++stk_iter) { if (stk_iter->startDatetime() <= dateList[i] && dateList[i] <= stk_iter->lastDatetime()) { numberPerDay[i]++; } } } vector<size_t> position(dayTotal); size_t discard = ama.discard(); for (auto stk_iter = block.begin(); stk_iter != block.end(); ++stk_iter) { KData kdata = stk_iter->getKData(query); if (kdata.empty()) continue; SignalPtr sg(SG_Single(ama)); sg->setTO(kdata); bool isHold = false; size_t n_dis = 0; for (size_t i = 0; i < dayTotal; ++i) { if (isHold) { if (sg->shouldSell(dateList[i])) { isHold = false; } else { position[i]++; } } else { if (sg->shouldBuy(dateList[i])) { position[i]++; isHold = true; } } if (dateList[i] >= kdata[0].datetime) { if (n_dis < discard) { n_dis++; numberPerDay[i]--; } } } } PriceList tmp_result(dayTotal, Null<price_t>()); for (auto i = discard; i < dayTotal; ++i) { tmp_result[i] = numberPerDay[i] ? (double)position[i]/(double)numberPerDay[i] : 1.0; } result = PRICELIST(tmp_result); result.name("POS"); return PRICELIST(result); }