void Emojis::setCurrentTheme(const QString &theme) { QString path = EMOJIS_PATH + theme + "/"; QString conf = path + "theme"; QFile cfile(conf); if( !cfile.open(QFile::ReadOnly) ) return; p->theme = theme; p->emojis.clear(); p->keys.clear(); const QString data = cfile.readAll(); const QStringList & list = data.split("\n",QString::SkipEmptyParts); foreach( const QString & l, list ) { const QStringList & parts = l.split("\t",QString::SkipEmptyParts); if( parts.count() < 2 ) continue; QString epath = path + parts.at(0); QString ecode = parts.at(1); p->emojis[ecode] = epath; p->keys << ecode; } emit currentThemeChanged(); }
int GetFile::GetHTTPFile(const std::string& value) { CURL *curl; curl = curl_easy_init(); if (! curl) { std::cerr << "curl_easy_init failed" << std::endl; return -1; } std::string filepath("/tmp/" + value); std::ofstream ofs; ofs.open(filepath); if (! ofs.is_open()) { std::cerr << "cannot open /tmp/" << value << std::endl; curl_easy_cleanup(curl); return -2; } std::ostringstream urlss; urlss << "http://" << host_name_ << ":" << port_ << "/" << value; curl_easy_setopt(curl, CURLOPT_URL, urlss.str().c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, cfile(ofs.rdbuf())); if (user_ != "" && password_ != "") { std::string userpwd = user_ + ":" + password_; curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str()); } CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << std::endl; curl_easy_cleanup(curl); ofs.close(); unlink(filepath.c_str()); return -3; } long response_code; res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); if (res != CURLE_OK) { std::cerr << "curl_easy_getinfo failed: " << curl_easy_strerror(res) << std::endl; curl_easy_cleanup(curl); ofs.close(); unlink(filepath.c_str()); return -4; } curl_easy_cleanup(curl); ofs.close(); if (response_code != 200) { unlink(filepath.c_str()); return -5; } return 0; }
int main(int argc, char **argv) { if (argc != 4) { std::cerr << "Usage: CImageProcess " << "<ifile> <ofile> <cmdfile>" << std::endl; exit(1); } CFile ifile(argv[1]); CFile ofile(argv[2]); CFile cfile(argv[3]); CImageFileSrc src(ifile); CImagePtr src_image = CImageMgrInst->createImage(src); CImagePtr dst_image = src_image->dup(); std::string line; while (cfile.readLine(line)) { CStrParse parse(line); parse.skipSpace(); std::string cmd; parse.readIdentifier(cmd); if (cmd == "hflip") dst_image->flipH(); else if (cmd == "vflip") dst_image->flipV(); else if (cmd == "resize") { parse.skipSpace(); int w, h; if (! parse.readInteger(&w)) { std::cerr << "Bad Resize Width" << std::endl; continue; } parse.skipSpace(); if (! parse.readInteger(&h)) { std::cerr << "Bad Resize Width" << std::endl; continue; } dst_image->reshape(w, h); } else if (cmd == "gray") { dst_image->grayScale(); } else std::cerr << "Invalid command " << cmd << std::endl; } CFileType type = CFileUtil::getImageTypeFromName(argv[2]); dst_image->write(&ofile, type); exit(0); }
bool HDFWalkerInputManager::put(xmlNodePtr cur) { //reference revision number HDFVersion start_version(0,4); //current node int pid=myComm->rank(); string froot("0"), cfile("0"); //string target("e"), collect("no"); int anode=-1, nblocks=1, nprocs=1; HDFVersion in_version(0,1); //set to be old version OhmmsAttributeSet pAttrib; pAttrib.add(cfile,"href"); pAttrib.add(cfile,"file"); pAttrib.add(froot,"fileroot"); pAttrib.add(anode,"node"); pAttrib.add(nprocs,"nprocs"); //pAttrib.add(collect,"collected"); pAttrib.add(in_version,"version"); pAttrib.put(cur); bool success=false; if(in_version>=start_version) { HDFWalkerInput_0_4 win(targetW,myComm,in_version); success= win.put(cur); cfile=win.FileName; } else { //missing version or old file if(froot[0] != '0')//use nprocs { anode=pid; if(nprocs==1) cfile=froot; else { char *h5name=new char[froot.size()+10]; sprintf(h5name,"%s.p%03d",froot.c_str(),pid); cfile=h5name; delete [] h5name; } } int pid_target= (anode<0)? pid:anode; if(pid_target == pid && cfile[0] != '0') { HDFWalkerInput_0_0 win(targetW,cfile); success= win.put(cur); } } if(success) CurrentFileRoot = cfile; return success; }
void BACKUPDLG::OnButton1() { CFileDialog cfile(FALSE,"org",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "备份文件(*.bak)|*.bak|All Files (*.*)|*.*||"); if(cfile.DoModal()==IDOK) { CString str; str=cfile.GetPathName(); if(str!="") m_edit=str; UpdateData(false); } else return; }
void LookUp::read_hostfile(const string& fname) { if(verbose_lvl) cout << indent(2) << "Reading " << fname << endl; Infile cfile(fname, ios::binary | ios::in); while(cfile && !cfile.eof()) { HostRec hr; cfile >> hr.ip; if(!is_validip(hr.ip)) { skiptillendofline(cfile); continue; } cfile >> hr.hostname; if(hr.hostname.empty()) { skiptillendofline(cfile); continue; } if(HostRec* hrp = is_falseduplicate(hr)) { if(cdb) { dbgs << "************************************************" << endl; dbgs << "WARNING: Found false duplicate in " << fname << endl; dbgs << "File says: (" << hr.ip << ", " << hr.hostname << ")" << endl; dbgs << "Cache says: (" << hrp->ip << ", " << hr.hostname << ")" << endl; dbgs << "************************************************" << endl; } cout << "Found false duplicate in " << fname << ": (" << hr.ip << ", " << hr.hostname << ")" << endl; cout << " while cache says: " << hrp->ip << ", " << hrp->hostname << ")" << endl; } else hc.push_back(hr); skiptillendofline(cfile); } hc.sort(); hc.unique(); }
void CRESTOREDLG::OnButton1() { // TODO: Add your control notification handler code here CFileDialog cfile(TRUE,"org",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"备份文件(*.org)|*.org|All Files (*.*)|*.*||"); if(cfile.DoModal()) { m_edit=""; CString str; str=cfile.GetPathName(); m_edit=str; UpdateData(false); } else return; }
int main() { syslog(LOG_INFO,"Raspberry Pi wiringPi DHT22 reader daemon based on lol_dht22 and 1wirevz"); // init wiringPI if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; if (setuid(getuid()) < 0) { perror("Dropping privileges failed\n"); exit(EXIT_FAILURE); } // read values from the config file cfile(); setlogmask(LOG_UPTO(LOG_INFO)); openlog(DAEMON_NAME, LOG_CONS | LOG_PERROR, LOG_USER); // syslog(LOG_INFO, "DS2482 I²C 1-Wire® Master to Volkszaehler deamon %s (%s) %d", DAEMON_VERSION, DAEMON_BUILD, count_i2cdevices()); char pid_file[16]; sprintf ( pid_file, "/tmp/%s.pid", DAEMON_NAME ); daemonize( "/tmp/", pid_file ); while(1) { // reading the sensor until a valid value while (read_dht22_dat() == 0) { syslog(LOG_INFO,"No data from sensor, retrying..."); sleep(1); } // posting the measured values to vz http_post(temperature, temperature_uuid); http_post(humidity, humidity_uuid); // sleep for the configured seconds sleep(minterval); } return(0); }
void Connections::writeCSVs(){ QFile cfile("coords.csv"); cfile.open(QIODevice::WriteOnly); QTextStream out(&cfile); out << "id,label,x,y" << endl; for (int i = 0; i<nodes.length(); i++){ out << i << "," << i << "," << nodes.at(i).x() << "," << nodes.at(i).y() << endl; } cfile.close(); QFile ffile("cons.csv"); ffile.open(QIODevice::WriteOnly); QTextStream fout(&ffile); fout << "from,to,val" << endl; for (int i = 0; i<edges.length(); i++){ fout << nodes.indexOf(edges.at(i)->fn) << "," << nodes.indexOf(edges.at(i)->tn) << "," << 1 << endl; } ffile.close(); }
int main (int argc, char *argv[]) { unsigned inumber; if (argc != 2) usage(argv[0]); errno = 0; inumber = strtol(argv[1], NULL, 10); if (errno || inumber <= 0) usage(argv[0]); mount(); cfile(inumber); umount(); exit(EXIT_SUCCESS); }
void LookUp::read_failedip(const string& fname) { Infile cfile(fname, ios::binary | ios::in); while(cfile && !cfile.eof()) { string ip; cfile >> ip; if(!is_validip(ip)) { skiptillendofline(cfile); continue; } failed.push_back(ip); skiptillendofline(cfile); } failed.sort(); failed.unique(); }
void ZConfigFile::Flush() { std::list<ZCF_Section>::iterator secIter; std::list<ZCF_Variable>::iterator varIter; std::string secName; //in case the filename is already cleared somehow if(rFilename.length()) { //open the file blanked out, to not duplicate entries std::ofstream cfile(rFilename.c_str(), std::ios::out|std::ios::trunc); if(cfile) { //iteration through sections for(secIter = rFileLayout.begin(); secIter != rFileLayout.end(); ++secIter) { //ensure that section is valid secName = CleanString((*secIter).section); if(secName.length() && secName[0] == '[' && secName[secName.length()-1] == ']') { cfile << (*secIter).section << std::endl; //write out raw section title //for each variable in section, write out variable=value for(varIter = (*secIter).varList.begin(); varIter != (*secIter).varList.end(); ++varIter) { if((*varIter).var[0] == '_') { if( ((*varIter).var).substr(2,7) == "comment") cfile << (*varIter).val << std::endl; else if( ((*varIter).var).substr(2,7) == "newline") cfile << std::endl; } else if(CleanString((*varIter).var).length()) //ensures that variable is valid cfile << (*varIter).var << '=' << (*varIter).val << std::endl; } } } cfile.close(); } } }
void ZConfigFile::Process(std::string filename) { rFilename = filename; int commentNum,newlineNum; char commentStr[15],newlineStr[15]; std::string section, str, var, tmp; std::ifstream cfile(rFilename.c_str()); commentNum = newlineNum = 0; //comment/newline support is a bit of a hack rFileLayout.clear(); //layout must be cleared, in case variable is being used multiple times while(!cfile.eof() && cfile.is_open()) //parses entire file { std::getline(cfile,str); //read in a line tmp = CleanString(str); //get a clean version //if std::string is bracketed it is a section, if it begins in a letter it is a variable if(tmp[0] == '[' && tmp[tmp.length()-1] == ']') section = str; else if(std::isalpha(tmp[0])) //variables must start with a letter { var = str.substr(0,str.find('=')); //split the std::string at the equals sign SetVariable(section,var,str.substr(str.find('=')+1,str.length()-var.length()-1)); } else if(tmp[0] == '#' || tmp[0] == ';') //acceptable comment characters { sprintf(commentStr,"__comment%d",commentNum); SetVariable(section,commentStr,str); ++commentNum; } else if(tmp.length() == 0 && !cfile.eof()) //avoid adding a line to end of file each time { sprintf(newlineStr,"__newline%d",newlineNum); SetVariable(section,newlineStr,str); ++newlineNum; } } cfile.close(); }
Config::Config( std::string filename ) { this->filename = filename; std::ifstream cfile( filename ); if( !cfile.is_open() ) { printf("Can't open config file!\n"); exit(-1); } std::string buf; while( std::getline (cfile, buf ) ) { if( buf.length() > 0 ) { buf.erase(remove_if(buf.begin(), buf.end(), isspace), buf.end()); if( buf[0] == '#' || buf[0] == ';' || ( buf[0] == '/' && buf[1] == '/' ) || buf[0] == '=' ) continue; std::size_t i = buf.find_last_of('='); if( i == std::string::npos ) continue; if( i+1 == buf.length() ) continue; // var name std::string key = buf.substr(0, i); if( key == "" ) continue; // var value std::string value = buf.substr( i+1, buf.length() ); if( value == "" ) continue; // std::cout << "Line parsed:\t'" << key << "'\t->\t'" << value << "'" << std::endl; this->params[key] = value; } } }
void main() { CElement element1(123); CElement element2(element1); cout << "The value of the element is " << element2.getValue() << endl; CFile<CElement> cfile("C:\\Development\\file.txt"); cout << "file size is " << cfile.size() << endl; // expected 0 cfile.write(element1); cout << "After writing single element, file size is " << cfile.size() << endl; // expected 1 CElement element3 = cfile.read(); cout << "Reading the single element from file: " << element3.getValue() << endl; CElement element4(456); CElement element5(78); CElement writeBuf[] = { element4, element5 }; cout << "writing one more element..." << endl; CElement element6(1000091); cfile.write(element6); cout << "Trying to write two elements from buffer... " << endl; cfile.write(writeBuf, 2); cout << "After writing 4 elements, size is: " << cfile.size() << endl; cout << "Trying to read 4 elems with buffer: " << endl; CElement* readBuf; int index = cfile.read(&readBuf, 4); cout << "elements are: " << endl; for (int i = 0; i < index; i++) { cout << readBuf[i]; } }
bool checkIfFileExist(constr& file) { std::ifstream cfile(file); return cfile; }
BOOL baidu_upload(CString sfile, CString token, CString fname,DWORD *process) { if (sfile == L"") { MessageBox(NULL, L"Îļþ·¾¶²»ÄÜΪ¿Õ", 0, 0); return FALSE; } if (token == L"") { MessageBox(NULL, L"token²»ÄÜΪ¿Õ", 0, 0); return FALSE; } if (fname == L"") { MessageBox(NULL, L"ÎļþÃû²»ÄÜΪ¿Õ", 0, 0); return FALSE; } CString url(L"/rest/2.0/pcs/file?method=upload&ondup=overwrite&path=%2Fapps%2Fhitdisk%2F" + fname + L"&access_token=" + token); DWORD total_length = 0;//ÉÏ´«Êý¾Ý×Ü´óС DWORD file_length = 0;//ÉÏ´«ÎļþµÄ´óС //DWORD read_length = 0;//ÒѾ´ÓÎļþ¶ÁÈ¡µÄ´óС DWORD sent_length = 0;//ÒѾÉÏ´«µÄÎļþµÄ´óС DWORD sent_bfleng = 0;//µ±Ç°Êý¾Ý¿éÒÑÉÏ´«´óС DWORD head_length = 0;//Êý¾ÝÍ·´óС DWORD tail_length = 0;//Êý¾Ýβ´óС DWORD read_part = 1024 * 1024 * 2; DWORD send_part = 1024; DWORD read_tmp = 0;//µ±Ç°´ÓÎļþ¶ÁÈ¡µ½»º³åÇøµÄ´óС DWORD sent_tmp = 0;//µ±Ç°·¢ËÍ´óС CFile cfile(sfile, CFile::modeRead); file_length = (DWORD)cfile.GetLength(); CHAR *send_buffer = new CHAR[read_part]; HINTERNET hRequest = NULL; HINTERNET hConnect = NULL; HINTERNET hnet = InternetOpen(fname, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); hConnect = InternetConnect(hnet, L"pcs.baidu.com", 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, L"POST", url, NULL, NULL, NULL, INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_RELOAD, 0); TCHAR ct[] = L"Content-Type: multipart/form-data; boundary=--myself"; HttpAddRequestHeaders(hRequest, ct, lstrlen(ct), HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE); CHAR head[] = "----myself\r\nContent-Disposition: form-data; name=\"file\"; filename=\"\"\r\n\r\n"; CHAR tail[] = "\r\n----myself--\r\n"; head_length = strlen(head); tail_length = strlen(tail); total_length = file_length + head_length + tail_length; INTERNET_BUFFERS Ibuffer; ZeroMemory(&Ibuffer, sizeof(INTERNET_BUFFERS)); Ibuffer.dwBufferTotal = total_length; Ibuffer.dwStructSize = sizeof(INTERNET_BUFFERS); HttpSendRequestEx(hRequest, &Ibuffer, NULL, 0, NULL); InternetWriteFile(hRequest, head, head_length, &sent_tmp); sent_length += sent_tmp; while (read_tmp = cfile.Read(send_buffer, read_part)) { sent_bfleng = 0; while (sent_bfleng != read_tmp) { if (read_tmp - sent_bfleng > send_part) { InternetWriteFile(hRequest, send_buffer + sent_bfleng, send_part, &sent_tmp); } else { InternetWriteFile(hRequest, send_buffer + sent_bfleng, read_tmp - sent_bfleng, &sent_tmp); } if (sent_tmp == 0) { InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hnet); cfile.Close(); delete[] send_buffer; return FALSE; } sent_bfleng += sent_tmp; sent_length += sent_tmp; *process = (DWORD)(100 * (double)sent_length / total_length); } } InternetWriteFile(hRequest, tail, tail_length, &sent_tmp); sent_length += sent_tmp; *process = (DWORD)(100 * (double)sent_length / total_length); HttpEndRequest(hRequest, NULL, 0, NULL); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hnet); cfile.Close(); delete[] send_buffer; return TRUE; }
unsigned int conf_reader::read(const std::string & _filename, bool _clear) { if (_filename != "") { filename = _filename; }; if (filename != "") { try { ifstream cfile(filename.c_str()); if (!cfile) throw base_exception(); if (_clear) values.clear(); // read the file line by line, ignoring comments. char inbuff[16*1024]; while (cfile) { cfile.getline(inbuff, 16*1024); string in = inbuff; // truncate at the start of a comment. unsigned long int where = in.find("#"); while (where != string::npos) { if (in[where-1] == '\\') { // comment char was escaped.. ignore. where = in.find("#",where+1); } else { // truncate the string at this point. in.erase(where); where = string::npos; }; }; // see if we can parse what's left. in = trim(in); unsigned long int split_point = in.find_first_of("\t "); if (split_point != string::npos) { // okay.. get the fields. pair arg; arg.first = gsub(trim(in.substr(0,split_point)),"\\#","#"); arg.second = gsub(trim(in.substr(split_point)),"\\#","#"); // is this an include directive? if (arg.first == "*INCLUDE") { read(arg.second,false); } else if (arg.first != "") { values.insert(arg); }; }; }; } catch (...) { cerr << "\nProblems reading configuration file \"" << filename << "\";\n\tdata may be incomplete." << endl; } }; return values.size(); };
BOOL baidu_download(CString dfile, CString token, CString fname,DWORD *process) { if (dfile == L"") { MessageBox(NULL,L"Îļþ·¾¶²»ÄÜΪ¿Õ", 0, 0); return FALSE; } if (token == L"") { MessageBox(NULL, L"token²»ÄÜΪ¿Õ", 0, 0); return FALSE; } if (fname == L"") { MessageBox(NULL, L"ÎļþÃû²»ÄÜΪ¿Õ", 0, 0); return FALSE; } CString url(L"/rest/2.0/pcs/file?method=download&path=%2Fapps%2Fhitdisk%2F" + fname + L"&access_token=" + token); DWORD headlength; DWORD FileLength;//Îļþ³¤¶È TCHAR* szBuff;//»º³åÇø DWORD bfsize = 1024 * 64;//»º³åÇø´óС TCHAR* FileBuff;//½âÎöÎļþ³¤¶ÈÖ¸Õë BOOL bResult = TRUE; HINTERNET hRequest = NULL; HINTERNET hConnect = NULL; HINTERNET hnet = InternetOpen(TEXT("Test"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); hConnect = InternetConnect(hnet, TEXT("pcs.baidu.com"), 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("GET"), url, NULL, NULL, NULL, INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_RELOAD, 0); bResult = HttpSendRequest(hRequest, NULL, 0, NULL, 0); //½âÎöÎļþ³¤¶È DWORD i; szBuff = new TCHAR[bfsize]; headlength = bfsize; bResult = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, szBuff, &headlength, NULL); FileBuff = wcsstr(szBuff, L"Content-Length"); FileBuff += 16; for (i = 0;; i++) { if (FileBuff[i] == '\r') break; } FileLength = CharToDword(FileBuff, i); CFile cfile(dfile, CFile::modeWrite | CFile::modeCreate); DWORD wbfclength = 0;//»º³åÇøµ±Ç°Êý¾Ý³¤¶È DWORD wbfsize = 1024 * 1024;//»º³åÇø´óС char *WriteBuffer = new char[wbfsize];//ÎļþдÈ뻺³åÇø CString show_process; DWORD dwBytesAvailable; DWORD FileReceived = 0; BOOL error = TRUE; BOOL cmp = 0; while (InternetQueryDataAvailable(hRequest, &dwBytesAvailable, 0, 0)) { DWORD dwBytesRead; if (dwBytesAvailable <= bfsize) { bResult = InternetReadFile(hRequest, szBuff, dwBytesAvailable, &dwBytesRead); } else { bResult = InternetReadFile(hRequest, szBuff, bfsize, &dwBytesRead); } FileReceived += dwBytesRead; CopyMemory(WriteBuffer + wbfclength, szBuff, dwBytesRead); if (error) { szBuff[13] = '\0'; cmp = _strnicmp((char *)szBuff,"{\"error_code\"" , 13); if (cmp == 0) { *process = 100; InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hnet); cfile.Close(); cfile.Remove(dfile); delete[] szBuff; delete[] WriteBuffer; return FALSE; } } wbfclength += dwBytesRead; if (wbfclength > wbfsize - bfsize) { cfile.Write(WriteBuffer, wbfclength); wbfclength = 0; } *process = (DWORD)(100 * (double)FileReceived / FileLength); if (dwBytesRead == 0) break; // End of File. } if (wbfclength) cfile.Write(WriteBuffer, wbfclength); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hnet); cfile.Close(); delete[] szBuff; delete[] WriteBuffer; return TRUE; }