int getFile( const char *filename, const struct stat *statptr, int flag ) { char *position ; // pointer to the requested filename char target[BUFFER_SIZE] = { '\0' } ; // value of the requested filename char theFileName[BUFFER_SIZE] = { '\0' } ; int index = 0 ; // used in determining the postion of the filename switch ( flag ) { case FTW_F : position = strrchr(filename, '/' ) ; _assert( __FILE__, __LINE__, position ) ; index = filename - position; if( index < 0 ) index *= -1; strcpy( target, filename + index + 1 ) ; strcat( theFileName, "file://" ) ; if( strcmp( operatorType, "~" ) == 0 ) { if( strstr( target, queryName ) != NULL ) { strcat( theFileName, filename ) ; nameResults = addResultItem ( nameResults, theFileName ); } } else if( strcmp( operatorType, "EQ" ) == 0 ) { if( strcmp( target, queryName ) == 0 ) { strcat( theFileName, filename ) ; nameResults = addResultItem ( nameResults, theFileName ); } } break ; } return 0 ; }
resultList* attributeID( query *oneQuery, int numClauses ) { int isFileQuery( char * ) ; struct stat statBuffer ; // a required arguement in unix stat() function resultList *resultID = NULL ; if( isFileQuery( oneQuery -> myClauses[numClauses].value ) ) { char tempPath[BUFFER_SIZE] = {'\0'} ; char resultPath[BUFFER_SIZE] = {'\0'} ; int numslash = 0 ; numslash = FSqueryValidator( oneQuery -> myClauses[numClauses].value ) ; strcpy( searchFile, ( oneQuery -> myClauses[numClauses].value ) + 5 ) ; switch( numslash ) { case 2: strcpy( tempPath, directoryPath ) ; strcat( tempPath, searchFile + 1 ) ; if( strlen( tempPath ) > ( strlen( directoryPath ) + 1 ) ) if( stat( tempPath, &statBuffer ) == 0 ) { sprintf( resultPath, "file://%s", tempPath ) ; resultID = addResultItem( resultID, resultPath ) ; } break ; case 3: if( strlen( searchFile ) > 3 ) if( stat( searchFile + 2, &statBuffer ) == 0 ) resultID = addResultItem( resultID, oneQuery -> myClauses[numClauses].value ) ; break ; default: oneQuery -> removeTag = 1 ; } } return resultID ; }
void VListUE::listHistory(const QString &p_cmd) { m_panel->setTitle(tr("History")); VHistoryList *history = g_mainWin->getHistoryList(); const QLinkedList<VHistoryEntry> &entries = history->getHistoryEntries(); if (p_cmd.isEmpty()) { // List the content. for (auto it = entries.rbegin(); it != entries.rend(); ++it) { QSharedPointer<VSearchResultItem> item(new VSearchResultItem(it->m_isFolder ? VSearchResultItem::Folder : VSearchResultItem::Note, VSearchResultItem::LineNumber, VUtils::fileNameFromPath(it->m_file), it->m_file)); addResultItem(item); } } else { // Search the content. VSearchConfig config(VSearchConfig::CurrentFolder, VSearchConfig::Name, VSearchConfig::Note | VSearchConfig::Folder, VSearchConfig::Internal, VSearchConfig::NoneOption, p_cmd, QString()); for (auto it = entries.rbegin(); it != entries.rend(); ++it) { QString name = VUtils::fileNameFromPath(it->m_file); if (!config.m_token.matched(name)) { continue; } QSharedPointer<VSearchResultItem> item(new VSearchResultItem(it->m_isFolder ? VSearchResultItem::Folder : VSearchResultItem::Note, VSearchResultItem::LineNumber, name, it->m_file)); addResultItem(item); } } }
void getConfiguartionData( char *cwd, char *searchDir ) { void setConfiguartionData( char *, char *, char *, char * ) ; FILE *oldConfigFile, *newConfigFile ; resultList *theLines ; char oneLine[BUFFER_SIZE] = { '\0' } ; char newLine[BUFFER_SIZE] = { '\0' } ; theLines = NULL ; oldConfigFile = fopen ( "vr.rc", "r" ) ; _assert( __FILE__, __LINE__, oldConfigFile ) ; while ( !feof( oldConfigFile ) ) { fgets ( oneLine, sizeof ( oneLine ), oldConfigFile ) ; if( strlen( oneLine ) ) { setConfiguartionData( oneLine, newLine, cwd, searchDir ) ; theLines = addResultItem( theLines, newLine ) ; oneLine[0] = '\0' ; } } fclose ( oldConfigFile ) ; newConfigFile = fopen ( "vr.rc", "w" ) ; _assert( __FILE__, __LINE__, oldConfigFile ) ; while ( theLines != NULL ) { fwrite( theLines -> oneResult, sizeof( char ), strlen( theLines -> oneResult ), newConfigFile ) ; theLines = ( resultList* ) theLines -> link ; } fclose ( newConfigFile ) ; }
queryList* FSqueryTool( queryList *listpointer ) { void getDirectoryPath( ) ; void attributeDATE( ) ; void attributeNAME( ) ; resultList* attributeID( query *, int ) ; char oneLine[BUFFER_SIZE] ; // one line from the an opened file char *word ; // a token during string tokenization int numClauses ; queryList *tempQueries ; // a pointer to the query list arguement resultList *tempResults ; getDirectoryPath( ) ; tempQueries = listpointer ; tempResults = NULL ; while( tempQueries != NULL ) { for( numClauses = 0 ; numClauses <= tempQueries -> oneQuery -> numClauses ; numClauses++ ) { if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses].attribute, "ID" ) == 0 ) { if( numClauses == 0 ) tempResults = attributeID( tempQueries -> oneQuery, numClauses ) ; else { if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "AND" ) == 0 ) tempResults = andResult( tempResults, attributeID( tempQueries -> oneQuery, numClauses ) ) ; if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "OR" ) == 0 ) tempResults = orResult(tempResults, attributeID( tempQueries -> oneQuery, numClauses ) ) ; } } if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses].attribute, "DATE" ) == 0 ) { char fileTime[22] = { '\0' } ; dateResults = NULL; formatTimeStamp( fileTime, tempQueries -> oneQuery -> myClauses[numClauses].value ) ; fileQueryTime = parsedate( fileTime, NULL) ; strcpy( operatorType, tempQueries -> oneQuery -> myClauses[numClauses].operator ) ; attributeDATE( ) ; if( numClauses == 0 ) tempResults = dateResults ; else { if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "AND" ) == 0 ) tempResults = andResult( tempResults, dateResults ) ; if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "OR" ) == 0 ) tempResults = orResult( tempResults, dateResults ) ; } } if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses].attribute, "NAME" ) == 0 ) { nameResults = NULL; strcpy( operatorType, tempQueries -> oneQuery -> myClauses[numClauses].operator ) ; strcpy( queryName, tempQueries -> oneQuery -> myClauses[numClauses].value ) ; attributeNAME( ) ; if( numClauses == 0 ) tempResults = nameResults ; else { if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "AND" ) == 0 ) tempResults = andResult( tempResults, nameResults ) ; if( strcmp( tempQueries -> oneQuery -> myClauses[numClauses-1].conjecture, "OR" ) == 0 ) tempResults = orResult( tempResults, nameResults ) ; } } } while ( tempResults != NULL ) { tempQueries -> oneQuery -> results = addResultItem( tempQueries -> oneQuery -> results, tempResults -> oneResult ) ; tempQueries -> oneQuery -> numFound++ ; tempResults = ( resultList* ) tempResults -> link ; } tempQueries = ( queryList* ) tempQueries -> link ; } return listpointer ; }
int getDate( const char *filename, const struct stat *statptr, int flag ) { void getFileTimeStamp( char * , char * ) ; char theFileName[BUFFER_SIZE] = { '\0' } ; char fileDate[20] = { '\0' } ; time_t fileDateTime ; switch ( flag ) { case FTW_F : getFileTimeStamp( fileDate, ctime( &statptr -> st_mtime ) ) ; fileDateTime = parsedate( fileDate, NULL ) ; strcat( theFileName, "file://" ) ; if( strcmp( operatorType, "EQ" ) == 0 ) { if( difftime( fileDateTime, fileQueryTime ) == 0 ) { strcat( theFileName, filename ) ; dateResults = addResultItem( dateResults, theFileName ) ; } } else if( strcmp( operatorType, "LT" ) == 0 ) { if( difftime( fileDateTime, fileQueryTime ) < 0 ) { strcat( theFileName, filename ) ; dateResults = addResultItem( dateResults, theFileName ) ; } } else if( strcmp( operatorType, "LE" ) == 0 ) { if( ( difftime( fileDateTime, fileQueryTime ) == 0 ) || ( difftime( fileDateTime, fileQueryTime ) < 0 ) ) { strcat( theFileName, filename ) ; dateResults = addResultItem( dateResults, theFileName ) ; } } else if( strcmp( operatorType, "GT" ) == 0 ) { if( difftime( fileDateTime, fileQueryTime ) > 0 ) { strcat( theFileName, filename ) ; dateResults = addResultItem( dateResults, theFileName ) ; } } else if( strcmp( operatorType, "GE" ) == 0 ) { if( ( difftime( fileDateTime, fileQueryTime ) == 0 ) || ( difftime( fileDateTime, fileQueryTime ) > 0 ) ) { strcat( theFileName, filename ) ; dateResults = addResultItem( dateResults, theFileName ) ; } } break ; } return 0 ; }
bool VListFolderUE::listFolder(const QString &p_path, const QString &p_cmd) { VDirectory *dir = g_vnote->getInternalDirectory(p_path); if (!dir) { // See if it is a notebook. VNotebook *nb = g_vnote->getNotebook(p_path); if (!nb) { return false; } dir = nb->getRootDir(); } m_panel->clearTitle(); m_listWidget->clearAll(); m_data.clear(); m_currentFolderPath = dir->fetchPath(); m_panel->setTitle(m_currentFolderPath); if (!dir->open()) { return true; } if (p_cmd.isEmpty()) { // List the content. for (auto const & it : dir->getSubDirs()) { QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Folder, VSearchResultItem::LineNumber, it->getName(), it->fetchPath())); addResultItem(item); } for (auto const & file : dir->getFiles()) { QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Note, VSearchResultItem::LineNumber, file->getName(), file->fetchPath())); addResultItem(item); } } else { // Search the content. VSearchConfig config(VSearchConfig::CurrentFolder, VSearchConfig::Name, VSearchConfig::Note | VSearchConfig::Folder, VSearchConfig::Internal, VSearchConfig::NoneOption, p_cmd, QString()); for (auto const & it : dir->getSubDirs()) { QString name = it->getName(); if (!config.m_token.matched(name)) { continue; } QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Folder, VSearchResultItem::LineNumber, name, it->fetchPath())); addResultItem(item); } for (auto const & file : dir->getFiles()) { QString name = file->getName(); if (!config.m_token.matched(name)) { continue; } QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Note, VSearchResultItem::LineNumber, name, file->fetchPath())); addResultItem(item); } } return true; }
vector<vector<int> > combinationSum(vector<int> &candidates, int target) { vector<vector<int> > result; int n = candidates.size(); if(!n) return result; sort(candidates.begin(), candidates.end()); vector<int> selection(n, 0); vector<int> copies(n, 0); int value = target; int index = 0; selection[index] = 0; copies[index] = 1; value -= candidates[selection[index]] * copies[index]; while(true) { if(selection[0] >= n) break; if(value == 0){ addResultItem(candidates, selection, copies, index, result); value += candidates[selection[index]] * copies[index]; if(selection[index] == n-1) { if(!index) break; // --index; ++copies[index]; value -= candidates[selection[index]]; } else { ++selection[index]; copies[index] = 1; value -= candidates[selection[index]]; } } else if(value > 0) { if(selection[index] == n-1) { ++copies[index]; value -= candidates[selection[index]]; } else { ++index; selection[index] = selection[index-1] +1 ; copies[index] = 1; value -= candidates[selection[index]]; } } else { value += candidates[selection[index]] * copies[index]; if(selection[index] >= n-1) if(!index) break; else { --index; ++copies[index]; value -= candidates[selection[index]]; } else if(copies[index] == 1) { // go back if(!index) break; else { --index; ++copies[index]; value -= candidates[selection[index]]; } } else { ++selection[index]; copies[index] = 1; value -= candidates[selection[index]]; } } } return result; }