main.cpp

#include <iostream> #include <stdio.h> #include <string.h> #include "DCTTest.h" #include "helper.h" #include "videocodec.h" using namespace VideoCodec; using namespace std; ConfigurationElement* parseConfiguration(int argc, char* argv[]); int main(int argc, char* argv[]) {    // Parse the command line arguments.    Configuration* configuration = new Configuration(parseConfiguration(argc, argv));    CodecFactory* factory = new CodecFactory();    Codec* codec = factory->GetCodec(configuration);        // Run the encoding based on the configuration.    codec->Run();        delete codec;    delete factory;    delete configuration;    return 0; } int lengthOnLevel(char* string, char sought) {    int level = 0;    int length = 0;        while (!(*string == sought) || (level != 0))    {       if (*string == '[')          level++;       else if (*string == ']')          level--;       // Break out if the string is not closed.       if (level < 0)          break;       string++;       length++;    }        return length; } ConfigurationElement* recursiveGetConfigurationInfo(char* text, int length) {    // The configuration is empty if there is no defining string.    if (length == 0)       return new ConfigurationElement();        // Establish the type of this part.    int index = 0;    int type = -1;    while (index < length)    {       if (text[index] == '=')       {          type = CONFIGURATION_PAIRS;          break;       }       index++;    }        if (type == -1 && text[0] == '[')       type = CONFIGURATION_LIST;        if (type == -1) {       index = 0;       while (index < length)       {          if (isalpha(text[index]))          {             type = CONFIGURATION_STRING;             break;          }          else if (text[index] == '.')             type = CONFIGURATION_FLOAT;          index++;       }              if (type == -1)          type = CONFIGURATION_INTEGER;    }        string* value = new string(text, length);    switch (type)    {       case CONFIGURATION_PAIRS:       {          delete value;          configurationPairs* elements = new configurationPairs();                    int index = 0;          char* moduleStart = text;          int moduleLength = 0;          string* moduleName = NULL;                    while (index < length)          {             if (text[index] == '=')             {                moduleName = new string(moduleStart, moduleLength);                                int length = lengthOnLevel(moduleStart + moduleLength + 1, ',');                                (*elements)[moduleName] = recursiveGetConfigurationInfo(moduleStart + moduleLength + 1, length);                                index += length + 1;                moduleStart = text + index + 1;                moduleLength = -1;             }             index++;             moduleLength++;          }                    ConfigurationElement* element = new ConfigurationElement();          element->SetPairs(elements);          return element;       }       case CONFIGURATION_LIST:       {          delete value;          configurationList* elements = new configurationList();          int start = 1;          int index = 1; // Skip the preceding bracket.          while (index < length)          {             if (text[index] == ',')             {                elements->insert(elements->end(), recursiveGetConfigurationInfo(text + start, index - start));                                start = index + 1;             }                          index++;          }                    // Get the last item in the list, omitting the final bracket.          if (index > start)             elements->insert(elements->end(), recursiveGetConfigurationInfo(text + start, index - start - 1));                    ConfigurationElement* element = new ConfigurationElement();          element->SetList(elements);                    return element;       }       case CONFIGURATION_INTEGER:       {          int* integerValue = new int;          sscanf(value->c_str(), "%d", integerValue);                    ConfigurationElement* element = new ConfigurationElement();          element->SetInteger(integerValue);                    delete value;          return element;       }       case CONFIGURATION_FLOAT:       {          float* floatValue = new float;          sscanf(value->c_str(), "%f", floatValue);                    ConfigurationElement* element = new ConfigurationElement();          element->SetFloat(floatValue);                    delete value;          return element;       }       case CONFIGURATION_STRING:       {          ConfigurationElement* element = new ConfigurationElement();          element->SetString(value);                    return element;       }       default:       {          delete value;                    return new ConfigurationElement();       }    } } ConfigurationElement* parseConfiguration(int argc, char* argv[]) {    int totalLength = 0;    for (int i = 1; i < argc; i++)       totalLength += strlen(argv[i]);        char* fullArgs = new char[totalLength + 1];    char* current = fullArgs;    for (int i = 1; i < argc; i++)    {       strcpy(current, argv[i]);       current += strlen(argv[i]);    }    *current = '\0';        // Now that the full arguments have been assembled, extract out all the information into the configuration structure.        configurationPairs* configuration = new configurationPairs();        // Parse each separate grouping.    char* location = fullArgs;    while (*location != '\0')    {       if (*location == '-')       {          location++;                    int index = 0;          while (location[index] != '[')             index++;                       // Copy in the module name, up to the first bracket.          string* moduleName = new string(location, index);                    // Work out the length of the arguments based on a stack of brackets.          int argumentsLength = lengthOnLevel(location + index + 1, ']');                    // Store this pair.          (*configuration)[moduleName] = recursiveGetConfigurationInfo(location + index + 1, argumentsLength);       }              location++;    }        delete[] fullArgs;        ConfigurationElement* root = new ConfigurationElement();    root->SetPairs(configuration);        return root; }