Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5020 in orxonox.OLD for orxonox


Ignore:
Timestamp:
Aug 14, 2005, 11:44:49 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: IniParser: added the ability to write to files

Location:
orxonox/trunk/src/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/gui/gui_keys.cc

    r4836 r5020  
    1 /* 
     1/*
    22   orxonox - the future of 3D-vertical-scrollers
    33
     
    1616   You should have received a copy of the GNU General Public License
    1717   along with this program; if not, write to the Free Software Foundation,
    18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
     18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1919
    2020
     
    3838  {
    3939    Box* keysBox0;          //!< The Frame that holds the keyOptions.
    40    
     40
    4141    //  keysFrame->setGroupName("Keyboard");
    4242    keysBox0 = new Box('v');
     
    4747      keysBox1 = new Box('h');
    4848      {
    49         PlayerKeys* player1;       //!< The first Player's keys.
    50         PlayerKeys* player2;       //!< The seconds Player's keys.
    51        
    52         player1 = new PlayerKeys(CONFIG_SECTION_PLAYER "1");
    53         player2 = new PlayerKeys(CONFIG_SECTION_PLAYER "2");
    54        
    55         keysBox1->fill(player1->getOpenButton());
    56         keysBox1->fill(player2->getOpenButton());
     49        PlayerKeys* player1;       //!< The first Player's keys.
     50        PlayerKeys* player2;       //!< The seconds Player's keys.
     51
     52        player1 = new PlayerKeys(CONFIG_SECTION_PLAYER "1");
     53        player2 = new PlayerKeys(CONFIG_SECTION_PLAYER "2");
     54
     55        keysBox1->fill(player1->getOpenButton());
     56        keysBox1->fill(player2->getOpenButton());
    5757      }
    5858      keysBox0->fill(keysBox1);
     
    6363    keysFrame->fill(keysBox0);
    6464  }
    65  
     65
    6666  // setting up the Input Windows that receives keystrokes.
    6767  this->inputWindow = new Window("inputWindow");
     
    7272#endif /* HAVE_GTK2 */
    7373  this->inputWindow->fill(inputButton);
    74  
     74
    7575  this->setMainWidget(keysFrame);
    7676}
     
    112112      pKeysBox = new Box('v');
    113113      {
    114         Button* closeButton;    //!< The CloseButton for this key-settings.
    115 
    116         pKeysBox->setGroupName(player);
    117         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_UP, "UP"));
    118         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_DOWN, "DOWN"));
    119         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_LEFT, "LEFT"));
    120         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_RIGHT, "RIGHT"));
    121         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_FIRE, "SPACE"));
    122         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_NEXT_WEAPON, "m"));
    123         pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_PREV_WEAPON, "n"));
    124         closeButton = new Button("close");
    125 #ifdef HAVE_GTK2
    126         closeButton->connectSignal("button_press_event", pKeyWindow, Window::windowClose);
    127 #endif /* HAVE_GTK2 */
    128 
    129         pKeysBox->fill(closeButton);
     114        Button* closeButton;    //!< The CloseButton for this key-settings.
     115
     116        pKeysBox->setGroupName(player);
     117        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_UP, "UP"));
     118        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_DOWN, "DOWN"));
     119        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_LEFT, "LEFT"));
     120        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_RIGHT, "RIGHT"));
     121        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_FIRE, "SPACE"));
     122        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_NEXT_WEAPON, "m"));
     123        pKeysBox->fill(addKey(CONFIG_NAME_PLAYER_PREV_WEAPON, "n"));
     124        closeButton = new Button("close");
     125#ifdef HAVE_GTK2
     126        closeButton->connectSignal("button_press_event", pKeyWindow, Window::windowClose);
     127#endif /* HAVE_GTK2 */
     128
     129        pKeysBox->fill(closeButton);
    130130      }
    131131      pKeyFrame->fill(pKeysBox);
     
    139139  pKeyWindow->connectSignal("delete_event", pKeyWindow, Window::windowClose);
    140140#endif /* HAVE_GTK2 */
    141  
     141
    142142  this->openButton = new Button(player);
    143143#ifdef HAVE_GTK2
     
    174174      keysBox = new Box('v');
    175175      {
    176         Button* closeButton;    //!< The CloseButton for this key-settings.
    177 
    178         keysBox->setGroupName(CONFIG_SECTION_MISC_KEYS);
    179         keysBox->fill(addKey(CONFIG_NAME_QUIT , "ESCAPE"));
    180         keysBox->fill(addKey(CONFIG_NAME_PAUSE, "PAUSE"));
    181         keysBox->fill(addKey(CONFIG_NAME_NEXT_WORLD, "x"));
    182         keysBox->fill(addKey(CONFIG_NAME_PREV_WORLD, "z"));
    183         keysBox->fill(addKey(CONFIG_NAME_VIEW0, "1"));
    184         keysBox->fill(addKey(CONFIG_NAME_VIEW1, "2"));
    185         keysBox->fill(addKey(CONFIG_NAME_VIEW2, "3"));
    186         keysBox->fill(addKey(CONFIG_NAME_VIEW3, "4"));
    187         keysBox->fill(addKey(CONFIG_NAME_VIEW4, "5"));
    188         keysBox->fill(addKey(CONFIG_NAME_VIEW5, "6"));
    189 
    190         closeButton = new Button("close");
    191 #ifdef HAVE_GTK2
    192         closeButton->connectSignal("button_press_event", keyWindow, Window::windowClose);
    193 #endif /* HAVE_GTK2 */
    194 
    195         keysBox->fill(closeButton);
     176        Button* closeButton;    //!< The CloseButton for this key-settings.
     177
     178        keysBox->setGroupName(CONFIG_SECTION_MISC_KEYS);
     179        keysBox->fill(addKey(CONFIG_NAME_QUIT , "ESCAPE"));
     180        keysBox->fill(addKey(CONFIG_NAME_PAUSE, "PAUSE"));
     181        keysBox->fill(addKey(CONFIG_NAME_NEXT_WORLD, "x"));
     182        keysBox->fill(addKey(CONFIG_NAME_PREV_WORLD, "z"));
     183        keysBox->fill(addKey(CONFIG_NAME_VIEW0, "1"));
     184        keysBox->fill(addKey(CONFIG_NAME_VIEW1, "2"));
     185        keysBox->fill(addKey(CONFIG_NAME_VIEW2, "3"));
     186        keysBox->fill(addKey(CONFIG_NAME_VIEW3, "4"));
     187        keysBox->fill(addKey(CONFIG_NAME_VIEW4, "5"));
     188        keysBox->fill(addKey(CONFIG_NAME_VIEW5, "6"));
     189
     190        closeButton = new Button("close");
     191#ifdef HAVE_GTK2
     192        closeButton->connectSignal("button_press_event", keyWindow, Window::windowClose);
     193#endif /* HAVE_GTK2 */
     194
     195        keysBox->fill(closeButton);
    196196      }
    197197      keyFrame->fill(keysBox);
     
    204204  keyWindow->connectSignal("delete_event", keyWindow, Window::windowClose);
    205205#endif /* HAVE_GTK2 */
    206  
     206
    207207  this->openButton = new Button("misc");
    208208#ifdef HAVE_GTK2
     
    254254  GuiKeys::inputWindow->open();
    255255}
    256  
     256
    257257/**
    258258 *  Function which gets keystrokes
     
    281281      strcpy(title, "RIGHT");
    282282      break;
    283      
     283
    284284    case GDK_space:
    285285      strcpy(title, "SPACE");
    286286      break;
    287      
     287
    288288    case GDK_Return:
    289289      strcpy(title, "RETURN");
    290290      break;
    291      
     291
    292292      // Special Keys //
    293293    case GDK_BackSpace:
     
    361361      strcpy(title, "INSERT");
    362362      break;
    363      
     363
    364364    case GDK_Escape:
    365365      strcpy(title, "ESCAPE");
     
    423423      strcpy(title, "F12");
    424424      break;
    425      
    426      
     425
     426
    427427    default:
    428428      char* tmp;
  • orxonox/trunk/src/lib/util/ini_parser.cc

    r5019 r5020  
    3434{
    3535  this->setClassID(CL_INI_PARSER, "IniParser");
    36   this->setName(fileName);
    3736
    3837  this->currentEntry = NULL;
     
    8483  this->currentSection = NULL;
    8584  this->sections = NULL;
     85  this->setName(NULL);
    8686}
    8787
     
    9898  if( fileName == NULL)
    9999    return false;
    100 
    101   printf("1\n");
     100  this->setName(fileName);
     101
    102102  if( (stream = fopen (fileName, "r")) == NULL)
    103103  {
     
    107107  else
    108108  {
    109     printf("2\n");
    110109    this->currentEntry = NULL;
    111110    this->currentSection = NULL;
     
    134133        {
    135134          *ptr = 0;
    136           IniSection* newSection = new IniSection;
    137           newSection->name = new char[strlen(buffer)+1];
    138           strcpy(newSection->name, buffer);
    139           newSection->entries = new tList<IniEntry>;
    140           this->currentSection = newSection;
    141           this->sections->add(newSection);
     135          this->addSection(buffer);
    142136        }
    143137      }
     
    163157        nameEnd[1] = '\0';
    164158
    165         IniEntry* newEntry = new IniEntry;
    166         newEntry->value = new char[strlen(valueBegin)+1];
    167         strcpy(newEntry->value, valueBegin);
    168         newEntry->name = new char[strlen (nameBegin)+1];
    169         strcpy(newEntry->name, nameBegin);
    170 
    171         this->currentSection->entries->add(newEntry);
     159        this->addVar(nameBegin, valueBegin);
    172160      }
    173161    }
     
    175163  fclose(stream);
    176164  return true;
     165}
     166
     167/**
     168 * opens a file and writes to it
     169 * @param fileName: path and name of the new file to write to
     170 * @return true on success false otherwise
     171 */
     172bool IniParser::writeFile(const char* fileName)
     173{
     174  FILE*    stream;           //!< The stream we use to read the file.
     175  if (sections != NULL)
     176    deleteSections();
     177  if( fileName == NULL)
     178    return false;
     179
     180  if( (stream = fopen (fileName, "w")) == NULL)
     181  {
     182    PRINTF(1)("IniParser could not open %s\n", fileName);
     183    return false;
     184  }
     185  else
     186  {
     187    if (this->sections)
     188    {
     189      tIterator<IniSection>* sectionIt = this->sections->getIterator();
     190      IniSection* sectionEnum = sectionIt->nextElement();
     191      while (sectionEnum)
     192      {
     193        fprintf(stream, " [%s]\n", sectionEnum->name);
     194
     195        tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
     196        IniEntry* entryEnum = entryIt->nextElement();
     197        while (entryEnum)
     198        {
     199          fprintf(stream, " %s = %s\n", entryEnum->name, entryEnum->value);
     200
     201          entryEnum = entryIt->nextElement();
     202        }
     203        delete entryIt;
     204
     205        sectionEnum = sectionIt->nextElement();
     206      }
     207      delete sectionIt;
     208    }
     209    else
     210      PRINTF(1)("%s no sections defined yet\n", fileName);
     211  }
     212  fclose(stream);
     213}
     214
     215bool IniParser::addSection(const char* sectionName)
     216{
     217  if (this->sections == NULL)
     218    this->sections = new tList<IniSection>;
     219
     220  IniSection* newSection = new IniSection;
     221  newSection->name = new char[strlen(sectionName)+1];
     222  strcpy(newSection->name, sectionName);
     223  newSection->entries = new tList<IniEntry>;
     224  this->currentSection = newSection;
     225  this->sections->add(newSection);
    177226}
    178227
     
    269318  else
    270319    return true;
     320}
     321
     322/**
     323 * adds a new Entry to either the currentSection or the section called by sectionName
     324 * @param entryName the Name of the Entry to add
     325 * @param value the value to assign to this entry
     326 * @param sectionName if NULL then this entry will be set to the currentSection
     327 * otherwise to the section refered to by sectionName.
     328 * If both are NULL no entry will be added
     329 * @return true if everything is ok false on error
     330 */
     331bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
     332{
     333  IniSection* addSection = NULL;
     334  if (sectionName != NULL)
     335  {
     336    tIterator<IniSection>* sectionIt = this->sections->getIterator();
     337    IniSection* sectionEnum = sectionIt->nextElement();
     338    while (sectionEnum)
     339    {
     340      if (!strcmp(sectionEnum->name, sectionName))
     341      {
     342        addSection = sectionEnum;
     343        break;
     344      }
     345      sectionEnum = sectionIt->nextElement();
     346    }
     347    delete sectionIt;
     348  }
     349  else
     350    addSection = this->currentSection;
     351
     352  if (addSection == NULL)
     353  {
     354    PRINTF(2)("section not found for value %s\n", entryName);
     355    return false;
     356  }
     357  else
     358  {
     359    IniEntry* newEntry = new IniEntry;
     360    newEntry->name = new char[strlen (entryName)+1];
     361    strcpy(newEntry->name, entryName);
     362    newEntry->value = new char[strlen(value)+1];
     363    strcpy(newEntry->value, value);
     364    this->currentSection->entries->add(newEntry);
     365    return true;
     366  }
    271367}
    272368
     
    277373 * @param defaultValue: what should be returned in case the entry cannot be found
    278374 * @return a pointer to a buffer conatining the value of the specified entry. This buffer will contain the data specified in defvalue in case the entry wasn't found
    279 
    280    The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly
    281    lead to unwanted behaviour.
     375 *
     376 *  The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly
     377 * lead to unwanted behaviour.
    282378*/
    283379const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
  • orxonox/trunk/src/lib/util/ini_parser.h

    r5018 r5020  
    4242    ~IniParser ();
    4343
    44     bool readFile(const char* name);
     44    bool readFile(const char* fileName);
     45    bool writeFile(const char* fileName);
    4546
     47    bool addSection(const char* sectionName);
    4648    bool getSection(const char* sectionName);
    4749
     
    5254    bool isOpen() const { return (sections != NULL)?true:false; };
    5355
     56    bool addVar(const char* entryName, const char* value, const char* sectionName = NULL);
    5457    const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const;
    5558
Note: See TracChangeset for help on using the changeset viewer.