Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "tier0/platform.h"
  8. #include "qlimits.h"
  9. #include "sys.h"
  10. #include "baseautocompletefilelist.h"
  11. #include "utlsymbol.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Fills in a list of commands based on specified subdirectory and extension into the format:
  16. // commandname subdir/filename.ext
  17. // commandname subdir/filename2.ext
  18. // Returns number of files in list for autocompletion
  19. //-----------------------------------------------------------------------------
  20. int CBaseAutoCompleteFileList::AutoCompletionFunc( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
  21. {
  22. char const *cmdname = m_pszCommandName;
  23. char *substring = (char *)partial;
  24. if ( Q_strstr( partial, cmdname ) )
  25. {
  26. substring = (char *)partial + strlen( cmdname ) + 1;
  27. }
  28. // Search the directory structure.
  29. char searchpath[MAX_QPATH];
  30. if ( m_pszSubDir && m_pszSubDir[0] && Q_strcasecmp( m_pszSubDir, "NULL" ) )
  31. {
  32. Q_snprintf(searchpath,sizeof(searchpath),"%s/*.%s", m_pszSubDir, m_pszExtension );
  33. }
  34. else
  35. {
  36. Q_snprintf(searchpath,sizeof(searchpath),"*.%s", m_pszExtension );
  37. }
  38. CUtlSymbolTable entries( 0, 0, true );
  39. CUtlVector< CUtlSymbol > symbols;
  40. char const *findfn = Sys_FindFirst( searchpath, NULL, 0 );
  41. while ( findfn )
  42. {
  43. char sz[ MAX_QPATH ];
  44. Q_snprintf( sz, sizeof( sz ), "%s", findfn );
  45. bool add = false;
  46. // Insert into lookup
  47. if ( substring[0] )
  48. {
  49. if ( !Q_strncasecmp( findfn, substring, strlen( substring ) ) )
  50. {
  51. add = true;
  52. }
  53. }
  54. else
  55. {
  56. add = true;
  57. }
  58. if ( add )
  59. {
  60. CUtlSymbol sym = entries.AddString( findfn );
  61. int idx = symbols.Find( sym );
  62. if ( idx == symbols.InvalidIndex() )
  63. {
  64. symbols.AddToTail( sym );
  65. }
  66. }
  67. findfn = Sys_FindNext( NULL, 0 );
  68. // Too many
  69. if ( symbols.Count() >= COMMAND_COMPLETION_MAXITEMS )
  70. break;
  71. }
  72. Sys_FindClose();
  73. for ( int i = 0; i < symbols.Count(); i++ )
  74. {
  75. char const *filename = entries.String( symbols[ i ] );
  76. Q_snprintf( commands[ i ], sizeof( commands[ i ] ), "%s %s", cmdname, filename );
  77. // Remove .dem
  78. commands[ i ][ strlen( commands[ i ] ) - 4 ] = 0;
  79. }
  80. return symbols.Count();
  81. }