/*
 * RDV
 * Real-time Data Viewer
 * http://rdv.googlecode.com/
 * 
 * Copyright (c) 2005-2007 University at Buffalo
 * Copyright (c) 2005-2007 NEES Cyberinfrastructure Center
 * Copyright (c) 2008 Palta Software
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * $URL: https://rdv.svn.sourceforge.net/svnroot/rdv/trunk/src/org/rdv/windows/Registry.java $
 * $Revision: 943 $
 * $Date: 2007-12-17 13:42:15 -0500 (Mon, 17 Dec 2007) $
 * $Author: jphanley $
 */

package org.rdv.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * A class to interact from the Windows registry. This class requires that the
 * command line utility 'reg.exe' is on the path.
 * 
 * @author Jason P. Hanley
 */
public class WindowsRegistry {
  /** the command line registry tool */
  private static final String REGISTRY_TOOL = "reg";
  
  /**
   * Reads the string (REG_SZ) for key using the default entry.
   * 
   * @param key           the registry key
   * @return              the value, or null if the key is not found
   * @throws IOException  if there is an error reading the registry
   */
  public static String readString(String key) throws IOException {
    return readString(key, null);
  }

  /**
   * Reads the string (REG_SZ) for key and entry name.
   * 
   * @param key           the registry key
   * @param name          the entry name
   * @return              the value, or null if the key/entry is not found
   * @throws IOException  if there is an error reading the registry
   */
  public static String readString(String key, String name) throws IOException {
    String command = REGISTRY_TOOL + " query \"" + key + "\" ";
    if (name != null) {
      command += "/v " + name;
    } else {
      command += "/ve";
    }
    
    String result = exec(command);
    
    String type = "REG_SZ";
    int p = result.indexOf(type);
    if (p == -1) {
      return null;
    }
    
    return result.substring(p + type.length()).trim();    
  }
  
  /**
   * Execute the command and return the results. This will not return until the
   * command has completed.
   * 
   * @param command     the command to execute
   * @return            the output of the command
   * @throws Exception  if there is an error executing the command
   */
  private static String exec(String command) throws IOException {
    StringBuilder result = new StringBuilder();

    Process process = Runtime.getRuntime().exec(command);
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
    String line;
    while ((line = reader.readLine()) != null) {
      result.append(line);
    }
    
    return result.toString();
  }
}
@@ -1324,94 +1324,8 @@
 $URL
-: https://rdv.svn.sourceforge.net/svnroot/rdv/trunk/src/org/rdv/windows/Registry.java 
 $%0D%0A 
@@ -1339,14 +1339,8 @@
 sion
-: 943 
 $%0D%0A 
@@ -1350,55 +1350,8 @@
 Date
-: 2007-12-17 13:42:15 -0500 (Mon, 17 Dec 2007) 
 $%0D%0A 
@@ -1363,19 +1363,8 @@
 thor
-: jphanley 
 $%0D%0A 

Set keywords.