OSDN Git Service

Reduce compilation time (2)
[winmerge-jp/winmerge-jp.git] / Src / DirItem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //    License (GPLv2+):
3 //    This program is free software; you can redistribute it and/or modify
4 //    it under the terms of the GNU General Public License as published by
5 //    the Free Software Foundation; either version 2 of the License, or (at
6 //    your option) any later version.
7 //    
8 //    This program is distributed in the hope that it will be useful, but
9 //    WITHOUT ANY WARRANTY; without even the implied warranty of
10 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //    GNU General Public License for more details.
12 //
13 //    You should have received a copy of the GNU General Public License
14 //    along with this program; if not, write to the Free Software
15 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 /////////////////////////////////////////////////////////////////////////////
17 /** 
18  * @file  DirItem.cpp
19  *
20  * @brief Implementation for DirItem routines
21  */
22
23 #include "pch.h"
24 #include "DirItem.h"
25 #include <windows.h>
26 #include "UnicodeString.h"
27 #include "paths.h"
28 #include "TFile.h"
29 #include "DebugNew.h"
30
31 /**
32         * @brief Convert file flags to string presentation.
33         * This function converts file flags to a string presentation that can be
34         * shown in the GUI.
35         * @return File flags as a string.
36         */
37 String FileFlags::ToString() const
38 {
39         String sflags;
40         if (attributes & FILE_ATTRIBUTE_READONLY)
41                 sflags += _T("R");
42         if (attributes & FILE_ATTRIBUTE_HIDDEN)
43                 sflags += _T("H");
44         if (attributes & FILE_ATTRIBUTE_SYSTEM)
45                 sflags += _T("S");
46         if (attributes & FILE_ATTRIBUTE_ARCHIVE)
47                 sflags += _T("A");
48         return sflags;
49 }
50
51 /**
52  * @brief Set filename and path for the item.
53  * @param [in] fullpath Full path to file to set to item.
54  */
55 void DirItem::SetFile(const String &fullPath)
56 {
57         String ext, filename2, path2;
58         paths::SplitFilename(fullPath, &path2, &filename2, &ext);
59         filename2 += _T(".");
60         filename2 += ext;
61         filename = filename2;
62         path = path2;
63 }
64
65 /**
66  * @brief Get the full path of the item.
67  * @return fullpath
68  */
69 String DirItem::GetFile() const
70 {
71         return paths::ConcatPath(path.get(), filename.get());
72 }
73
74 /**
75  * @brief Update fileinfo from given file.
76  * This function updates file's information from given item. Function
77  * does not set filename and path.
78  * @param [in] sFilePath Full path to file/directory to update
79  * @return true if information was updated (item was found).
80  */
81 bool DirItem::Update(const String &sFilePath)
82 {
83         bool retVal = false;
84
85         size = DirItem::FILE_SIZE_NONE;
86         flags.reset();
87         mtime = 0;
88
89         if (!sFilePath.empty())
90         {
91                 try
92                 {
93                         TFile file(sFilePath);
94
95                         mtime = file.getLastModified();
96                         // There can be files without modification date.
97                         // Then we must use creation date. Of course we assume
98                         // creation date then exists...
99                         if (mtime == 0)
100                                 mtime = file.created();
101
102                         // No size for directory ( size remains as -1)
103                         if (!file.isDirectory())
104                                 size = file.getSize();
105
106                         flags.attributes = GetFileAttributes(TFile(sFilePath).wpath().c_str());
107
108                         retVal = true;
109                 }
110                 catch (...)
111                 {
112                 }
113         }
114         return retVal;
115 }
116
117 /**
118  * @brief Clears FileInfo data.
119  */
120 /*void DirItem::Clear()
121 {
122         ClearPartial();
123         filename.erase();
124         path.erase();
125 }*/
126
127 /**
128  * @brief Clears FileInfo data except path/filename.
129  */
130 void DirItem::ClearPartial()
131 {
132         ctime = 0;
133         mtime = 0;
134         size = DirItem::FILE_SIZE_NONE;
135         version.Clear();
136         flags.reset();
137 }