path.h
上传用户:dfhlxjd
上传日期:2007-01-07
资源大小:12k
文件大小:2k
源码类别:

Shell编程

开发平台:

Visual C++

  1. //----------------------------------------
  2. // (c) Reliable Software 1997
  3. //----------------------------------------
  4. #ifndef _path_H_INCLUDED
  5. #define _path_H_INCLUDED
  6. #include "main.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. class Path
  10. {
  11. public:
  12. Path(char const * name)
  13.         :_pathLen (strlen (name))
  14. {
  15.         if (_pathLen >= MAX_PATH)
  16.             throw WinException ("Path too long");
  17. strcpy(_name,name);
  18. if (_name[_pathLen - 1] == '\')
  19.         {
  20.             _pathLen--;
  21.         }
  22.         SetCurrentDirectory(_name);
  23. }
  24. char const * WildcardPath()
  25. {
  26. // create a wildcard path of the name
  27.         if (_pathLen + 4 >= MAX_PATH)
  28.             throw WinException ("Path too long");
  29. strcpy(_name + _pathLen, "\*.*");
  30.         return _name;
  31. }
  32. void DirDown(char const * name)
  33. {
  34.         int len = strlen (name);
  35.         if (_pathLen + len + 1 >= MAX_PATH)
  36.             throw WinException ("Path too long");
  37. strcpy (_name + _pathLen, "\");
  38. strcat(_name, name);
  39.         _pathLen += len + 1;
  40.         SetCurrentDirectory(_name);
  41. }
  42. void DirUp()
  43. {
  44.         _name [_pathLen] = '';
  45. // remove the last part of this string
  46. char * lastSep = strrchr(_name, '\');
  47. if (lastSep)
  48. {
  49.     *lastSep = '';
  50.             _pathLen = lastSep - _name;
  51.             if ((lastSep - _name) == 2)
  52.             {
  53.                 // preserve the directory separator when we have reached the root
  54.                 _name[2] = '\';
  55.                 _name[3] = '';
  56.             }
  57. }
  58.         SetCurrentDirectory(_name);
  59. }
  60. char const * GetName() const {return _name;}
  61. protected:
  62.     int     _pathLen;
  63. char    _name[MAX_PATH + 1];
  64. };
  65. #endif // _path_H_INCLUDED