void TProfile::Init(LPCTSTR section, LPCTSTR filename) { Section = section ? strnewdup(section) : 0; #if defined(UNICODE) USES_CONVERSION; #endif // Use OpenFile to track down the given filename // if can't find it, use copy of original name, // if found, use copy of full path // if (filename) { OFSTRUCT ofs; ofs.cBytes = sizeof ofs; #if defined(UNICODE) FileName = strnewdup( (OpenFile(W2A(filename), &ofs, OF_EXIST) == HFILE_ERROR) ? filename : A2W(ofs.szPathName) ); #else FileName = strnewdup( //JJH #if defined(__GNUC__) (OpenFile(filename, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : (LPCTSTR)ofs.szPathName #else (OpenFile(filename, &ofs, OF_EXIST) == HFILE_ERROR) ? filename : ofs.szPathName #endif ); #endif } else { FileName = 0; } }
// /// Sets the document path for Open and Save operations. // bool TDocument::SetDocPath(LPCTSTR path) { // Path has been set already // if (path && (path == DocPath)) { TRACEX(OwlDocView, 0, _T("SetDocPath(): path [") << tstring(path).c_str() << _T("] ")\ _T("already set!")); return true; } delete[] DocPath; DocPath = (path && *path) ? strnewdup(path) : 0; tchar title[_MAX_PATH] = _T("Unknown"); // Never used - but just in case! if (!DocPath || TCommDlg::GetFileTitle(DocPath, title, COUNTOF(title)) != 0) { CHECK(DocManager); CHECK(DocManager->GetApplication()); int len = DocManager->GetApplication()->LoadString(IDS_UNTITLED, title, COUNTOF(title)); if (DocManager->IsFlagSet(dmMDI)) wsprintf(title+len, _T("%d"), ++(DocManager->GetUntitledIndex())); } SetTitle(title); return true; // derived classes may validate path }
// /// Supports drag and drop. // TFileDroplet::TFileDroplet(const tstring& fileName, const TPoint& p, bool inClient) : FileName(strnewdup(fileName.c_str())), Point(p), InClientArea(inClient) { }
// /// Sets the title of the document. // void TDocument::SetTitle(LPCTSTR title) { if (!Title || title!=Title) { //This check allows calls like SetTitle(GetTitle()) (which is indirect way of calling ReindexFrames) delete[] Title; Title = title ? strnewdup(title) : 0; } ReindexFrames(); }
// /// Sets FileName and updates the caption of the window, /// replacing an empty name with 'Untitled' in its caption // void TEditFile::SetFileName(LPCTSTR fileName) { if (fileName != FileName) { delete[] FileName; FileName = fileName ? strnewdup(fileName) : 0; } tstring untitled(LoadString(IDS_UNTITLEDFILE)); SetDocTitle(FileName ? (LPCTSTR)FileName : untitled.c_str(), 0); }
// /// Set the text for this gadget // /// If the text stored in Text is not the same as the new text, SetText deletes the /// text stored in Text. Then, it sets TextLen to the length of the new string. If /// no text exists, it sets both Text and TextLen to 0 and then calls Invalidate to /// invalidate the rectangle. // void TTextGadget::SetText(LPCTSTR text) { // Skip processing if new text is the same as current // if (text && Text && _tcscmp(text, Text) == 0) return; delete[] Text; if (text) { Text = strnewdup(text); TextLen = ::_tcslen(Text); } else { Text = 0; TextLen = 0; } if (GetGadgetWindow()) GetGadgetWindow()->GadgetChangedSize(*this); Invalidate(); }
/* * This ist the Entry Function of this Mex-DLL */ void mexFunction(int nlhs, mxArray*plhs[], int nrhs, const mxArray*prhs[]) { mexAtExit(CloseDBs); /* * Get the current Language */ if (Language == -1) { #ifdef _WIN32 switch(PRIMARYLANGID(GetUserDefaultLangID())) { case LANG_GERMAN: Language = 1; break; default: Language = 0; } #else Language = 0; #endif } /* * Print Version Information */ if (! FirstStart) { FirstStart = true; mexPrintf (MSG_HELLO, sqlite3_libversion()); } /* * Check if the first argument is a number, then we have to use * this number as an database id. */ int db_id = 0; int FirstArg = 0; int NumArgs = nrhs; if (nrhs >= 1 && mxIsNumeric(prhs[0])) { db_id = (int) *mxGetPr(prhs[0]); if (db_id < 0 || db_id > MaxNumOfDbs) { mexPrintf(MSG_INVALIDDBHANDLE); mexErrMsgTxt(MSG_IMPOSSIBLE); } db_id --; FirstArg ++; NumArgs --; } /* * All remaining arguments have to be strings */ bool isNotOK = false; int i; for (i = FirstArg; i < nrhs; i++) { if (! mxIsChar(prhs[i])) { isNotOK = true; break; } } if (NumArgs < 1 || isNotOK) { mexPrintf(MSG_USAGE); mexErrMsgTxt(MSG_INVALIDARG); } /* * Get the first string argument, this is the command string */ char *command = getstring(prhs[FirstArg]); if (! strcmp(command, "open")) { /* * open a database. There have to be two string arguments. * The command 'open' and the database filename */ if (NumArgs != 2) { mexPrintf(MSG_NOOPENARG, mexFunctionName()); mxFree(command); mexErrMsgTxt(MSG_INVALIDARG); } // TODO: Memoryleak 'command not freed' when getstring fails char* dbname = getstring(prhs[FirstArg +1]); /* * Is there an database ID? The close the database with the same id */ if (db_id > 0 && g_dbs[db_id]) { sqlite3_close(g_dbs[db_id]); g_dbs[db_id] = 0; } /* * If there isn't an database id, then try to get one */ if (db_id < 0) { for (i = 0; i < MaxNumOfDbs; i++) { if (g_dbs[i] == 0) { db_id = i; break; } } } /* * no database id? sorry, database id table full */ if (db_id < 0) { plhs[0] = mxCreateScalarDouble((double) 0); mexPrintf(MSG_NOFREESLOT); mxFree(command); mxFree(dbname); mexErrMsgTxt(MSG_IMPOSSIBLE); } /* * Open the database */ int rc = sqlite3_open(dbname, &g_dbs[db_id]); if (rc) { /* * Anything wrong? free the database id and inform the user */ mexPrintf(MSG_CANTOPEN, sqlite3_errmsg(g_dbs[db_id])); sqlite3_close(g_dbs[db_id]); g_dbs[db_id] = 0; plhs[0] = mxCreateScalarDouble((double) 0); mxFree(command); mxFree(dbname); mexErrMsgTxt(MSG_IMPOSSIBLE); } /* * return value will be the used database id */ plhs[0] = mxCreateScalarDouble((double) db_id +1); mxFree(dbname); } else if (! strcmp(command, "close")) { /* * close a database */ /* * if the database id is < 0 than close all open databases */ if (db_id < 0) { for (i = 0; i < MaxNumOfDbs; i++) { if (g_dbs[i]) { sqlite3_close(g_dbs[i]); g_dbs[i] = 0; } } } else { /* * If the database is open, then close it. Otherwise * inform the user */ if (! g_dbs[db_id]) { mxFree(command); mexErrMsgTxt(MSG_DBNOTOPEN); } else { sqlite3_close(g_dbs[db_id]); g_dbs[db_id] = 0; } } } else if (! strcmp(command, "status")) { for (i = 0; i < MaxNumOfDbs; i++) { mexPrintf("DB Handle %d: %s\n", i, g_dbs[i] ? "OPEN" : "CLOSED"); } } else { /* * Every unknown command is treated as an sql query string */ /* * database id < 0? Thats an error... */ if (db_id < 0) { mexPrintf(MSG_INVALIDDBHANDLE); mxFree(command); mexErrMsgTxt(MSG_IMPOSSIBLE); } /* * database not open? -> error */ if (!g_dbs[db_id]) { mxFree(command); mexErrMsgTxt(MSG_DBNOTOPEN); } const char* query = command; /* * emulate the "show tables" sql query */ if (! strcmpi(query, "show tables")) { query = "SELECT name as tablename FROM sqlite_master " "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' " "UNION ALL " "SELECT name as tablename FROM sqlite_temp_master " "WHERE type IN ('table','view') " "ORDER BY 1"; } /* * complete the query */ if (sqlite3_complete(query)) { mxFree(command); mexErrMsgTxt(MSG_INVQUERY); } sqlite3_stmt *st; /* * and prepare it * if anything is wrong with the query, than complain about it. */ if (sqlite3_prepare_v2(g_dbs[db_id], query, -1, &st, 0)) { if (st) sqlite3_finalize(st); mxFree(command); mexErrMsgIdAndTxt(TransErrToIdent(g_dbs[db_id]), sqlite3_errmsg(g_dbs[db_id])); } /* * Any results? */ int ncol = sqlite3_column_count(st); if (ncol > 0) { char **fieldnames = new char *[ncol]; /* Column names */ Values* allrows = 0; /* All query results */ Values* lastrow = 0; /* pointer to the last result row */ int rowcount = 0; /* number of result rows */ /* * Get the column names of the result set */ for(i=0; i<ncol; i++) { const char *cname = sqlite3_column_name(st, i); fieldnames[i] = new char [strlen(cname) +1]; strcpy (fieldnames[i], cname); /* * replace invalid chars by '_', so we can build * valid MATLAB structs */ char *mk_c = fieldnames[i]; while (*mk_c) { if ((*mk_c == ' ') || (*mk_c == '*') || (*mk_c == '?')) *mk_c = '_'; mk_c++; } } /* * get the result rows from the engine * * We cannot get the number of result lines, so we must * read them in a loop and save them into an temporary list. * Later, we can transfer this List into an MATLAB array of structs. * This way, we must allocate enough memory for two result sets, * but we save time by allocating the MATLAB Array at once. */ for(;;) { /* * Advance to teh next row */ int step_res = sqlite3_step(st); /* * no row left? break out of the loop */ if (step_res != SQLITE_ROW) break; /* * get new memory for the result */ Values* RecordValues = new Values(ncol); Value *v = RecordValues->m_Values; for (int j = 0; j < ncol; j++, v++) { int fieldtype = sqlite3_column_type(st,j); v->m_Type = fieldtype; switch (fieldtype) { case SQLITE_NULL: v->m_NumericValue = g_NaN; break; case SQLITE_INTEGER: v->m_NumericValue = (double) sqlite3_column_int(st, j); break; case SQLITE_FLOAT: v->m_NumericValue = (double) sqlite3_column_double(st, j); break; case SQLITE_TEXT: v->m_StringValue = strnewdup((const char*) sqlite3_column_text(st, j)); break; default: mxFree(command); mexErrMsgTxt(MSG_UNKNWNDBTYPE); } } /* * and add this row to the list of all result rows */ if (! lastrow) { allrows = lastrow = RecordValues; } else { lastrow->m_NextValues = RecordValues; lastrow = lastrow->m_NextValues; } /* * we have one more... */ rowcount ++; } /* * end the sql engine */ sqlite3_finalize(st); /* * got nothing? return an empty result to MATLAB */ if (rowcount == 0 || ! allrows) { if (!( plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL) )) { mxFree(command); mexErrMsgTxt(MSG_CANTCREATEOUTPUT); } } else { /* * Allocate an array of MATLAB structs to return as result */ int ndims[2]; ndims[0] = rowcount; ndims[1] = 1; if (( plhs[0] = mxCreateStructArray (2, ndims, ncol, (const char**)fieldnames)) == 0) { mxFree(command); mexErrMsgTxt(MSG_CANTCREATEOUTPUT); } /* * transfer the result rows from the temporary list into the result array */ lastrow = allrows; i = 0; while(lastrow) { Value* recordvalue = lastrow->m_Values; for (int j = 0; j < ncol; j++, recordvalue++) { if (recordvalue -> m_Type == SQLITE_TEXT) { mxArray* c = mxCreateString(recordvalue->m_StringValue); mxSetFieldByNumber(plhs[0], i, j, c); } else if (recordvalue -> m_Type == SQLITE_NULL && !NULLasNaN) { mxArray* out_double = mxCreateDoubleMatrix(0,0,mxREAL); mxSetFieldByNumber(plhs[0], i, j, out_double); } else { mxArray* out_double = mxCreateDoubleScalar(recordvalue->m_NumericValue); mxSetFieldByNumber(plhs[0], i, j, out_double); } } allrows = lastrow; lastrow = lastrow->m_NextValues; delete allrows; i++; } } for(int i=0; i<ncol; i++) delete [] fieldnames[i]; delete [] fieldnames; } else { /* * no result, cleanup the sqlite engine */ int res = sqlite3_step(st); sqlite3_finalize(st); if (res != SQLITE_DONE) { mxFree(command); mexErrMsgIdAndTxt(TransErrToIdent(g_dbs[db_id]), sqlite3_errmsg(g_dbs[db_id])); } } } mxFree(command); }