You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2008/08/16 22:39:40 UTC

svn commit: r686558 - /incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl

Author: jan
Date: Sat Aug 16 13:39:40 2008
New Revision: 686558

URL: http://svn.apache.org/viewvc?rev=686558&view=rev
Log:
Add missing functionality where previously unspecified ini-sections wouldn't be appended to the local config file.

Modified:
    incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl

Modified: incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl?rev=686558&r1=686557&r2=686558&view=diff
==============================================================================
--- incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl (original)
+++ incubator/couchdb/branches/runtimeconfig/src/couchdb/couch_config_writer.erl Sat Aug 16 13:39:40 2008
@@ -19,6 +19,8 @@
 %% @see couch_config
 
 -module(couch_config_writer).
+-include("couch_db.hrl").
+
 -export([save_to_file/2]).
 
 %% @spec save_to_file(
@@ -26,16 +28,28 @@
 %%           File::filename()) -> ok
 %% @doc Saves a Module/Key/Value triple to the ini file File::filename()
 save_to_file({{Module, Variable}, Value}, File) ->
+    
+    ?LOG_DEBUG("saving to file '~s', Congif: '~p'", [File, {{Module, Variable}, Value}]),
+    
     % open file and create a list of lines
     {ok, Stream} = file:read_file(File),
-    {ok, Lines} = regexp:split(binary_to_list(Stream), "\r\n|\n|\r|\032"),
+    OldFileContents = binary_to_list(Stream),
+    {ok, Lines} = regexp:split(OldFileContents, "\r\n|\n|\r|\032"),
     
     % prepare input variables
     ModuleName = "[" ++ Module ++ "]",
     VariableList = Variable,
     
     % produce the contents for the config file
-    NewFileContents = save_loop({{ModuleName, VariableList}, Value}, Lines, "", "", []),
+    NewFileContents = 
+    case NewFileContents2 = save_loop({{ModuleName, VariableList}, Value}, Lines, "", "", []) of
+        % we didn't change anything, that means we couldn't find a matching
+        % [ini section] in which case we just append a new one.
+        OldFileContents ->
+            append_new_ini_section({{ModuleName, VariableList}, Value}, OldFileContents);
+        _ ->
+            NewFileContents2
+    end,
     
     % do the save, close the config file and get out
     save_file(File, NewFileContents),
@@ -43,7 +57,7 @@
     ok.
 
 %% @doc Iterates over the lines of an ini file and replaces or adds a new
-%%      coofiguration directive.
+%%      configuration directive.
 save_loop({{Module, Variable}, Value}, [Line|Rest], OldCurrentModule, Contents, DoneVariables) ->
 
     % if we find a new [ini section] (Module), save that for reference
@@ -93,6 +107,9 @@
     % we're out of new lines, just return the new file's contents
     NewFileContents.
 
+append_new_ini_section({{ModuleName, Variable}, Value}, OldFileContents) ->
+    OldFileContents ++ "\n\n" ++ ModuleName ++ "\n" ++  Variable ++ "=" ++ Value ++ "\n".
+
 %% @spec parse_module(Lins::string(), OldModule::string()) -> string()
 %% @doc Tries to match a line against a pattern specifying a ini module or 
 %%      section ("[Module]"). Returns OldModule if no match is found.