All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DirTool.cpp
Go to the documentation of this file.
1 /*
2  * DirTool.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <HSEP/DirTool.h>
23 
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <string.h>
28 
29 namespace HSEP {
30 
31  ErrorCode DirTool::traverse(StringArray& pResultRef, string& pPathRef, string& pHeadRef) {
32 
34 
35  DIR* vDirPtr;
36  dirent* vDirEntPtr;
37  string vFilePath;
38  size_t vHeadSize = pHeadRef.size();
39  const char* vHeadPtr = pHeadRef.c_str();
40 
41  if ((vDirPtr = opendir (pPathRef.c_str())) != nullptr) {
42  while ((vDirEntPtr = readdir (vDirPtr)) != nullptr) {
43  if (strncmp(vHeadPtr,vDirEntPtr->d_name,vHeadSize)==0) {
44  string vValue = string(vDirEntPtr->d_name);
45  pResultRef.push(vValue);
46  }
47  }
48  closedir(vDirPtr);
49  vResult = EC::OK;
50  }
51  else {
52  switch (errno) {
53  case EACCES:
54  vResult = EC::AccessError;
55  break;
56  case EMFILE:
57  case ENFILE:
58  vResult = EC::ManyOpenFiles;
59  break;
60  case ENOMEM:
61  vResult = EC::NotEnoughMemory;
62  break;
63  default:
64  vResult = EC::NotCatalogedError;
65  break; // to avoid Eclipse editor warning (not really needed)
66  } // switch (errno)
67  } // else {
68 
69  return vResult;
70 
71  } // DirTool::traverse
72 
73  ErrorCode DirTool::traverse(StringArray& pResultRef, string& pPathRef, const char* pHeadPtr) {
74  string vHead = string (pHeadPtr);
75  return traverse(pResultRef,pPathRef,vHead);
76  } // DirTool::traverse
77 
78 } // HSEP namespace