/* 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) { 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. char* classBegin; //< the beginn of the slass string char* classEnd; //< the end of the class string char* objectBegin; //< the begin of the object string char* objectEnd; //< the end of the object string char* functionBegin; //< the begin of the function string char* functionEnd; //< the end of the function string PRINTF(4)("AutoComplete on input\n"); if (input != NULL) this->input = input; if (this->input == NULL) { PRINTF(2)("No ShellInput supplied\n"); return false; } if (this->input->getInput() == NULL) return this->classComplete(""); 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->classComplete(""); return false; } else if (inputSplits.getCount() == 1 && strlen(inputSplits.getString(0)) == strlen(completionLine)) { printf("trying to complete a Class with %d\n", inputSplits.getString(0)); this->classComplete(inputSplits.getString(0)); } // OBJECT COMPLETIONS else if ( inputSplits.getCount() <= 2 && strlen(inputSplits.getString(0)) < strlen(completionLine)) { classID = ClassList::StringToID(inputSplits.getString(0)); if (classID == CL_NULL) return false; if (inputSplits.getCount()==2) this->objectComplete(inputSplits.getString(1), classID); else this->objectComplete("", classID); } /* 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->createCompleteList(clList, classBegin); if (classList != NULL) this->generalComplete(classList, classBegin, "CL: %s "); else return false; } 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) { printf("%s\n", objectBegin); if (unlikely(objectBegin == NULL)) return false; tList* boList = ClassList::getList(classID); if (boList != NULL) { printf("\n", boList->firstElement()->getName()); const tList* objectList = this->createCompleteList(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; const char* addString = stringList->firstElement(); unsigned int addLength = 0; unsigned int inputLenght = strlen(begin); if (addString != NULL) addLength = strlen(addString); tIterator* charIterator = stringList->getIterator(); const char* charElem = charIterator->firstElement(); while (charElem != NULL) { PRINTF(0)(displayAs, charElem); for (unsigned int i = inputLenght; i < addLength; i++) if (addString[i] != charElem[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 classNameBegin * @param inputList the List to parse through * @param classNameBegin the beginning string * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, * !! The strings MUST NOT be deleted !! */ const tList* ShellCompletion::createCompleteList(const tList* inputList, const char* classNameBegin) { if (inputList == NULL || classNameBegin == NULL) return NULL; unsigned int searchLength = strlen(classNameBegin); if (this->completionList != NULL) delete this->completionList; this->completionList = new tList; // tList* classList = ClassList::getClassList(); tIterator* iterator = inputList->getIterator(); const char* enumString = iterator->firstElement(); while (enumString != NULL) { if (strlen(enumString) >= searchLength && !strncasecmp(enumString, classNameBegin, searchLength)) { printf("%s\n", enumString); this->completionList->add(enumString); } enumString = iterator->nextElement(); } delete iterator; return this->completionList; } /** * searches for classes, which beginn with classNameBegin * @param inputList the List to parse through * @param classNameBegin the beginning string * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, * !! The strings MUST NOT be deleted !! */ const tList* ShellCompletion::createCompleteList(const tList* inputList, const char* classNameBegin) { if (inputList == NULL || classNameBegin == NULL) return NULL; unsigned int searchLength = strlen(classNameBegin); if (this->completionList != NULL) delete this->completionList; this->completionList = new tList; tIterator* iterator = inputList->getIterator(); BaseObject* enumBO = iterator->firstElement(); while (enumBO != NULL) { if (enumBO->getName() != NULL && strlen(enumBO->getName()) >= searchLength && !strncasecmp(enumBO->getName(), classNameBegin, searchLength)) { this->completionList->add(enumBO->getName()); } enumBO = iterator->nextElement(); } delete iterator; return this->completionList; }