/** @brief Read a long long from an input stream * * @param in wxInputStream& * @param out_LongLong long long& * @return bool * */ static bool ReadLongLong( wxInputStream& in, long long& out_LongLong ) { long long val = 0; if (!in.CanRead()) return false; in.Read(&val, sizeof(val)); out_LongLong = val; return true; }
void MyPipeFrame::DoGetFromStream(wxTextCtrl *text, wxInputStream& in) { while ( in.CanRead() ) { char buffer[4096]; buffer[in.Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = '\0'; text->AppendText(buffer); } }
/** @brief Read an int from an input stream * * @param in wxInputStream& * @param out_Int int& * @return bool * */ static bool ReadInt( wxInputStream& in, int& out_Int ) { int val = 0; if (!in.CanRead()) return false; in.Read(&val, sizeof(val)); out_Int = val; return true; }
// parse header section: just skip everything bool DXFRenderer::ParseHeader(wxInputStream& stream) { wxTextInputStream text(stream); wxString line1, line2; while (stream.CanRead()) { GetLines(text, line1, line2); if (line1 == "0" && line2 == "ENDSEC") return true; } return false; }
/** @brief Read a tokendatabase from disk * * @param tokenDatabase The tokendatabase to read into * @param in The wxInputStream to read from * @return true if the operation succeeded, false otherwise * */ bool ClTokenDatabase::ReadIn( ClTokenDatabase& tokenDatabase, wxInputStream& in ) { in.SeekI(4); // Magic number int version = 0; if (!ReadInt(in, version)) return false; int i = 0; if (version != 0x01) { return false; } tokenDatabase.Clear(); int read_count = 0; wxMutexLocker(tokenDatabase.m_Mutex); while (in.CanRead()) { int packetType = 0; if (!ReadInt(in, packetType)) return false; switch (packetType) { case ClTokenPacketType_filenames: if (!ClFilenameDatabase::ReadIn(tokenDatabase.m_FileDB, in)) return false; break; case ClTokenPacketType_tokens: int packetCount = 0; if (!ReadInt(in, packetCount)) return false; for (i = 0; i < packetCount; ++i) { ClAbstractToken token; if (!ClAbstractToken::ReadIn(token, in)) return false; if (token.fileId != -1) { //ClTokenId tokId = tokenDatabase.InsertToken(token); //fprintf( stdout, " '%s' / '%s' / fId=%d location=%d:%d hash=%d dbEntryId=%d\n", (const char*)token.identifier.mb_str(), (const char*)token.displayName.mbc_str(), token.fileId, token.location.line, token.location.column, token.tokenHash, tokId ); read_count++; } } break; } } return true; }
/** @brief Read a string from an input stream * * @param in wxInputStream& * @param out_String wxString& * @return bool * */ static bool ReadString( wxInputStream& in, wxString& out_String ) { int len; if (!ReadInt(in, len)) return false; if (len == 0) { out_String = out_String.Truncate(0); return true; } if (!in.CanRead()) return false; char buffer[len + 1]; in.Read( buffer, len ); buffer[len] = '\0'; out_String = wxString::FromUTF8(buffer); return true; }
// parse tables section: save layers name and colour bool DXFRenderer::ParseTables(wxInputStream& stream) { wxTextInputStream text(stream); wxString line1, line2; bool inlayer=false; DXFLayer layer; while (stream.CanRead()) { GetLines(text, line1, line2); if (line1 == "0" && inlayer) { // flush layer if (!layer.name.IsEmpty() && layer.colour != -1) { DXFLayer *p = new DXFLayer; p->name = layer.name; p->colour = layer.colour; m_layers.Append(p); } layer = DXFLayer(); inlayer = false; } if (line1 == "0" && line2 == "ENDSEC") return true; else if (line1 == "0" && line2 == "LAYER") inlayer = true; else if (inlayer) { if (line1 == "2") // layer name layer.name = line2; else if (line1 == "62") // ACI colour { long l; line2.ToLong(&l); layer.colour = l; } } } return false; }
// parse and load a DXF file // currently pretty limited, but knows enough to handle 3DFACEs and LINEs bool DXFRenderer::Load(wxInputStream& stream) { Clear(); wxTextInputStream text(stream); wxString line1, line2; while (stream.CanRead()) { GetLines(text, line1, line2); if (line1 == "999") // comment continue; else if (line1 == "0" && line2 == "SECTION") { GetLines(text, line1, line2); if (line1 == "2") { if (line2 == "HEADER") { if (!ParseHeader(stream)) return false; } else if (line2 == "TABLES") { if (!ParseTables(stream)) return false; } else if (line2 == "ENTITIES") { if (!ParseEntities(stream)) return false; } } } } NormalizeEntities(); m_loaded = true; return true; }
// parse entities section: save 3DFACE and LINE entities bool DXFRenderer::ParseEntities(wxInputStream& stream) { wxTextInputStream text(stream); wxString line1, line2; int state = 0; // 0: none, 1: 3DFACE, 2: LINE DXFVector v[4]; int colour = -1; wxString layer; while (stream.CanRead()) { GetLines(text, line1, line2); if (line1 == "0" && state > 0) { // flush entity if (state == 1) // 3DFACE { DXFFace *p = new DXFFace; p->v0 = v[0]; p->v1 = v[1]; p->v2 = v[2]; p->v3 = v[3]; p->CalculateNormal(); if (colour != -1) p->colour = colour; else p->colour = GetLayerColour(layer); m_entities.Append(p); colour = -1; layer.clear(); v[0] = v[1] = v[2] = v[3] = DXFVector(); state = 0; } else if (state == 2) // LINE { DXFLine *p = new DXFLine; p->v0 = v[0]; p->v1 = v[1]; if (colour != -1) p->colour = colour; else p->colour = GetLayerColour(layer); m_entities.Append(p); colour = -1; layer.clear(); v[0] = v[1] = v[2] = v[3] = DXFVector(); state = 0; } } if (line1 == "0" && line2 == "ENDSEC") return true; else if (line1 == "0" && line2 == "3DFACE") state = 1; else if (line1 == "0" && line2 == "LINE") state = 2; else if (state > 0) { const double d=ToDouble(line2); if (line1 == "10") v[0].x = d; else if (line1 == "20") v[0].y = d; else if (line1 == "30") v[0].z = d; else if (line1 == "11") v[1].x = d; else if (line1 == "21") v[1].y = d; else if (line1 == "31") v[1].z = d; else if (line1 == "12") v[2].x = d; else if (line1 == "22") v[2].y = d; else if (line1 == "32") v[2].z = d; else if (line1 == "13") v[3].x = d; else if (line1 == "23") v[3].y = d; else if (line1 == "33") v[3].z = d; else if (line1 == "8") // layer layer = line2; else if (line1 == "62") // colour { long l; line2.ToLong(&l); colour = l; } } } return false; }
bool wxDatabaseConfig::LoadStream(wxInputStream& inStream, wxString* err) { SetRootPath(m_entry); if (m_viewName.IsEmpty()) { m_viewName = m_self->GetAppName(); if (m_viewName.IsEmpty()) { m_viewName = viewname_default; } } m_viewName.Replace(wxCONFIG_PATH_SEPARATOR, wxEmptyString); if (!inStream.CanRead()) { if (err) err->Append("can't read"); return false; } wxFileConfig config(inStream); /* SQLite doesn't support stored procedures nor recursion. The best compromise between all the flavours of databases is to store the group paths as a closure set and to access the groups and entries via a view with triggers instead of accessing the underlying tables directly. table "tablename_entry" contains the rows for both group names and entry name/values table "tablename_path" contains a closure set for the group paths view "viewname" (and its associated triggers) provide the interface used by this code */ wxString sqlCreateTablePath; wxString sqlCreateTableEntry; wxString sqlCreateViewPath; wxString sqlCreateTriggerInsteadOfInsertViewPath; wxString sqlCreateTriggerInsteadOfUpdateViewPath; wxString sqlCreateTriggerInsteadOfDeleteViewPath; wxString sqlAddEntry; wxString sqlDelEntry; wxString sqlEditEntry; wxString sqlFindEntries; wxString sqlDropAll; // should work on all DBs? sqlCreateTablePath << "IF NOT EXISTS (SELECT * FROM sys.views WHERE name = '" << viewname <<"') " "AND NOT EXISTS (SELECT * FROM sys.objects WHERE type IN ('U') AND name = '" << tablename_path << "') EXECUTE('" "CREATE TABLE " << tablename_path << "(" "pid INT NOT NULL," "id INT NOT NULL," "PRIMARY KEY (pid, id)," "FOREIGN KEY (pid) REFERENCES " << tablename_entry << "(id)," "FOREIGN KEY (id) REFERENCES " << tablename_entry << "(id)" ")" "')" ; sqlCreateTableEntry << "IF NOT EXISTS (SELECT * FROM sys.views WHERE name = '" << viewname <<"') " "AND NOT EXISTS (SELECT * FROM sys.objects WHERE type IN ('U') AND name = '" << tablename_entry <<"') EXECUTE('" "CREATE TABLE " << tablename_entry << "(" "id INT IDENTITY PRIMARY KEY," "name VARCHAR(255) NOT NULL," "value VARCHAR(255) NULL" ")" "')" ; sqlAddEntry << "INSERT INTO " << viewname <<" (id, name, value) VALUES (?, ?, ?)" ; sqlDelEntry << "DELETE FROM " << viewname <<" WHERE id = ?" ; sqlEditEntry << "UPDATE " << viewname <<" SET name = ?, value = ? WHERE id = ?" ; sqlFindEntries << "SELECT id, name, value, path, isgroup FROM " << viewname <<" WHERE path LIKE ?" ; sqlDropAll << "DROP VIEW " << viewname <<"\n" "DROP TABLE " << tablename_path <<"\n" "DROP TABLE " << tablename_entry <<"\n" ; sqlCreateTriggerInsteadOfInsertViewPath << "IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = '" << triggername_insertviewpath <<"') EXECUTE('\n" "CREATE TRIGGER [" << triggername_insertviewpath <<"] ON " << viewname <<" INSTEAD OF INSERT\n" "AS\n" "BEGIN\n" "\tINSERT INTO " << tablename_entry <<" (name, value) SELECT i.name, i.value FROM inserted i\n" "\tINSERT INTO " << tablename_path <<" (pid, id)\n" "\tSELECT p.pid, (SELECT MAX(id) FROM " << tablename_entry <<")\n" "\tFROM\n" "\t\t" << tablename_path <<" p\n" "\t\tJOIN inserted i ON p.id = i.id -- treat i.id as the pid p\n" "\tUNION\n" "\tSELECT MAX(id), MAX(id) FROM " << tablename_entry <<"\n" //"\tSELECT id, name, value FROM " << tablename_entry <<" WHERE id IN (SELECT MAX(id) FROM " << tablename_entry <<")\n" "END\n" "')" ; sqlCreateTriggerInsteadOfUpdateViewPath << "IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = '" << triggername_updateviewpath <<"') EXECUTE('\n" "CREATE TRIGGER [" << triggername_updateviewpath <<"] ON " << viewname <<" INSTEAD OF UPDATE\n" "AS\n" "BEGIN\n" "\tUPDATE " << tablename_entry <<" SET\n" "\t\t" << tablename_entry <<".name = i.name,\n" "\t\t" << tablename_entry <<".value = CASE WHEN " << tablename_entry <<".value IS NULL THEN NULL ELSE i.value END\n" "\tFROM\n" "\t\tinserted i\n" "\tWHERE\n" "\t\t" << tablename_entry <<".id = i.id\n" //"\tSELECT e.id, e.name, e.value FROM " << tablename_entry <<" e JOIN inserted i ON i.id = e.id\n" "END\n" "')" ; sqlCreateTriggerInsteadOfDeleteViewPath << "IF NOT EXISTS (SELECT * FROM sys.triggers WHERE name = '" << triggername_deleteviewpath <<"') EXECUTE('\n" "CREATE TRIGGER [" << triggername_deleteviewpath <<"] ON " << viewname <<" INSTEAD OF DELETE\n" "AS\n" "BEGIN\n" "\tDELETE p\n" "\tFROM\n" "\t\t" << tablename_path <<" c\n" "\t\tJOIN " << tablename_path <<" p ON p.id = c.id\n" "\t\tJOIN deleted d ON c.pid = d.id -- treat d.id as the id\n" "\tDELETE e\n" "\tFROM\n" "\t\t" << tablename_entry <<" e\n" "\tWHERE\n" "\t\te.id NOT IN (SELECT id FROM " << tablename_path <<")\n" "END\n" "')" ; m_pDatabase = wxDatabase::GetDatabase(config, err); if (m_pDatabase == NULL) { if (err) err->Append("cannot establish database connection from"); return false; } // if necessary add the database library path to the system path if (!m_pDatabase->GetLibraryPath().IsEmpty()) { wxString path; wxGetEnv("PATH", &path); #ifdef WIN32 path.Append(";"); #else path.Append(":"); #endif path.Append(m_pDatabase->GetLibraryPath()); wxSetEnv("PATH", path); } if (false); #if wxUSE_DATABASE_SQLITE else if (m_pDatabase->GetTypeName().IsSameAs("SQLITE")) { sqlCreateViewPath << "IF NOT EXISTS (SELECT * FROM sys.views WHERE name = '" << viewname <<"') EXECUTE('" "CREATE VIEW [" << viewname <<"]\n" "AS\n" "SELECT\n" "\te.id,\n" "\te.name,\n" "\te.value,\n" "\tCONVERT(VARCHAR(255), (\n" "\t\tSELECT\n" "\t\t\t''/''+dbo.GROUP_CONCAT_D(ie.name, ''/'')\n" "\t\tFROM\n" "\t\t\t" << tablename_path <<" ic\n" "\t\t\tJOIN " << tablename_path <<" ip ON ip.id = ic.pid\n" "\t\t\tJOIN " << tablename_entry <<" ie ON ie.id = ip.pid\n" "\t\tWHERE\n" "\t\t\tic.id = ic.pid\n" "\t\t\tAND\n" "\t\t\tic.id = c.id\n" "\t\tGROUP BY\n" "\t\t\tic.id\n" "\t)) AS [path],\n" "\tCASE WHEN e.value IS NULL THEN 1 ELSE 0 END AS [isgroup]\n" "FROM\n" "\t" << tablename_path <<" c\n" "\tJOIN " << tablename_entry <<" e ON e.id = c.id\n" "WHERE\n" "\tc.id = c.pid\n" "')" ; } #endif #if wxUSE_DATABASE_POSTGRESQL else if (m_pDatabase->GetTypeName().IsSameAs("POSTGRESQL")) { } #endif #if wxUSE_DATABASE_MYSQL else if (m_pDatabase->GetTypeName().IsSameAs("MYSQL")) { } #endif #if wxUSE_DATABASE_ODBC else if (m_pDatabase->GetTypeName().IsSameAs("ODBC")) { } #endif #if wxUSE_DATABASE_TDS else if (m_pDatabase->GetTypeName().IsSameAs("TDS")) { sqlCreateViewPath << "IF NOT EXISTS (SELECT * FROM sys.views WHERE name = '" << viewname <<"') EXECUTE('" "CREATE VIEW [" << viewname <<"]\n" "AS\n" "SELECT\n" "\te.id,\n" "\te.name,\n" "\te.value,\n" "\tCONVERT(VARCHAR(255), (\n" "\t\tSELECT\n" "\t\t\t''/''+e.name AS [text()]\n" "\t\tFROM\n" "\t\t\t" << tablename_path <<" p\n" "\t\t\tJOIN " << tablename_entry <<" e ON e.id = p.pid\n" "\t\tWHERE\n" "\t\t\tp.id = c.pid\n" "\t\tFOR XML PATH('''')\n" "\t)) AS [path],\n" "\tCASE WHEN e.value IS NULL THEN 1 ELSE 0 END AS [isgroup]\n" "FROM\n" "\t" << tablename_path <<" c\n" "\tJOIN " << tablename_entry <<" e ON e.id = c.id\n" "WHERE\n" "\tc.id = c.pid\n" "')" ; } #endif m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateTableEntry))); m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateTablePath))); m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateViewPath))); m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateTriggerInsteadOfInsertViewPath))); m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateTriggerInsteadOfUpdateViewPath))); m_pDatabase->CloseStatement(this->ExecuteStatement(this->PrepareStatement(sqlCreateTriggerInsteadOfDeleteViewPath))); m_pStatementSqlAddEntry = this->PrepareStatement(sqlAddEntry); m_pStatementSqlDelEntry = this->PrepareStatement(sqlDelEntry); m_pStatementSqlEditEntry = this->PrepareStatement(sqlEditEntry); m_pStatementSqlFindEntries = this->PrepareStatement(sqlFindEntries); m_pStatementSqlDropAll = this->PrepareStatement(sqlDropAll); return true; }