Esempio n. 1
0
static cl_object
generate(cl_object digits, float_approx *approx)
{
        cl_object d, x;
        cl_fixnum digit;
        bool tc1, tc2;
        do {
                d = ecl_truncate2(ecl_times(approx->r, PRINT_BASE), approx->s);
                approx->r = VALUES(1);
                approx->mp = ecl_times(approx->mp, PRINT_BASE);
                approx->mm = ecl_times(approx->mm, PRINT_BASE);
                tc1 = approx->low_ok?
                        ecl_lowereq(approx->r, approx->mm) :
                        ecl_lower(approx->r, approx->mm);
                x = ecl_plus(approx->r, approx->mp);
                tc2 = approx->high_ok?
                        ecl_greatereq(x, approx->s) :
                        ecl_greater(x, approx->s);
                if (tc1 || tc2) {
                        break;
                }
                ecl_string_push_extend(digits, ecl_digit_char(ecl_fixnum(d), 10));
        } while (1);
        if (tc2 && !tc1) {
                digit = ecl_fixnum(d) + 1;
        } else if (tc1 && !tc2) {
                digit = ecl_fixnum(d);
        } else if (ecl_lower(times2(approx->r), approx->s)) {
                digit = ecl_fixnum(d);
        } else {
                digit = ecl_fixnum(d) + 1;
        }
        ecl_string_push_extend(digits, ecl_digit_char(digit, 10));
        return digits;
}
Esempio n. 2
0
BOOL CNetworksApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	AfxEnableControlContainer();

	/////////////Êý¾Ý¿â
   	if( (_access( "./user.db", 0 )) == -1 )
	{
		char * pErrMsg = 0;  
		int ret = 0;  
		sqlite3 * db = 0;  
		ret = sqlite3_open("./user.db", &db);  
		if ( ret != SQLITE_OK )  
		{  
		AfxMessageBox(sqlite3_errmsg(db));  
		exit(1);  
		} 
		ret = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);  
		
		  sqlite3_exec(db, "CREATE TABLE login(username varchar(64), password varchar(64));", NULL, NULL, NULL);
		  ret = sqlite3_exec(db, "INSERT INTO login VALUES('18d86ccba6c49512154e5abfe51eaa06','d8c993d0e022c4849ac0c27db27ecf69'); \
		  INSERT INTO login VALUES('5efb93a618588266af575fb8a381d60f','ccf82797241106ef5cbb8c28f2edd63d')", NULL, NULL, &pErrMsg); 
		  ret = sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL);                     
		  sqlite3_close(db);  
		  db = 0; 
	}
Esempio n. 3
0
void movePlayer(Player *player, World *world, int fwd_b, int bwd_b, int left_b, int right_b, int up_b, int down_b, int inertia) {
    vec3 right = {
        cos(player->horizontalAngle) * HORIZONTAL_MOVE_SPEED,
        0,
        sin(player->horizontalAngle) * HORIZONTAL_MOVE_SPEED
    };

    vec3 fwd = {-right[2], 0, right[0]};
    vec3 up = {0, VERTICAL_MOVE_SPEED, 0};

    vec3 movement = {0, 0, 0};
    vec3 force = {0, 0, 0};

    translate_v3v(force, gravity);
    scale_v3(force, deltaTime);

    if (fwd_b)
        translate_v3v(movement, fwd);
    if (bwd_b)
        subtract_v3v(movement, fwd);
    if (left_b)
        subtract_v3v(movement, right);
    if (right_b)
        translate_v3v(movement, right);
    if (up_b)
        translate_v3v(movement, up);
    if (down_b)
        subtract_v3v(movement, up);

    if (inertia) {
        multiply_v3(player->velocity, movementDecay);

        translate_v3v(player->velocity, force);
    } else {
        zero_v3(player->velocity);

        // force / (1 - decay) = mov[i] / (1 - decay[i])
        // the middle value doesn't work for this, because there's no speed limit in the y direction.
        // so I just did 3.333 * (HORIZONTAL_MOVE_SPEED / VERTICAL_MOVE_SPEED) to make it even
        scale_v3f(movement, 3.333, 3.333 * (HORIZONTAL_MOVE_SPEED / VERTICAL_MOVE_SPEED), 3.333);
    }

    translate_v3v(player->velocity, movement);

    if (fabsf(player->velocity[0]) < MIN_SPEED) player->velocity[0] = 0;
    if (fabsf(player->velocity[1]) < MIN_SPEED) player->velocity[1] = 0;
    if (fabsf(player->velocity[2]) < MIN_SPEED) player->velocity[2] = 0;

    collidePlayer(player, world);

    translate_v3f(player->position, VALUES(deltaTime * player->velocity));
}
Esempio n. 4
0
static float_approx *
setup(cl_object number, float_approx *approx)
{
        cl_object f = cl_integer_decode_float(number);
        cl_fixnum e = ecl_fixnum(VALUES(1)), min_e;
        bool limit_f = 0;
        switch (ecl_t_of(number)) {
        case t_singlefloat:
                min_e = FLT_MIN_EXP;
                limit_f = (number->SF.SFVAL ==
                           ldexpf(FLT_RADIX, FLT_MANT_DIG-1));
                break;
        case t_doublefloat:
                min_e = DBL_MIN_EXP;
                limit_f = (number->DF.DFVAL ==
                           ldexp(FLT_RADIX, DBL_MANT_DIG-1));
                break;
#ifdef ECL_LONG_FLOAT
        case t_longfloat:
                min_e = LDBL_MIN_EXP;
                limit_f = (number->longfloat.value ==
                           ldexpl(FLT_RADIX, LDBL_MANT_DIG-1));
#endif
        }
        approx->low_ok = approx->high_ok = ecl_evenp(f);
        if (e > 0) {
                cl_object be = EXPT_RADIX(e);
                if (limit_f) {
                        cl_object be1 = ecl_times(be, ecl_make_fixnum(FLT_RADIX));
                        approx->r = times2(ecl_times(f, be1));
                        approx->s = ecl_make_fixnum(FLT_RADIX*2);
                        approx->mm = be;
                        approx->mp = be1;
                } else {
                        approx->r = times2(ecl_times(f, be));
                        approx->s = ecl_make_fixnum(2);
                        approx->mm = be;
                        approx->mp = be;
                }
        } else if (!limit_f || (e == min_e)) {
                approx->r = times2(f);
                approx->s = times2(EXPT_RADIX(-e));
                approx->mp = ecl_make_fixnum(1);
                approx->mm = ecl_make_fixnum(1);
        } else {
                approx->r = times2(ecl_make_fixnum(FLT_RADIX));
                approx->s = times2(EXPT_RADIX(1-e));
                approx->mp = ecl_make_fixnum(FLT_RADIX);
                approx->mm = ecl_make_fixnum(1);
        }
        return approx;
}
Esempio n. 5
0
/** \brief Eine Nachricht in die Datenbank einfügen
 *
 * \param mes message*  Nachricht
 * \return bool         true: es hat funktioniert; false: es hat nicht funktoiniert
 *
 */
bool insert_message(message * mes){
	char * query=NULL;
	char * time_created=NULL;
	MYSQL * my=NULL;
	bool success=false;

	if(strlen(mes->title)<2 || strlen(mes->message)<2)print_exit_failure("Geben Sie eine Meldung ein!!");

	clean_string(mes->message);
	clean_string(mes->title);
	mes->message=nlcr_to_htmlbr(mes->message);
	#ifdef DEBUG
	fprintf(stderr, "\n\n Meldung: '%s'", mes->message);
	#endif // DEBUG
	remove_newline(mes->message);

	time_created=calloc(21, sizeof(char)); // 20
	strftime(time_created, 20, "%Y-%m-%d %H:%M:%S", mes->created);
	//                           4  2  2  2  2  2 ---> ? 19 +1 (\0) --> 20 = ????

	if(asprintf(&query, "INSERT INTO Meldungen \
				(titel, meldung, kurse, erstellerID, erstellt)\
				VALUES('%s', '%s', '%s', '%d', '%s')",
				mes->title, mes->message, mes->courses, mes->creator_id, time_created  ) == -1){
		print_exit_failure("Es konnte kein Speicher angefordert werden (insert_message)");
	}
	#ifdef DEBUG
	fprintf(stderr, "\n\nQuery (insert_message) : '%s'\n\n", query);
	#endif // DEBUG

	my=mysql_init(NULL);
	if(my == NULL){
		print_exit_failure("MYSQL init failure!");
	}

	if(mysql_real_connect(my, "localhost", SQL_ALTERNATE_USER, SQL_ALTERNATE_PASS, SQL_BASE, 0, NULL, 0) == NULL){
		print_exit_failure("MYSQL-connection error!");
	}

	if(mysql_query(my, query)){
		#ifdef DEBUG
		fprintf(stderr, "sql_query:\n%s\nfailed\n", query);
		#endif // DEBUG

		success=false;
	}else{
		success=true;
	}
	mysql_close(my);
	free(query);
	return success;
}
Esempio n. 6
0
/* 当网关登录时,初始化客户端记录列表 */
bool CClientInfo::InitClientRecord(const char * name)
{
	char sql_line[NORMAL_XG_BUF_LEN];
	char sql_line_exist[NORMAL_XG_BUF_LEN];

	int sql_len_exist = _snprintf_s(sql_line_exist, NORMAL_XG_BUF_LEN-1, _TRUNCATE,
		"SELECT serial FROM %s WHERE serial='%s'", m_client_record_table, name);
	sql_line_exist[sql_len_exist] = '\0';
	MYSQL_RES * existresults = BeginQuery(sql_line_exist);	// 查询网关名称看是否已存在
	if (NULL == existresults)
	{
		return FALSE;
	}

	if(existresults->row_count == 0)
	{	
		EndQuery(existresults);

		// 如果不存在则初始化client_record表,各项均置为0
		int sql_len = _snprintf_s(sql_line, NORMAL_XG_BUF_LEN - 1, _TRUNCATE, 
			"INSERT INTO %s(serial,android_phone,android_pad,pc,tmp,iOS)\
			VALUES('%s', 0, 0, 0, 0, 0)",
			m_client_record_table, name);
		if(-1 == sql_len)
		{
			return false;
		}

		SLOG(4)("InitClientRecord = %s", sql_line);

		sql_line[sql_len] = '\0';

		MYSQL_RES * results = BeginQuery(sql_line);
		EndQuery(results);
	}
	else
	{
		EndQuery(existresults);
	}
	return TRUE;
}
//the schema is almost the same with the insert trigger, but it is used for delete trigger, because delete trigger can not use NEW, only OLD is vaildate
static const CHAR* s_threads_update_on_delete_trigger_schema = "CREATE TRIGGER IF NOT EXISTS sms_update_thread_on_delete AFTER DELETE ON sms_tb 	\
        BEGIN  \
        UPDATE threads_tb SET    date = OLD.date	\
        WHERE threads_tb.thread_id = OLD.thread_id;\
        UPDATE threads_tb SET message_count =\
        (SELECT COUNT(sms_tb.sms_id) FROM sms_tb  WHERE thread_id = OLD.thread_id AND sms_tb.type != 3)  WHERE threads_tb.thread_id = OLD.thread_id; \
        UPDATE threads_tb SET unread_count =  (SELECT COUNT(*)   FROM sms_tb  WHERE read = 0 AND type != 3  AND thread_id = OLD.thread_id) WHERE threads_tb.thread_id = OLD.thread_id;\
        DELETE FROM threads_tb WHERE thread_id=OLD.thread_id AND thread_id NOT IN (SELECT  thread_id FROM sms_tb) ;\
        END;";

#if 0
//insert a new thread id into canonical_addr_tb, we also need to add a new row into threads_tb
static const CHAR* s_threads_update_on_new_thread_trigger_schema = "CREATE TRIGGER IF NOT EXISTS new_thread_on_insert AFTER INSERT ON canonical_addr_tb 	\
        BEGIN  \
        INSERT INTO threads_tb (ID) VALUES (NEW.ID);\
        END;";
#endif

static const CHAR* s_sms_index_schema = "CREATE INDEX IF NOT EXISTS type_thread_index ON sms_tb (type, date DESC);";

static const CHAR* s_sms_thread_id_index_schema = "CREATE INDEX IF NOT EXISTS thread_index ON sms_tb (thread_id,date DESC);";

static const CHAR* s_thread_address_index_schema = "CREATE INDEX IF NOT EXISTS thread_address_index ON threads_tb (canonical_address);";

static const CHAR* s_thread_date_index_schema = "CREATE INDEX IF NOT EXISTS thread_date_index ON threads_tb (date );";

static INT32 _sms_create_table_impl(DatabaseHandle* db_handle,const char* tb_name,const CHAR* schema_str,BOOL force_new_table)
{

Esempio n. 8
0
/** \brief Nutzer mit Name, (ggf. Kürzel) und Passwort in die DB einfügen
 *
 * \param pers person*  Personen-Struktur
 * \return void
 * Eine Person in die DB einfügen, falls diese noch nicht existiert.
 * Das Passwort wird mithilfe von crypt() verschlüsselt
 */
void insert_user(person * pers){
	MYSQL *my=NULL;
	char * query=NULL;

	if(pers == NULL){
		print_exit_failure("Programm falsch.\n Wörk!");
	}

	if(email_exists(pers->email)){
		print_exit_failure("Benutzer Existiert schon!");
	}

	my=mysql_init(NULL);
	if(my == NULL){
		print_exit_failure("MYSQL init failure\n Wörk!");
	}

	if(mysql_real_connect(my, "localhost", SQL_ALTERNATE_USER, SQL_ALTERNATE_PASS, SQL_BASE, 0, NULL, 0) == NULL){
		print_exit_failure("MYSQL-connection error!");
	}

	char * salt=NULL;
	salt_generate(&salt);

    //Verhindern, dass ein bereits vorhandenes Salt zweimal verwendet wird (falls zwei Nutzer identische Passwörter wählen)
	while(salt_exists(&salt)){
		salt_generate(&salt);
	}
	char * arg=NULL;
	asprintf(&arg, "$6$%s$", salt);

	char * encr=crypt(pers->password, arg);
	char * store_pw=NULL;
	asprintf(&store_pw, "%s%s", salt, encr+strlen(arg));
	free(arg);
	free(pers->password);

	//pers->password=encr+strlen(arg);

	clean_string(pers->first_name);
	clean_string(pers->email);
	clean_string(pers->name);


	//Ist es eine Lehrer oder ein Schüler?
	if(!pers->isTeacher){
		if(asprintf(&query, "INSERT INTO Benutzer (vorname, name, email, passwort, kurse) \
					VALUES('%s', '%s', '%s', '%s', 'n/a')",
					pers->first_name, pers->name, pers->email, store_pw) == -1)
		{
			print_exit_failure("Es konnte kein Speicher angefordert werden (insert_user)");
		}
	}else{
		clean_string(pers->acronym);
		if(asprintf(&query, "INSERT INTO Benutzer (vorname, name, email, passwort, kurse, kuerzel) \
					VALUES('%s', '%s', '%s', '%s', 'n/a', '%s')",
					pers->first_name, pers->name, pers->email, store_pw, pers->acronym) == -1)
		{
			print_exit_failure("Es konnte kein Speicher angefordert werden (insert_user)");
		}
	}
	#ifdef DEBUG
	fprintf(stderr, "\nInsert dat:\n%s\n", query);
	#endif // DEBUG
	if(mysql_query(my, query)){
		#ifdef DEBUG
		fprintf(stderr, "sql_query:\n%s\nfailed\n", query);
		#endif // DEBUG
		print_exit_failure("mysql_query failed (insert)");
	}
	free(query);
	mysql_close(my);
}
Esempio n. 9
0
// insert operation.
bool insertRecord(sql::Connection *con) {
    string cid, first_name, last_name, address, phone;
    string food_name, category, stmtstr;
    int age, calorie, quantity;
    string prompt = "Please select the table you want to insert into:";
    sql::Statement *stmt;
    char tbl = selectTable(prompt);
    switch(tbl) {
    case 'c': {			// insert to Customer table.
        cout << "You have selected Customer table. " << endl;
        cout << "Please provide customer information: " << endl;
        cout << "Please enter a customer id(string): ";
        cin >> cid;
        cout << "Please enter first name(string): ";
        cin >> first_name;
        cout << "Please enter last name(string): ";
        cin >> last_name;
        cout << "Please enter age(int): ";
        cin >> age;
        cout << "Please enter address(string): ";
        cin >> address;
        cout << "Please enter phone number(string): ";
        cin >> phone;
        try {
            stmt = con->createStatement();
            stmtstr = "INSERT INTO Customer(CID, CFname, CLname, CAddress, CAge, CPhone) \
  VALUES(\""+cid+"\",\""+first_name+"\",\""+last_name+"\",\"" \
                      +address+"\",\""+itoa(age)+"\",\""+phone+"\")";
            stmt->execute(stmtstr);
            con->commit();
            cout << endl << first_name
                 << ", thanks for registration. " << endl;
            stmt->close();
        } catch(sql::SQLException &e) {
            cout << "# ERROR: SQLException in " << __FILE__;
            cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
            cout << "# ERROR: " << e.what();
            cout << " (MySQL error code: " << e.getErrorCode();
            cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        }
        break;
    }
    case 'f': {			// insert to Food table.
        cout << "You have selected Food table." << endl;
        cout << "Please enter food name(string): ";
        cin >> food_name;
        cout << "Please enter food Category(string): ";
        cin >> category;
        cout << "Please enter unit calorie(int): ";
        cin >> calorie;
        try {
            stmt = con->createStatement();
            stmtstr = "INSERT INTO Food(FName, FCategory, FUcalorie) \
  VALUES(\""+food_name+"\",\""+category+"\",\""+itoa(calorie)+"\")";
            stmt->execute(stmtstr);
            con->commit();
            cout << food_name << " is successfully registrated! " << endl;
            stmt->close();
        } catch(sql::SQLException &e) {
            cout << "# ERROR: SQLException in " << __FILE__;
            cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
            cout << "# ERROR: " << e.what();
            cout << " (MySQL error code: " << e.getErrorCode();
            cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        }
        break;
    }
    case 'o': {			// insert to FOrder table.
        cout << "You have selected Order table. " << endl;
        cout << "Please enter customer id(string): ";
        cin >> cid;
        cout << "Please enter food name(string) you want to order: ";
        cin >> food_name;
        cout << "Please enter quantity(int): ";
        cin >> quantity;
        try {
            stmt = con->createStatement();
            stmtstr = "INSERT INTO FOrder (OCID, OFName, OQuant)\
  VALUES(\""+cid+"\",\""+food_name+"\",\""+itoa(quantity)+"\")";
            stmt->execute(stmtstr);
            con->commit();
            cout << "Order successfully placed, thanks for your business!" << endl;
            stmt->close();
        } catch(sql::SQLException &e) {
            cout << "# ERROR: SQLException in " << __FILE__;
            cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
            cout << "# ERROR: " << e.what();
            cout << " (MySQL error code: " << e.getErrorCode();
            cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        }
        break;
    }
    default: {
        cout << "Sorry, wrong input." << endl;
    }
    }
    return true;
}
Esempio n. 10
0
/**
 * 功能:添加分享资源
 * 参数:resource_share - 分享资源结构体
 *	返回值:添加成功返回0,失败返回1,数据库内部错误返回-1
 */
int add_share_resource(const struct resource_share *res)
{
	
	MYSQL *mysql = NULL;
	char query_str[512];
	int affect_rows;
	if(mysql == NULL) {
		mysql = open();
	}
	
	/* insert into tbl_resoure */
	sprintf(query_str,"INSERT INTO tbl_resource(f_res_name,\
		f_res_size,f_res_md5,f_res_piececount) VALUES('%s',\
		'%s','%s','%d')", res->name,res->size,res->md5,res->piececount);
	if(mysql_query(mysql,query_str)) {
		fprintf(stderr,"Error: %s\n",mysql_error(mysql));
	}
	
	/* get sharetime */
	char *sharetime;
	sharetime = get_curr_time();

	/* insert into tbl_resource_owner */
	if(strlen(res->mac) <= 0) {

		mysql_close(mysql);
		return 0;
	}
	memset(query_str,'\0',sizeof(query_str));
	sprintf(query_str,"INSERT INTO tbl_resource_owner(f_res_md5,\
		f_res_owner,f_res_sharetime) VALUES('%s','%s','%s')",res->md5
		,res->mac,sharetime);
	if(mysql_query(mysql,query_str)) {
		fprintf(stderr,"Error: %s\n",mysql_error(mysql));
	}


	/* insert into tbl_resource_tags */
	if(strlen(res->tag) <= 0) {

		mysql_close(mysql);
		return 0;
	}
	const char *sp = TAG_SEPERATOR;
	char *tag = NULL;
	char key[512];

	int tag_failed = 0;
	strcpy(key,res->tag);
	tag = strtok(key,sp);
	while(tag) {
		memset(query_str,'\0',sizeof(query_str));
		sprintf(query_str,"INSERT INTO tbl_resource_tags(f_res_md5,\
			f_res_tags) VALUES('%s','%s')",res->md5,tag);
		if(mysql_query(mysql,query_str)) {
			fprintf(stderr,"Error: %s\n",mysql_error(mysql));

			mysql_close(mysql);
			return -1;
		}
		affect_rows = mysql_affected_rows(mysql);
		if(affect_rows < 0) {
			tag_failed = 1;
		}

		tag = strtok(NULL,sp);
	}

	mysql_close(mysql);

	return tag_failed == 1 ? -1:0;
}