aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2013-06-16 19:52:56 -0700
committerChris Robinson <[email protected]>2013-06-16 19:52:56 -0700
commit784e396cc7e4452faaa02caed445bccc10a15d5f (patch)
tree8e663047a4668bbf2640c8d048605d473db3648e
parent8bd77be5222a62b060f1cfeb5d62cb44ced3a250 (diff)
Don't split config sections into separate blocks
-rw-r--r--Alc/alcConfig.c115
1 files changed, 42 insertions, 73 deletions
diff --git a/Alc/alcConfig.c b/Alc/alcConfig.c
index 8757c3ff..5d9448b0 100644
--- a/Alc/alcConfig.c
+++ b/Alc/alcConfig.c
@@ -45,13 +45,11 @@ typedef struct ConfigEntry {
} ConfigEntry;
typedef struct ConfigBlock {
- char *name;
ConfigEntry *entries;
unsigned int entryCount;
} ConfigBlock;
-static ConfigBlock *cfgBlocks;
-static unsigned int cfgCount;
+static ConfigBlock cfgBlock;
static char buffer[1024];
@@ -73,7 +71,7 @@ static char *rstrip(char *line)
static void LoadConfigFromFile(FILE *f)
{
- ConfigBlock *curBlock = cfgBlocks;
+ char curSection[128] = "";
ConfigEntry *ent;
while(fgets(buffer, sizeof(buffer), f))
@@ -95,10 +93,8 @@ static void LoadConfigFromFile(FILE *f)
if(line[0] == '[')
{
- ConfigBlock *nextBlock;
char *section = line+1;
char *endsection;
- unsigned int i;
endsection = strchr(section, ']');
if(!endsection || section == endsection || endsection[1] != 0)
@@ -108,36 +104,14 @@ static void LoadConfigFromFile(FILE *f)
}
*endsection = 0;
- nextBlock = NULL;
- for(i = 0;i < cfgCount;i++)
+ if(strcasecmp(section, "general") == 0)
+ curSection[0] = 0;
+ else
{
- if(strcasecmp(cfgBlocks[i].name, section) == 0)
- {
- nextBlock = cfgBlocks+i;
- TRACE("found block '%s'\n", nextBlock->name);
- break;
- }
+ strncpy(curSection, section, sizeof(curSection)-1);
+ curSection[sizeof(curSection)-1] = 0;
}
- if(!nextBlock)
- {
- nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
- if(!nextBlock)
- {
- ERR("config parse error: error reallocating config blocks\n");
- continue;
- }
- cfgBlocks = nextBlock;
- nextBlock = cfgBlocks+cfgCount;
- cfgCount++;
-
- nextBlock->name = strdup(section);
- nextBlock->entries = NULL;
- nextBlock->entryCount = 0;
-
- TRACE("found new block '%s'\n", nextBlock->name);
- }
- curBlock = nextBlock;
continue;
}
@@ -162,27 +136,35 @@ static void LoadConfigFromFile(FILE *f)
}
rstrip(key);
+ if(curSection[0] != 0)
+ {
+ size_t len = strlen(curSection);
+ memmove(&key[len+1], key, sizeof(key)-1-len);
+ key[len] = '/';
+ memcpy(key, curSection, len);
+ }
+
/* Check if we already have this option set */
- ent = curBlock->entries;
- while((unsigned int)(ent-curBlock->entries) < curBlock->entryCount)
+ ent = cfgBlock.entries;
+ while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
{
if(strcasecmp(ent->key, key) == 0)
break;
ent++;
}
- if((unsigned int)(ent-curBlock->entries) >= curBlock->entryCount)
+ if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
{
/* Allocate a new option entry */
- ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
+ ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
if(!ent)
{
ERR("config parse error: error reallocating config entries\n");
continue;
}
- curBlock->entries = ent;
- ent = curBlock->entries + curBlock->entryCount;
- curBlock->entryCount++;
+ cfgBlock.entries = ent;
+ ent = cfgBlock.entries + cfgBlock.entryCount;
+ cfgBlock.entryCount++;
ent->key = strdup(key);
ent->value = NULL;
@@ -200,10 +182,6 @@ void ReadALConfig(void)
const char *str;
FILE *f;
- cfgBlocks = calloc(1, sizeof(ConfigBlock));
- cfgBlocks->name = strdup("general");
- cfgCount = 1;
-
#ifdef _WIN32
if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
{
@@ -249,51 +227,42 @@ void FreeALConfig(void)
{
unsigned int i;
- for(i = 0;i < cfgCount;i++)
+ for(i = 0;i < cfgBlock.entryCount;i++)
{
- unsigned int j;
- for(j = 0;j < cfgBlocks[i].entryCount;j++)
- {
- free(cfgBlocks[i].entries[j].key);
- free(cfgBlocks[i].entries[j].value);
- }
- free(cfgBlocks[i].entries);
- free(cfgBlocks[i].name);
+ free(cfgBlock.entries[i].key);
+ free(cfgBlock.entries[i].value);
}
- free(cfgBlocks);
- cfgBlocks = NULL;
- cfgCount = 0;
+ free(cfgBlock.entries);
}
const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
{
- unsigned int i, j;
+ unsigned int i;
+ char key[256];
if(!keyName)
return def;
- if(!blockName)
- blockName = "general";
-
- for(i = 0;i < cfgCount;i++)
+ if(blockName && strcasecmp(blockName, "general") != 0)
+ snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
+ else
{
- if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
- continue;
+ strncpy(key, keyName, sizeof(key)-1);
+ key[sizeof(key)-1] = 0;
+ }
- for(j = 0;j < cfgBlocks[i].entryCount;j++)
+ for(i = 0;i < cfgBlock.entryCount;i++)
+ {
+ if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
{
- if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
- {
- TRACE("Found %s:%s = \"%s\"\n", blockName, keyName,
- cfgBlocks[i].entries[j].value);
- if(cfgBlocks[i].entries[j].value[0])
- return cfgBlocks[i].entries[j].value;
- return def;
- }
+ TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
+ if(cfgBlock.entries[i].value[0])
+ return cfgBlock.entries[i].value;
+ return def;
}
}
- TRACE("Key %s:%s not found\n", blockName, keyName);
+ TRACE("Key %s not found\n", key);
return def;
}
href='#n374'>374 375
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package net.java.games.util;

import java.io.*;
import java.nio.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;

/** Simplified clone of DxTex tool from the DirectX SDK, written in
    Java using the DDSReader; tests fetching of texture data */

public class DxTex {
  private InternalFrameListener frameListener;
  private File defaultDirectory;
  private JDesktopPane desktop;
  private static String endl = System.getProperty("line.separator");
  private JMenu mipMapMenu;

  public static void main(String[] args) {
    new DxTex().run(args);
  }

  private void run(String[] args) {
    defaultDirectory = new File(System.getProperty("user.dir"));
    JFrame frame = new JFrame("DirectX Texture Tool");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = createMenu("File", 'F', 0);
    JMenuItem item =
      createMenuItem("Open...",
                     new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
                           openFile();
                         }
                       },
                     KeyEvent.VK_O, InputEvent.CTRL_MASK,
                     'O', 0);
    menu.add(item);
    item =
      createMenuItem("Exit",
                     new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
                           System.exit(0);
                         }
                       },
                     KeyEvent.VK_Q, InputEvent.CTRL_MASK,
                     'x', 1);
    menu.add(item);
    menuBar.add(menu);

    menu = createMenu("MipMap", 'M', 0);
    menu.setEnabled(false);
    mipMapMenu = menu;
    menuBar.add(menu);

    frame.setJMenuBar(menuBar);

    desktop = new JDesktopPane();
    frame.getContentPane().add(desktop);
    frame.setSize(640, 480);
    frame.show();

    frameListener = new InternalFrameAdapter() {
        public void internalFrameActivated(InternalFrameEvent e) {
          JInternalFrame ifr = e.getInternalFrame();
          if (ifr instanceof ImageFrame) {
            // Recompute entries in mip map menu
            final ImageFrame frame = (ImageFrame) ifr;
            if (frame.getNumMipMaps() > 0) {
              mipMapMenu.removeAll();
              // Add entries
              for (int i = 0; i < frame.getNumMipMaps(); i++) {
                final int map = i;
                JMenuItem item;
                String title = "Level " + (i + 1);
                ActionListener listener = new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                      SwingUtilities.invokeLater(new Runnable() {
                          public void run() {
                            frame.setMipMapLevel(map);
                          }
                        });
                    }
                  };
                if (i < 9) {
                  char c = (char) ('0' + i + 1);
                  item = createMenuItem(title, listener, c, 6);
                } else {
                  item = createMenuItem(title, listener);
                }
                mipMapMenu.add(item);
              }
              mipMapMenu.setEnabled(true);
            } else {
              mipMapMenu.setEnabled(false);
            }
          } else {
            mipMapMenu.setEnabled(false);
          }
        }

        public void internalFrameClosing(InternalFrameEvent e) {
          desktop.remove(e.getInternalFrame());
          desktop.invalidate();
          desktop.validate();
          desktop.repaint();
          // THIS SHOULD NOT BE NECESSARY
          desktop.requestFocus();
        }        

        public void internalFrameClosed(InternalFrameEvent e) {
          JInternalFrame ifr = e.getInternalFrame();
          if (ifr instanceof ImageFrame) {
            ((ImageFrame) ifr).close();
          }
        }
      };

    for (int i = 0; i < args.length; i++) {
      final File file = new File(args[i]);
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            openFile(file);
          }
        });
    }
  }

  //----------------------------------------------------------------------
  // Actions
  //

  private void openFile() {
    JFileChooser chooser = new JFileChooser(defaultDirectory);
    chooser.setMultiSelectionEnabled(false);
    chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
          return (f.isDirectory() ||
                  f.getName().endsWith(".dds"));
        }
        
        public String getDescription() {
          return "Texture files (*.dds)";
        }
      });

    int res = chooser.showOpenDialog(null);
    if (res == JFileChooser.APPROVE_OPTION) {
      final File file = chooser.getSelectedFile();
      defaultDirectory = file.getParentFile();
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            openFile(file);
          }
        });
    }
  }

  private void openFile(File file) {
    try {
      DDSReader reader = new DDSReader();
      reader.loadFile(file.getAbsolutePath());
      showImage(file.getName(), reader, 0);
    } catch (IOException e) {
      showMessageDialog("Error while opening file:" + endl +
                        exceptionToString(e),
                        "Error opening file",
                        JOptionPane.WARNING_MESSAGE);
    }
  }

  //----------------------------------------------------------------------
  // Image display
  //

  private void showImage(String filename, DDSReader reader, int mipMapLevel) {
    try {
      ImageFrame fr = new ImageFrame(filename, reader, mipMapLevel);
      desktop.add(fr);