static void b_write(int p, const void *buff, int size) { int i; _Cli(); for(i = 0; i < size; i++) { myout( p , ( (byte *) buff )[i] ); } _Sti(); }
//数值算法 void stl_numeric_algo(){ double array[] = {-2,2,4,4,5}; double array1[] = {2,3,4,5,6}; vector<double> a(array,array + sizeof(array)/sizeof(double)); vector<double> b(array1,array1 + sizeof(array1)/sizeof(double)); vector<double> result(10); cout<<"accumulate func: "<<accumulate(a.begin(), a.end(), 0, minus<double>())<<endl; cout<<"inner product func: "<<inner_product(a.begin(),a.end(),b.begin(),0.0,plus<double>(),multiplies<double>())<<endl; adjacent_difference(a.begin(),a.end(),result.begin()); cout<<"adjacent_difference : "<<result<<endl; partial_sum(result.begin(),result.end(),result.begin()); cout<<"partial_sum : "<<result<<endl; ostream_iterator<double> myout(cout, " "); copy(result.begin(), result.end(), myout); }
int createMETAAudit(QSqlDatabase mydb, QString auditDir) { QSqlQuery query(mydb); QSqlQuery query2(mydb); QString sql; QStringList TriggerData; QStringList TriggerLite; QStringList dropMyTriggers; QStringList dropLiteTriggers; TriggerData << "CREATE TABLE IF NOT EXISTS audit_log ("; TriggerData << "audit_id INT(11) NOT NULL AUTO_INCREMENT ,"; TriggerData << "audit_date TIMESTAMP NULL ,"; TriggerData << "audit_action VARCHAR(6) NULL ,"; TriggerData << "audit_user VARCHAR(120) NULL ,"; TriggerData << "audit_table VARCHAR(60) NULL ,"; TriggerData << "audit_column VARCHAR(60) NULL ,"; TriggerData << "audit_key VARCHAR(500) NULL ,"; TriggerData << "audit_oldvalue VARCHAR(500) NULL ,"; TriggerData << "audit_newvalue VARCHAR(500) NULL ,"; TriggerData << "audit_insdeldata TEXT NULL ,"; TriggerData << "PRIMARY KEY (audit_id) )"; TriggerData << " ENGINE = InnoDB CHARSET=utf8;"; TriggerData << ""; //TriggerData << "ALTER TABLE audit_log ADD COLUMN audit_insdeldata TEXT NULL;"; //TriggerData << "ALTER TABLE audit_log CHANGE audit_updatekey audit_key varchar(500);"; //TriggerData << ""; TriggerLite << "BEGIN;"; TriggerLite << "CREATE TABLE audit_log ("; TriggerLite << "audit_id INTEGER NOT NULL,"; TriggerLite << "audit_date VARCHAR(120) NULL ,"; TriggerLite << "audit_action VARCHAR(6) NULL ,"; TriggerLite << "audit_user VARCHAR(120) NULL ,"; TriggerLite << "audit_table VARCHAR(60) NULL ,"; TriggerLite << "audit_column VARCHAR(60) NULL ,"; TriggerLite << "audit_key VARCHAR(500) NULL ,"; TriggerLite << "audit_oldvalue VARCHAR(500) NULL ,"; TriggerLite << "audit_newvalue VARCHAR(500) NULL ,"; TriggerLite << "audit_insdeldata TEXT NULL ,"; TriggerLite << "PRIMARY KEY (audit_id) );"; TriggerLite << ""; int auditIndex; auditIndex = 0; sql = "SELECT tbl_cod FROM dict_tblinfo WHERE tbl_cod NOT LIKE 'v_%'"; //Excluse views in audit if (query.exec(sql)) { while (query.next()) { auditIndex = auditIndex + 1; if (ignoreTables.indexOf(query.value(0).toString().toLower()) < 0) { //Update trigger for MySQL------------------------------------------------------------------- dropMyTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) +"_update;"; TriggerData << "delimiter $$"; TriggerData << "CREATE TRIGGER audit_" + QString::number(auditIndex) +"_update"; TriggerData << "AFTER UPDATE ON " + query.value(0).toString(); TriggerData << "FOR EACH ROW BEGIN"; TriggerData << "DECLARE ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP();"; TriggerData << "DECLARE us VARCHAR(120) DEFAULT USER();"; TriggerData << ""; QString mykeyData; mykeyData = "CONCAT("; QString myNoKeyData; sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { while (query2.next()) { mykeyData = mykeyData + "'(" + query2.value(0).toString() + ")',OLD." + query2.value(0).toString() + ",',',"; } mykeyData = mykeyData.left(mykeyData.length()-5) + ")"; } sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' ORDER BY clm_key,clm_pos DESC"; // AND clm_key = 0 if (query2.exec(sql)) { while (query2.next()) { TriggerData << "IF OLD." + query2.value(0).toString() + " <> NEW." + query2.value(0).toString() + " THEN INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_column,audit_key,audit_oldvalue,audit_newvalue) VALUES (ts,'UPDATE',us,'" + query.value(0).toString() + "','" + query2.value(0).toString() + "'," + mykeyData + ",OLD." + query2.value(0).toString() + ",NEW." + query2.value(0).toString() + ");"; TriggerData << "END IF;"; } } TriggerData << ""; TriggerData << "END$$"; TriggerData << "DELIMITER ;"; TriggerData << ""; //Insert trigger for MySQL----------------------------------------------------------------------------------- dropMyTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) +"_insert;"; TriggerData << "delimiter $$"; TriggerData << "CREATE TRIGGER audit_" + QString::number(auditIndex) +"_insert"; TriggerData << "AFTER INSERT ON " + query.value(0).toString(); TriggerData << "FOR EACH ROW BEGIN"; TriggerData << "DECLARE ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP();"; TriggerData << "DECLARE us VARCHAR(120) DEFAULT USER();"; TriggerData << ""; //Getting the key data of the deleted record sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { mykeyData = "CONCAT("; while (query2.next()) { mykeyData = mykeyData + "'(" + query2.value(0).toString() + ")',NEW." + query2.value(0).toString() + ",',',"; } } mykeyData = mykeyData.left(mykeyData.length()-5) + ")"; // Getting the non-key data of the deleted record // The non-key data is represented as Hexadecimal character values to avoid conflicts with controlling characters like () and , sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 0"; if (query2.exec(sql)) { myNoKeyData = "CONCAT("; while (query2.next()) { myNoKeyData = myNoKeyData + "'(" + query2.value(0).toString() + ")',ifnull(hex(cast(NEW." + query2.value(0).toString() + " as char)),''),',',"; } } if (myNoKeyData != "CONCAT(") myNoKeyData = myNoKeyData.left(myNoKeyData.length()-5) + ")"; else myNoKeyData = "''"; TriggerData << "INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_key,audit_insdeldata) VALUES (ts,'INSERT',us,'" + query.value(0).toString() + "'," + mykeyData + "," + myNoKeyData + ");"; TriggerData << ""; TriggerData << "END$$"; TriggerData << "DELIMITER ;"; TriggerData << ""; //Delete trigger for MySQL---------------------------------------------------------------------------- dropMyTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) +"_delete;"; TriggerData << "delimiter $$"; TriggerData << "CREATE TRIGGER audit_" + QString::number(auditIndex) +"_delete"; TriggerData << "AFTER DELETE ON " + query.value(0).toString(); TriggerData << "FOR EACH ROW BEGIN"; TriggerData << "DECLARE ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP();"; TriggerData << "DECLARE us VARCHAR(120) DEFAULT USER();"; TriggerData << ""; //Getting the key data of the deleted record sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { mykeyData = "CONCAT("; while (query2.next()) { mykeyData = mykeyData + "'(" + query2.value(0).toString() + ")',OLD." + query2.value(0).toString() + ",',',"; } } mykeyData = mykeyData.left(mykeyData.length()-5) + ")"; // Getting the non-key data of the deleted record // The non-key data is represented as Hexadecimal values to avoid conflicts with controlling characters like () and , sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 0"; if (query2.exec(sql)) { myNoKeyData = "CONCAT("; while (query2.next()) { myNoKeyData = myNoKeyData + "'(" + query2.value(0).toString() + ")',ifnull(hex(cast(OLD." + query2.value(0).toString() + " as char)),''),',',"; } } if (myNoKeyData != "CONCAT(") myNoKeyData = myNoKeyData.left(myNoKeyData.length()-5) + ")"; else myNoKeyData = "''"; TriggerData << "INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_key,audit_insdeldata) VALUES (ts,'DELETE',us,'" + query.value(0).toString() + "'," + mykeyData + "," + myNoKeyData + ");"; TriggerData << ""; TriggerData << "END$$"; TriggerData << "DELIMITER ;"; TriggerData << ""; //----------------------------------------------------------------------SQLite------------------------------------------------------------------------------------------------------------------------------ //Update trigger for SQLite------------------------------------------------------------------ QString litekeyData; QString liteNoKeyData; litekeyData = "("; sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { while (query2.next()) { litekeyData = litekeyData + "'(" + query2.value(0).toString() + ")' || OLD." + query2.value(0).toString() + "|| ',' ||"; } litekeyData = litekeyData.left(litekeyData.length()-9) + ")"; } dropLiteTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) + "_update;"; TriggerLite << "CREATE TRIGGER audit_" + QString::number(auditIndex) + "_update"; TriggerLite << "AFTER UPDATE ON " + query.value(0).toString(); TriggerLite << "FOR EACH ROW BEGIN"; TriggerLite << ""; sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' ORDER BY clm_key,clm_pos DESC"; //AND clm_key = 0; if (query2.exec(sql)) { while (query2.next()) { TriggerLite << "INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_column,audit_key,audit_oldvalue,audit_newvalue) values (hex(datetime('now','localtime')),'UPDATE','NONE','" + query.value(0).toString() + "','" + query2.value(0).toString() + "'," + litekeyData + ",OLD." + query2.value(0).toString() + ",NEW." + query2.value(0).toString() + ");"; } } TriggerLite << ""; TriggerLite << "END;"; TriggerLite << ""; //Insert trigger for SQLite--------------------------------------------------------------------------- dropLiteTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) + "_insert;"; TriggerLite << "CREATE TRIGGER audit_" + QString::number(auditIndex) + "_insert"; TriggerLite << "AFTER INSERT ON " + query.value(0).toString(); TriggerLite << "FOR EACH ROW BEGIN"; TriggerLite << ""; //Getting the key data of the deleted record sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { litekeyData = "("; while (query2.next()) { litekeyData = litekeyData + "'(" + query2.value(0).toString() + ")' || NEW." + query2.value(0).toString() + "|| ',' ||"; } } litekeyData = litekeyData.left(litekeyData.length()-9) + ")"; // Getting the non-key data of the deleted record // The non-key data is represented as Hexadecimal values to avoid conflicts with controlling characters like () and , sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 0"; if (query2.exec(sql)) { liteNoKeyData = "("; while (query2.next()) { liteNoKeyData = liteNoKeyData + "'(" + query2.value(0).toString() + ")' || hex(NEW." + query2.value(0).toString() + ") || ',' ||"; } } if (liteNoKeyData != "(") liteNoKeyData = liteNoKeyData.left(liteNoKeyData.length()-9) + ")"; else liteNoKeyData = "''"; TriggerLite << "INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_key,audit_insdeldata) values (hex(datetime('now','localtime')),'INSERT','NONE','" + query.value(0).toString() + "'," + litekeyData + "," + liteNoKeyData + ");"; TriggerLite << ""; TriggerLite << "END;"; TriggerLite << ""; //Delete trigger for SQLite---------------------------------------------------------------------------------- dropLiteTriggers << "DROP TRIGGER audit_" + QString::number(auditIndex) + "_delete;"; TriggerLite << "CREATE TRIGGER audit_" + QString::number(auditIndex) + "_delete"; TriggerLite << "AFTER DELETE ON " + query.value(0).toString(); TriggerLite << "FOR EACH ROW BEGIN"; TriggerLite << ""; //Getting the key data of the deleted record sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 1"; if (query2.exec(sql)) { litekeyData = "("; while (query2.next()) { litekeyData = litekeyData + "'(" + query2.value(0).toString() + ")' || OLD." + query2.value(0).toString() + "|| ',' ||"; } } litekeyData = litekeyData.left(litekeyData.length()-9) + ")"; // Getting the non-key data of the deleted record // The non-key data is represented as Hexadecimal values to avoid conflicts with controlling characters like () and , sql = "SELECT clm_cod FROM dict_clminfo WHERE tbl_cod = '" + query.value(0).toString() + "' AND clm_key = 0"; if (query2.exec(sql)) { liteNoKeyData = "("; while (query2.next()) { liteNoKeyData = liteNoKeyData + "'(" + query2.value(0).toString() + ")' || hex(OLD." + query2.value(0).toString() + ") || ',' ||"; } } if (liteNoKeyData != "(") liteNoKeyData = liteNoKeyData.left(liteNoKeyData.length()-9) + ")"; else liteNoKeyData = "''"; TriggerLite << "INSERT INTO audit_log(audit_date,audit_action,audit_user,audit_table,audit_key,audit_insdeldata) values (hex(datetime('now','localtime')),'DELETE','NONE','" + query.value(0).toString() + "'," + litekeyData + "," + liteNoKeyData + ");"; TriggerLite << ""; TriggerLite << "END;"; TriggerLite << ""; } } TriggerLite << "CREATE TRIGGER noAuditUpdates BEFORE UPDATE ON audit_log FOR EACH ROW BEGIN SELECT CASE WHEN ((SELECT NULL) IS NULL) THEN RAISE(ABORT, 'Audit table cannot be updated') END; END;"; TriggerLite << ""; TriggerLite << "CREATE TRIGGER noAuditDeletes BEFORE DELETE ON audit_log FOR EACH ROW BEGIN SELECT CASE WHEN ((SELECT NULL) IS NULL) THEN RAISE(ABORT, 'Audit table cannot be deleted') END; END;"; TriggerLite << ""; TriggerLite << "COMMIT;"; TriggerLite << ""; QDir audir(auditDir); if (!audir.exists()) { audir.setPath("."); if (!audir.mkdir(auditDir)) { log("Error creating audit dir"); return 1; } else { audir.cd(auditDir); } } //Saves the MySQL create audit file QString fileName; fileName = audir.absolutePath() + "/" + "mysql_create_audit.sql"; QFileInfo f(fileName); QString fp; fp = f.path(); if (!fileName.isEmpty()) { QFile myfile(fileName); if (!myfile.open(QIODevice::WriteOnly | QIODevice::Text)) return 1; QTextStream myout(&myfile); for (int pos = 0; pos <= TriggerData.count() -1;pos++) myout << TriggerData[pos] << "\n"; } log("MySQL Create audit script loaded in " + fileName); //Saves the SQLite create audit file fileName = ""; fileName = audir.absolutePath() + "/" + "sqlite_create_audit.sql"; if (!fileName.isEmpty()) { QFile litefile(fileName); if (!litefile.open(QIODevice::WriteOnly | QIODevice::Text)) return 1; QTextStream liteout(&litefile); for (int pos = 0; pos <= TriggerLite.count() -1;pos++) liteout << TriggerLite[pos] << "\n"; } log("SQLite Create audit script loaded in " + fileName); //Saves the MySQL drop audit file fileName = ""; fileName = audir.absolutePath() + "/" + "mysql_drop_audit.sql"; if (!fileName.isEmpty()) { QFile mydropfile(fileName); if (!mydropfile.open(QIODevice::WriteOnly | QIODevice::Text)) return 1; QTextStream mydropout(&mydropfile); for (int pos = 0; pos <= dropMyTriggers.count() -1;pos++) mydropout << dropMyTriggers[pos] << "\n"; } log("MySQL Drop audit script loaded in " + fileName); //Saves the SQlite drop audit file fileName = ""; fileName = audir.absolutePath() + "/" + "sqlite_drop_audit.sql"; if (!fileName.isEmpty()) { QFile litedropfile(fileName); if (!litedropfile.open(QIODevice::WriteOnly | QIODevice::Text)) return 1; QTextStream litedropout(&litedropfile); for (int pos = 0; pos <= dropLiteTriggers.count() -1;pos++) litedropout << dropLiteTriggers[pos] << "\n"; } log("SQLite Drop audit script loaded in " + fileName); } return 0; }
int main(int argc, char *argv[]) { int i = 0; ifstream myin("test.dat"); ifstream prefin("pref.dat"); ofstream myout("out.js"); if(!myin) { cout << "cannot open the file \n"; return 1; } while (!myin.eof()) { myin >> ptr_ga[i].name >> ptr_ga[i].value ; // cout << ptr_ga[i].name <<","<< ptr_ga[i].value <<"\n"; i++; } myin.close(); if(!myout) { cout << "cannot open the file \n"; return 1; } if (argc == 1) { char prefer [7]; char prefname[50]; char pref1[5]; char pref2[7]; char bool1[5]; char bool2[6]; if(!prefin) { cout << "cannot open the file \n"; return 1; } while (!prefin.eof()) { prefin >> prefer >> prefname ; // cout <<"This is "<< prefer << " and " << prefname << "\n"; i++; strcpy(pref1, "pref"); strcpy(pref2, "config"); strcpy(bool1, "true"); strcpy(bool2, "false"); if (strcmp(prefer,pref1) ==0) { // cout << "inside the def pref \n"; if (GetGlobal(prefname)!= NULL) { if (( strcmp (GetGlobal(prefname), bool1) == 0)|| ( strcmp (GetGlobal(prefname), bool2)== 0) || (isnum (GetGlobal(prefname)))) { //cout << "the current value is " <<GetGlobal(prefname)<<"\n"; myout<< "defaultPref(\"" << prefname << "\", " <<GetGlobal(prefname) <<");\n"; } else myout<< "defaultPref(\"" << prefname << "\", \"" <<GetGlobal(prefname) <<"\");\n"; } else cout << prefname << " is not found \n"; } else if (strcmp(prefer,pref2) ==0) { // cout << "inside the config \n"; if (GetGlobal(prefname)!= NULL) { if (( strcmp (GetGlobal(prefname), bool1) == 0)|| ( strcmp (GetGlobal(prefname), bool2) == 0) || (isnum (GetGlobal(prefname)))) {//cout << "the value of isnum is " << isnum <<"\n"; //cout << "the curretn value is "<<GetGlobal(prefname)<<"\n"; myout<< "config(\"" << prefname << "\", " <<GetGlobal(prefname) <<");\n"; } else myout<< "config(\"" << prefname << "\", \"" <<GetGlobal(prefname) <<"\");\n"; } else cout << prefname << " is not found \n"; } } }