/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: ... co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "shell_completion.h" #include "shell_input.h" #include "substring.h" #include "base_object.h" #include "class_list.h" #include "list.h" #include "debug.h" #include "stdlibincl.h" using namespace std; /** * standard constructor * @todo this constructor is not jet implemented - do it */ ShellCompletion::ShellCompletion(ShellInput* input) { this->completionList = NULL; this->input = input; } /** * standard deconstructor */ ShellCompletion::~ShellCompletion () { // delete what has to be deleted here if (this->completionList) { this->emptyCompletionList(); delete this->completionList; } } /** * autocompletes the Shell's inputLine * @returns true, if a result was found, false otherwise * * @todo implement it!! */ bool ShellCompletion::autoComplete(ShellInput* input) { const char* completionLine; long classID; //< the classID retrieved from the Class. tList* objectList; //< the list of Objects stored in classID bool emptyComplete = false; //< if the completion input is empty string. e.g "" PRINTF(5)("AutoComplete on input\n"); this->emptyCompletionList(); if (input != NULL) this->input = input; if (this->input == NULL) { PRINTF(2)("No ShellInput supplied\n"); return false; } // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object if (this->input->getInput() != NULL && strrchr(this->input->getInput(), ' ') >= this->input->getInput() + strlen(this->input->getInput())-1) { emptyComplete = true; } if (this->input->getInput() == NULL) completionLine = ""; else completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n"); SubString inputSplits(completionLine, true); // CLASS COMPLETION if (inputSplits.getCount() == 0) { PRINTF(5)("Listing all Classes\n"); this->objectComplete("", CL_SHELL_COMMAND_CLASS); return false; } else if (inputSplits.getCount() == 1 && emptyComplete == false) { printf("trying to complete a Class with '%s'\n", inputSplits.getString(0)); this->objectComplete(inputSplits.getString(0), CL_SHELL_COMMAND_CLASS); } // OBJECT/FUNCTION COMPLETIONS else if ( (inputSplits.getCount() == 1 && emptyComplete == true ) || (inputSplits.getCount() == 2 && emptyComplete == false)) { classID = ClassList::StringToID(inputSplits.getString(0)); if (classID == CL_NULL) return false; if (inputSplits.getCount()==2) { if (completionLine[strlen(completionLine)-1] != ' ') this->objectComplete(inputSplits.getString(1), classID); } else this->objectComplete("", classID); } // else if (inputSplits.getCount() <= 3 ) /* completionLine = new char[strlen(this->input->getText())+1]; strcpy(completionLine, this->input->getText()); char* commandBegin = strrchr(completionLine, ' '); if (commandBegin == NULL) commandBegin = completionLine; else { if(commandBegin >= completionLine + strlen(completionLine)) commandBegin = completionLine + strlen(completionLine); else commandBegin++; } char* objectStart; if (objectStart = strstr(commandBegin, "::")) { char* classIdentity = new char[objectStart - commandBegin +1]; strncpy(classIdentity, commandBegin, objectStart - commandBegin); classIdentity[objectStart - commandBegin] = '\0'; this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity)); delete[] classIdentity; } else this->classComplete(commandBegin); delete[] completionLine;*/ } /** * autocompletes a className * @param classBegin the Beginning of a String to autoComplete * @return true on success, false otherwise */ bool ShellCompletion::classComplete(const char* classBegin) { if (unlikely(classBegin == NULL)) return false; const tList* clList = ClassList::getClassList(); if (clList != NULL) { const tList* classList = this->addToCompleteList(clList, classBegin); if (classList != NULL) this->generalComplete(classList, classBegin, "CL: %s "); else return false; delete classList; } else return false; return true; } /** * autocompletes an ObjectName * @param objectBegin the beginning string of a Object * @param classID the ID of the Class to search for. * @return true on success, false otherwise */ bool ShellCompletion::objectComplete(const char* objectBegin, long classID) { if (unlikely(objectBegin == NULL)) return false; tList* boList = ClassList::getList(classID); if (boList != NULL) { //printf("%s\n", boList->firstElement()->getName()); const tList* objectList = this->addToCompleteList(boList, objectBegin); if (objectList != NULL) this->generalComplete(objectList, objectBegin, "%s "); else return false; } else return false; return true; } /** * completes a Function * @param functionBegin the beginning of the function String */ bool ShellCompletion::functionComplete(const char* functionBegin) { } /** * completes the inputline on grounds of an inputList * @param stringList the List to parse through * @param begin the String to search in the inputList, and to extend with it. * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" * @param addBack what should be added at the end of the completion * @param addFront what should be added to the front of one finished completion * @return true if ok, false otherwise */ bool ShellCompletion::generalComplete(const tList* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront) { if (stringList == NULL || this->input == NULL ) return false; if (stringList->getSize() == 0) return false; ShellC_Element* addElem = stringList->firstElement(); const char* addString = addElem->name; unsigned int addLength = 0; unsigned int inputLenght = strlen(begin); // Determin the longest Match if (addString != NULL) addLength = strlen(addString); tIterator* charIterator = stringList->getIterator(); ShellC_Element* charElem = charIterator->firstElement(); while (charElem != NULL) { PRINTF(0)(displayAs, charElem->name); for (unsigned int i = inputLenght; i < addLength; i++) if (addString[i] != charElem->name[i]) { addLength = i; break; } charElem = charIterator->nextElement(); } delete charIterator; if (addLength >= inputLenght) { char* adder = new char[addLength+1]; strncpy(adder, addString, addLength); adder[addLength] = '\0'; if (this->input) { this->input->removeCharacters(inputLenght); this->input->addCharacters(adder); if (stringList->getSize() == 1) { if ( addBack != NULL ) this->input->addCharacters(addBack); this->input->addCharacter(' '); } delete[] adder; } } return true; } /** * searches for classes, which beginn with completionBegin * @param inputList the List to parse through * @param completionBegin the beginning string * !! The strings MUST NOT be deleted !! */ const tList* ShellCompletion::addToCompleteList(const tList* inputList, const char* completionBegin) { if (inputList == NULL || completionBegin == NULL) return NULL; unsigned int searchLength = strlen(completionBegin); tIterator* iterator = inputList->getIterator(); const char* enumString = iterator->firstElement(); while (enumString != NULL) { if (strlen(enumString) >= searchLength && !strncasecmp(enumString, completionBegin, searchLength)) { printf("%s\n", enumString); ShellC_Element* newElem = new ShellC_Element; newElem->name = enumString; this->completionList->add(newElem); } enumString = iterator->nextElement(); } delete iterator; return this->completionList; } /** * searches for classes, which beginn with completionBegin * @param inputList the List to parse through * @param completionBegin the beginning string * !! The strings MUST NOT be deleted !! */ const tList* ShellCompletion::addToCompleteList(const tList* inputList, const char* completionBegin) { if (inputList == NULL || completionBegin == NULL) return NULL; unsigned int searchLength = strlen(completionBegin); tIterator* iterator = inputList->getIterator(); BaseObject* enumBO = iterator->firstElement(); while (enumBO != NULL) { if (enumBO->getName() != NULL && strlen(enumBO->getName()) >= searchLength && !strncasecmp(enumBO->getName(), completionBegin, searchLength)) { ShellC_Element* newElem = new ShellC_Element; newElem->name = enumBO->getName(); this->completionList->add(newElem); printf("%s\n",enumBO->getName()); } enumBO = iterator->nextElement(); } delete iterator; return this->completionList; } void ShellCompletion::emptyCompletionList() { if (this->completionList != NULL) { tIterator* elemIT = this->completionList->getIterator(); ShellC_Element* elem = elemIT->firstElement(); while (elem != NULL) { delete elem; elem = elemIT->nextElement(); } delete this->completionList; } this->completionList = new tList; }