Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/ArgumentCompletionFunctions.cc @ 1441

Last change on this file since 1441 was 1441, checked in by landauf, 17 years ago

you'll love this: separated displayed strings in autocompletion from actually inserted strings, to make fancy things like a really good autocompletion for files and directories.

@bensch: this is the "better system" I was talking about

File size: 6.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (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, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include <iostream>
30#include <map>
31
32#include <dirent.h>
33
34#include "ArgumentCompletionFunctions.h"
35#include "CoreIncludes.h"
36#include "Identifier.h"
37#include "ConfigValueContainer.h"
38#include "TclThreadManager.h"
39#include "util/Convert.h"
40#include "util/String.h"
41#include "util/SubString.h"
42
43namespace orxonox
44{
45    namespace autocompletion
46    {
47        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(fallback)()
48        {
49            return ArgumentCompletionList();
50        }
51
52        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(files)(const std::string& fragment)
53        {
54            ArgumentCompletionList dirlist;
55            ArgumentCompletionList filelist;
56
57            SubString tokens(fragment, "/", "", false, '\0', false, '\0', false, '\0', '\0', false, '\0');
58
59            std::string startdirectory = ".";
60            if (fragment.size() > 0 && fragment[fragment.size() - 1] == '/' && tokens.size() > 0)
61                startdirectory = tokens.subSet(0, tokens.size()).join("/");
62            else if (tokens.size() > 1)
63                startdirectory = tokens.subSet(0, tokens.size() - 1).join("/");
64
65            struct stat fileInfo;
66            struct dirent *currentFile;
67            DIR *handler = 0;
68
69            handler = opendir(startdirectory.c_str());
70            if (handler)
71            {
72                while ((currentFile = readdir(handler)) != 0)
73                {
74                    if (strcmp(currentFile->d_name, ".") && strcmp(currentFile->d_name, ".."))
75                    {
76                        std::string path = startdirectory + "/" + currentFile->d_name;
77                        if (stat(path.c_str(), &fileInfo) == -1)
78                        {
79                            closedir(handler);
80                            break;
81                        }
82
83                        if (S_ISREG(fileInfo.st_mode)) // normal file
84                            filelist.push_back(ArgumentCompletionListElement(path, getLowercase(path), currentFile->d_name));
85                        else if (S_ISDIR(fileInfo.st_mode)) // directory
86                            dirlist.push_back(ArgumentCompletionListElement(path + "/", getLowercase(path) + "/", std::string(currentFile->d_name) + "/"));
87                        else // special file
88                            filelist.push_back(ArgumentCompletionListElement(path, getLowercase(path), currentFile->d_name));
89                    }
90                }
91
92                closedir(handler);
93            }
94
95            filelist.insert(filelist.begin(), dirlist.begin(), dirlist.end());
96            return filelist;
97        }
98
99        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalueclasses)()
100        {
101            ArgumentCompletionList classlist;
102
103            for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getIdentifierMapBegin(); it != Identifier::getIdentifierMapEnd(); ++it)
104                if ((*it).second->hasConfigValues())
105                    classlist.push_back(ArgumentCompletionListElement((*it).second->getName(), getLowercase((*it).first)));
106
107            return classlist;
108        }
109
110        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalues)(const std::string& fragment, const std::string& classname)
111        {
112            ArgumentCompletionList configvalues;
113            std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getIdentifierMap().find(classname);
114
115            if (identifier != Identifier::getIdentifierMapEnd() && (*identifier).second->hasConfigValues())
116            {
117                for (std::map<std::string, ConfigValueContainer*>::const_iterator it = (*identifier).second->getConfigValueMapBegin(); it != (*identifier).second->getConfigValueMapEnd(); ++it)
118                    configvalues.push_back(ArgumentCompletionListElement((*it).second->getName(), getLowercase((*it).first)));
119            }
120
121            return configvalues;
122        }
123
124        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(configvalue)(const std::string& fragment, const std::string& varname, const std::string& classname)
125        {
126            ArgumentCompletionList oldvalue;
127            std::map<std::string, Identifier*>::const_iterator identifier = Identifier::getLowercaseIdentifierMap().find(getLowercase(classname));
128            if (identifier != Identifier::getLowercaseIdentifierMapEnd())
129            {
130                std::map<std::string, ConfigValueContainer*>::const_iterator variable = (*identifier).second->getLowercaseConfigValueMap().find(getLowercase(varname));
131                if (variable != (*identifier).second->getLowercaseConfigValueMapEnd())
132                {
133                    std::string valuestring = (*variable).second->toString();
134                    oldvalue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring));
135                }
136            }
137            return oldvalue;
138        }
139
140        ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(tclthreads)()
141        {
142            std::list<unsigned int> threadnumbers = TclThreadManager::getInstance().getThreadList();
143            ArgumentCompletionList threads;
144
145            for (std::list<unsigned int>::const_iterator it = threadnumbers.begin(); it != threadnumbers.end(); ++it)
146                threads.push_back(ArgumentCompletionListElement(getConvertedValue<unsigned int, std::string>(*it)));
147
148            return threads;
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.