~adiroiban/ubuntu/karmic/kover/i18n-support

1 by Adi Roiban
fix LP: #478507
1
//
2
// File: directory.h
3
// Created by: adrian <adrian@lisas.de>
4
// Created on: Thu Jan 30 17:28:51 2003
5
//
6
7
#include "directory.h"
8
#include <sys/types.h>
9
#include <dirent.h>
10
#include <sys/stat.h>
11
#include <cstring>
12
13
directory::directory(string current_dir)
14
{
15
    struct stat stat_struct;
16
    struct dirent *dirent_struct;
17
    DIR *dir = NULL;
18
    string full_path;
19
    int start = 0;
20
    int stop = 0;
21
    
22
    current_dir = normalize_path(current_dir);
23
  
24
    if ((current_dir.find("/dev/")==0) || (current_dir.find("/proc/")==0))
25
        return;
26
    
27
    stop = current_dir.rfind("/") - 1;
28
    start = current_dir.rfind("/",stop);
29
    short_name = current_dir.substr(start+1,stop-start);
30
    
31
    this->current_dir = current_dir;
32
    /* does the directory exist */
33
    if (stat(current_dir.c_str(), &stat_struct) == -1)
34
        return;
35
    dir = opendir(current_dir.c_str());
36
    if (!dir)
37
        return;
38
    dirent_struct = readdir(dir);
39
    while (dirent_struct) {
40
        full_path = current_dir;
41
        full_path += dirent_struct->d_name;
42
        if (is_dir(full_path) && !(!strcmp(dirent_struct->d_name, ".")))
43
            directories.push_back(dirent_struct->d_name);
44
		  if (!is_dir(full_path))
45
			   files.push_back(dirent_struct->d_name);
46
        dirent_struct = readdir(dir);
47
    }
48
    
49
    directories.sort();
50
}
51
52
bool directory::is_dir(string dir)
53
{
54
    struct stat stat_struct;
55
56
    stat(dir.c_str(), &stat_struct);
57
    if (stat_struct.st_mode & S_IFDIR)
58
        return true;
59
    return false;
60
}
61
62
directory::~directory()
63
{
64
    printf("deleted: %s\n",current_dir.c_str());
65
}
66
67
bool directory::has_childs()
68
{
69
    if (childs.size())
70
        return true;
71
    return false;
72
}
73
74
int directory::how_many()
75
{
76
    return (int) directories.size();
77
}
78
79
int directory::how_many_files()
80
{
81
    return (int) files.size();
82
}
83
84
int directory::how_many_childs()
85
{
86
    return (int) childs.size();
87
}
88
89
string directory::get_directory(int id) {
90
    if (id < 0 || id >= how_many())
91
        return string();
92
    list <string> :: iterator dir;
93
    int i = 0;
94
    for (dir = directories.begin(); dir != directories.end(); dir++) {
95
        if (i++ == id)
96
            return *dir;
97
    }
98
    return string();
99
}
100
101
string directory::get_file(int id) {
102
    if (id < 0 || id >= how_many_files())
103
        return string();
104
    list <string> :: iterator file;
105
    int i = 0;
106
    for (file = files.begin(); file != files.end(); file++) {
107
        if (i++ == id)
108
            return *file;
109
    }
110
    return string();
111
}
112
113
directory* directory::get_childs(int id) {
114
    if (id < 0 || id >= how_many_childs())
115
        return NULL;
116
    list <directory *> :: iterator dir;
117
    int i = 0;
118
    for (dir = childs.begin(); dir != childs.end(); dir++) {
119
        if (i++ == id)
120
            return *dir;
121
    }
122
    return NULL;
123
}
124
125
string directory::get_current()
126
{
127
    return current_dir;
128
}
129
130
string directory::get_short_name() {
131
    return short_name;
132
}
133
134
void directory::make_childs()
135
{
136
    childs.clear();
137
    list <string> :: iterator dir;
138
    for (dir = directories.begin(); dir != directories.end(); dir++) {
139
        childs.push_back(new directory(current_dir + *dir));
140
    }
141
}
142
143
string directory::normalize_path(string path) {
144
    if (path.substr(path.size()-1,path.size()).compare("/"))
145
        path += "/";
146
    return path;
147
}
148
149
150
directory *directory::get_child_by_path(string path) {
151
    path = normalize_path(path);
152
    list <directory *> :: iterator dir;
153
    for (dir = childs.begin(); dir != childs.end(); dir++) {
154
        printf("%s:%s:%d\n",(*dir)->get_current().c_str(),
155
                path.c_str(),path.size());
156
        if (!(*dir)->get_current().compare(0,path.size(),path))
157
            return (*dir);
158
    }
159
    return this;
160
}