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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// File: directory.h
// Created by: adrian <adrian@lisas.de>
// Created on: Thu Jan 30 17:28:51 2003
//

#include "directory.h"
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <cstring>

directory::directory(string current_dir)
{
    struct stat stat_struct;
    struct dirent *dirent_struct;
    DIR *dir = NULL;
    string full_path;
    int start = 0;
    int stop = 0;
    
    current_dir = normalize_path(current_dir);
  
    if ((current_dir.find("/dev/")==0) || (current_dir.find("/proc/")==0))
        return;
    
    stop = current_dir.rfind("/") - 1;
    start = current_dir.rfind("/",stop);
    short_name = current_dir.substr(start+1,stop-start);
    
    this->current_dir = current_dir;
    /* does the directory exist */
    if (stat(current_dir.c_str(), &stat_struct) == -1)
        return;
    dir = opendir(current_dir.c_str());
    if (!dir)
        return;
    dirent_struct = readdir(dir);
    while (dirent_struct) {
        full_path = current_dir;
        full_path += dirent_struct->d_name;
        if (is_dir(full_path) && !(!strcmp(dirent_struct->d_name, ".")))
            directories.push_back(dirent_struct->d_name);
		  if (!is_dir(full_path))
			   files.push_back(dirent_struct->d_name);
        dirent_struct = readdir(dir);
    }
    
    directories.sort();
}

bool directory::is_dir(string dir)
{
    struct stat stat_struct;

    stat(dir.c_str(), &stat_struct);
    if (stat_struct.st_mode & S_IFDIR)
        return true;
    return false;
}

directory::~directory()
{
    printf("deleted: %s\n",current_dir.c_str());
}

bool directory::has_childs()
{
    if (childs.size())
        return true;
    return false;
}

int directory::how_many()
{
    return (int) directories.size();
}

int directory::how_many_files()
{
    return (int) files.size();
}

int directory::how_many_childs()
{
    return (int) childs.size();
}

string directory::get_directory(int id) {
    if (id < 0 || id >= how_many())
        return string();
    list <string> :: iterator dir;
    int i = 0;
    for (dir = directories.begin(); dir != directories.end(); dir++) {
        if (i++ == id)
            return *dir;
    }
    return string();
}

string directory::get_file(int id) {
    if (id < 0 || id >= how_many_files())
        return string();
    list <string> :: iterator file;
    int i = 0;
    for (file = files.begin(); file != files.end(); file++) {
        if (i++ == id)
            return *file;
    }
    return string();
}

directory* directory::get_childs(int id) {
    if (id < 0 || id >= how_many_childs())
        return NULL;
    list <directory *> :: iterator dir;
    int i = 0;
    for (dir = childs.begin(); dir != childs.end(); dir++) {
        if (i++ == id)
            return *dir;
    }
    return NULL;
}

string directory::get_current()
{
    return current_dir;
}

string directory::get_short_name() {
    return short_name;
}

void directory::make_childs()
{
    childs.clear();
    list <string> :: iterator dir;
    for (dir = directories.begin(); dir != directories.end(); dir++) {
        childs.push_back(new directory(current_dir + *dir));
    }
}

string directory::normalize_path(string path) {
    if (path.substr(path.size()-1,path.size()).compare("/"))
        path += "/";
    return path;
}


directory *directory::get_child_by_path(string path) {
    path = normalize_path(path);
    list <directory *> :: iterator dir;
    for (dir = childs.begin(); dir != childs.end(); dir++) {
        printf("%s:%s:%d\n",(*dir)->get_current().c_str(),
                path.c_str(),path.size());
        if (!(*dir)->get_current().compare(0,path.size(),path))
            return (*dir);
    }
    return this;
}