void SQLite::ConstructUpdateSqlStatement(UpdateStatement* Statement, sqlite3_stmt** SQLiteStatement) { const char* tail = NULL; std::stringstream query; query << "UPDATE " << Statement->GetTable() << " SET "; for (std::vector<DAL::InsertStatement::Column*>::iterator it = Statement->ValuesBegin(); it != Statement->ValuesEnd(); it++) { if (!this->ParseValue(*it)) { throw WarningException("SQLite::ConstructUpdateSqlStatement", "Unable to add columnvalue " "to updatestatement: columnname: " + (*it)->Name + " columnvalue: " + (*it)->Value + " table: " + Statement->GetTable(), HERE); } query << (*it)->Name << " = " << "?,"; } std::string queryString = query.str(); boost::algorithm::trim_right_if(queryString, boost::algorithm::is_any_of(",")); queryString += " " + Statement->GetWhere() + ";"; int result = sqlite3_prepare_v2(this->database, queryString.c_str(), queryString.size(), SQLiteStatement, &tail); if (result != SQLITE_OK) { throw WarningException("SQLite::ConstructUpdateSqlStatement", Statement->ToString() + std::string(" sqlite3_prepare_v2 returned ") + boost::lexical_cast<std::string>(result), HERE); } int columnIndex = 1; for (std::vector<DAL::InsertStatement::Column*>::iterator it = Statement->ValuesBegin(); it != Statement->ValuesEnd(); it++) { this->BindParameter(*it, SQLiteStatement, columnIndex); columnIndex++; } }
bool SQLite::ExecuteNonQuery(const char* SqlStatement) { bool returnValue = false; Logger::GetInstance().Debug("SQLite::ExecuteNonQuery", SqlStatement); try { char* error = NULL; if (sqlite3_exec(this->database, SqlStatement, NULL, NULL, &error) != SQLITE_OK) { std::string errorMsg = "Database::Execute FATAL: Could not execute " + std::string(SqlStatement) + " : " + std::string(error); sqlite3_free(error); throw WarningException("DAL::ExecuteNonQuery", errorMsg, HERE); } returnValue = true; } catch (std::exception &e) { throw WarningException("DAL::ExecuteNonQuery", e.what(), HERE); } catch (std::string& msg) { throw WarningException("DAL::ExecuteNonQuery", msg, HERE); } catch (const char* e) { throw WarningException("DAL::ExecuteNonQuery", e, HERE); } return returnValue; }
DAL::DataSet* SQLite::ExecuteQuery(const char* SqlStatement) { SQLite::SQLiteDataSet* returnValue = new SQLite::SQLiteDataSet(); Logger::GetInstance().Debug("SQLite::ExecuteQuery", SqlStatement); try { char* error = NULL; if (sqlite3_exec(this->database, SqlStatement, SQLite::ExecuteCallback, returnValue, &error) != SQLITE_OK) { std::string errorMsg = "SQLite::ExecuteQuery FATAL: Could not execute " + std::string(SqlStatement) + " : " + std::string(error); sqlite3_free(error); throw WarningException("SQLite::ExecuteQuery", errorMsg, HERE); } } catch (WarningException& e) { throw WarningException(e, HERE); } catch (std::exception &e) { throw FatalException("DAL::ExecuteQuery", e.what(), HERE); } catch (...) { throw FatalException("DAL::ExecuteQuery", "Unspecified exception while executing an sql query.", HERE); } return returnValue; }
void SQLite::BindParameter(DAL::InsertStatement::Column* Column, sqlite3_stmt** SQLiteStatement, int ColumnIndex) { int result; switch (Column->Type) { case DAL::PreparedStatement::Bool: case DAL::PreparedStatement::Int: case DAL::PreparedStatement::SmallInt: result = sqlite3_bind_int(*SQLiteStatement, ColumnIndex, boost::lexical_cast<int>(Column->Value)); if (result != SQLITE_OK) { throw WarningException("SQLite::BindParameter", std::string("Problem with binding int parameter. Returnvalue: ") + boost::lexical_cast<std::string>(result), HERE); } break; case DAL::PreparedStatement::BigInt: result = sqlite3_bind_int64(*SQLiteStatement, ColumnIndex, boost::lexical_cast<long long>(Column->Value)); if (result != SQLITE_OK) { throw WarningException("SQLite::BindParameter", std::string("Problem with binding bigint parameter. Returnvalue: ") + boost::lexical_cast<std::string>(result), HERE); } break; case DAL::PreparedStatement::Real: case DAL::PreparedStatement::Decimal: result = sqlite3_bind_double(*SQLiteStatement, ColumnIndex, boost::lexical_cast<double>(Column->Value)); if (result != SQLITE_OK) { throw WarningException("SQLite::BindParameter", std::string("Problem with binding real parameter. Returnvalue: ") + boost::lexical_cast<std::string>(result), HERE); } break; case DAL::PreparedStatement::Blob: result = sqlite3_bind_blob(*SQLiteStatement, ColumnIndex, Column->Value.c_str(), Column->Value.size(), SQLITE_TRANSIENT); if (result != SQLITE_OK) { throw WarningException("SQLite::BindParameter", std::string("Problem with binding blob parameter. Returnvalue: ") + boost::lexical_cast<std::string>(result), HERE); } break; case DAL::PreparedStatement::Text: case DAL::PreparedStatement::Statement: case DAL::PreparedStatement::Date: case DAL::PreparedStatement::TimeStamp: case DAL::PreparedStatement::Uuid: result = sqlite3_bind_text(*SQLiteStatement, ColumnIndex, Column->Value.c_str(), Column->Value.size(), SQLITE_TRANSIENT); if (result != SQLITE_OK) { throw WarningException("SQLite::BindParameter", std::string("Problem with binding text parameter. Returnvalue: ") + boost::lexical_cast<std::string>(result), HERE); } break; default: break; } }
void Message(MessageType type, const char* message, const char* title) { if (running_unittests) { throw WarningException(message); } int flags = 1; switch (type) { case MESSAGEBOX_ERROR: flags = SDL_MESSAGEBOX_ERROR; if (title == NULL) title = "Error"; break; case MESSAGEBOX_INFORMATION: flags = SDL_MESSAGEBOX_INFORMATION; if (title == NULL) title = "Information"; break; case MESSAGEBOX_WARNING: flags = SDL_MESSAGEBOX_WARNING; if (title == NULL) title = "Warning"; break; default: Int3(); title = ""; // Remove warning about unitialized variable break; } SDL_ShowSimpleMessageBox(flags, title, message, os::getSDLMainWindow()); }
void ClientConnection::UpdateLocalClipboard(char *buf, int len) { if (m_opts.m_DisableClipboard) return; // Copy to wincontents replacing LF with CR-LF char *wincontents = new char[len * 2 + 1]; int j = 0;; for (int i = 0; m_netbuf[i] != 0; i++, j++) { if (buf[i] == '\x0a') { wincontents[j++] = '\x0d'; len++; } wincontents[j] = buf[i]; } wincontents[j] = '\0'; // The clipboard should not be modified by more than one thread at once { omni_mutex_lock l(m_clipMutex); if (!OpenClipboard(m_hwnd)) { throw WarningException(sz_C1); } if (! ::EmptyClipboard()) { throw WarningException(sz_C2); } // Allocate a global memory object for the text. HGLOBAL hglbCopy = GlobalAlloc(GMEM_DDESHARE, (len +1) * sizeof(TCHAR)); if (hglbCopy != NULL) { // Lock the handle and copy the text to the buffer. LPTSTR lptstrCopy = (LPTSTR) GlobalLock(hglbCopy); memcpy(lptstrCopy, wincontents, len * sizeof(TCHAR)); lptstrCopy[len] = (TCHAR) 0; // null character GlobalUnlock(hglbCopy); // Place the handle on the clipboard. SetClipboardData(CF_TEXT, hglbCopy); } delete [] wincontents; if (! ::CloseClipboard()) { throw WarningException(sz_C3); } } }
int SQLite::ExecuteCallback(void* DataSetParam, int ColumnCount, char** ColumnValue, char** ColumnName) { try { SQLite::SQLiteDataSet* dataSet = (SQLite::SQLiteDataSet*) DataSetParam; // Set columnnames std::string value; if (!dataSet->ColumnNames().size()) { for (int i = 0; i < ColumnCount; i++) { value = (ColumnName[i]) ? ColumnName[i] : ""; dataSet->ColumnNames().push_back(value); } } // Set columnValues SQLite::SQLiteDataSet::RowValuesType* newRow = dataSet->AddRow(); for (int i = 0; i < ColumnCount; i++) { value = (ColumnValue[i]) ? ColumnValue[i] : ""; if (value == "NULL") value = ""; newRow->push_back(value); } } catch (std::exception &e) { throw WarningException("DAL::ExecuteCallback", e.what(), HERE); } catch (std::string& msg) { throw WarningException("DAL::ExecuteCallback", msg, HERE); } catch (...) { throw WarningException("DAL::ExecuteCallback", "Unspecified exception while executing sqlite callback.", HERE); } return 0; }
void SQLite::ConstructSqlStatement(PreparedStatement* Statement, sqlite3_stmt** SQLiteStatement) { switch (Statement->GetType()) { case Insert: this->ConstructInsertSqlStatement((InsertStatement*) Statement, SQLiteStatement); break; case Update: this->ConstructUpdateSqlStatement((UpdateStatement*) Statement, SQLiteStatement); break; default: throw WarningException("SQLite::ConstructSqlStatement", "Unknown prepared statement type", HERE); } }
bool SQLite::ExecuteNonQuery(PreparedStatement* Statement) { bool returnValue = true; sqlite3_stmt* statement = NULL; this->ConstructSqlStatement(Statement, &statement); Logger::GetInstance().Debug("SQLite::ExecuteNonQuery", Statement->ToString()); int result = sqlite3_step(statement); if (result != SQLITE_DONE && result != SQLITE_OK) { sqlite3_reset(statement); throw WarningException("SQLite::ExecuteNonQuery", "Could not execute non query " + Statement->ToString() + " : " + std::string(sqlite3_errmsg(this->database)), HERE); } sqlite3_finalize(statement); return returnValue; }
Flasher::Flasher(int port) { // Create a dummy window. We don't use it for anything except // receiving socket events, so a seperate listening thread would // probably be easier! WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = Flasher::WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = pApp->m_instance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = (const char *) NULL; wndclass.lpszClassName = FLASHER_CLASS_NAME; wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wndclass); m_hwnd = CreateWindow(FLASHER_CLASS_NAME, FLASHER_CLASS_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, pApp->m_instance, NULL); // record which client created this window helper::SafeSetWindowUserData(m_hwnd, (LONG)this); // Select a font for displaying user name LOGFONT lf; memset(&lf, 0, sizeof(lf)); lf.lfHeight = FLASHFONTHEIGHT; lf.lfWeight = FW_BOLD; lf.lfItalic = 1; lf.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS; lf.lfFaceName[0] = '\0'; m_hfont = CreateFontIndirect(&lf); if (m_hfont == NULL) { vnclog.Print(1, _T("FAILED TO SELECT FLASHER FONT!\n")); } // Create a listening socket struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; m_sock = socket(AF_INET, SOCK_STREAM, 0); if (!m_sock) throw WarningException(sz_G1); try { int one = 1, res = 0; // If we use the SO_REUSEADDR option then you can run multiple daemons // because the bind doesn't return an error. Only one gets the accept, // but when that process dies it hands back to another. This may or may // not be desirable. We don't use it. //int res = setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &one, sizeof(one)); //if (res == SOCKET_ERROR) // throw WarningException("Error setting Flasher socket options"); res = bind(m_sock, (struct sockaddr *)&addr, sizeof(addr)); if (res == SOCKET_ERROR) throw WarningException(sz_G2); res = listen(m_sock, 5); if (res == SOCKET_ERROR) throw WarningException(sz_G3); } catch (...) { closesocket(m_sock); m_sock = 0; throw; } // Send a message to specified window on an incoming connection WSAAsyncSelect (m_sock, m_hwnd, WM_SOCKEVENT, FD_ACCEPT | FD_CLOSE); }
Daemon::Daemon(int port) { // Create a dummy window WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wndclass.lpfnWndProc = Daemon::WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = pApp->m_instance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = (const char *) NULL; wndclass.lpszClassName = DAEMON_CLASS_NAME; wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wndclass); m_hwnd = CreateWindow(DAEMON_CLASS_NAME, DAEMON_CLASS_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, pApp->m_instance, NULL); // record which client created this window helper::SafeSetWindowUserData(m_hwnd, (LONG_PTR)this); // Load a popup menu m_hmenu = LoadMenu(pApp->m_instance, MAKEINTRESOURCE(IDR_TRAYMENU)); // sf@2003 - Store Port number for systray display m_nPort = port; // Create a listening socket struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; m_deamon_sock = socket(AF_INET, SOCK_STREAM, 0); if (!m_deamon_sock) throw WarningException(sz_I1); try { int res = 0; res = bind(m_deamon_sock, (struct sockaddr *)&addr, sizeof(addr)); if (res == SOCKET_ERROR) throw WarningException(sz_I2); res = listen(m_deamon_sock, 5); if (res == SOCKET_ERROR) throw WarningException(sz_I3); } catch (...) { closesocket(m_deamon_sock); m_deamon_sock = INVALID_SOCKET; throw; } // Send a message to specified window on an incoming connection WSAAsyncSelect (m_deamon_sock, m_hwnd, WM_SOCKEVENT, FD_ACCEPT); // Create the tray icon AddTrayIcon(); // A timer checks that the tray icon is intact m_timer = SetTimer( m_hwnd, IDT_TRAYTIMER, 15000, NULL); }
Daemon::Daemon(int port) { // Create a dummy window WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wndclass.lpfnWndProc = Daemon::WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = pApp->m_instance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = (const char *) NULL; wndclass.lpszClassName = DAEMON_CLASS_NAME; wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wndclass); m_hwnd = CreateWindow(DAEMON_CLASS_NAME, DAEMON_CLASS_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, pApp->m_instance, NULL); // record which client created this window SetWindowLong(m_hwnd, GWL_USERDATA, (LONG) this); // Load a popup menu m_hmenu = LoadMenu(pApp->m_instance, MAKEINTRESOURCE(IDR_TRAYMENU)); // Create a listening socket struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; m_sock = socket(AF_INET, SOCK_STREAM, 0); if (!m_sock) throw WarningException("Error creating Daemon socket"); int one = 1, res = 0; //res = setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &one, sizeof(one)); //if (res == SOCKET_ERROR) { // closesocket(m_sock); // m_sock = 0; // throw WarningException("Error setting Daemon socket options"); //} res = bind(m_sock, (struct sockaddr *)&addr, sizeof(addr)); if (res == SOCKET_ERROR) { closesocket(m_sock); m_sock = 0; throw WarningException("Error binding Daemon socket"); } res = listen(m_sock, 5); if (res == SOCKET_ERROR) { closesocket(m_sock); m_sock = 0; throw WarningException("Error when Daemon listens"); } // Send a message to specified window on an incoming connection WSAAsyncSelect (m_sock, m_hwnd, WM_SOCKEVENT, FD_ACCEPT); // Create the tray icon AddTrayIcon(); // A timer checks that the tray icon is intact m_timer = SetTimer( m_hwnd, IDT_TRAYTIMER, 15000, NULL); }