speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/4] replace switch operator in load_output module with if statem


From: Andrei Kholodnyi
Subject: [PATCH 1/4] replace switch operator in load_output module with if statements.
Date: Tue, 5 Oct 2010 00:00:12 +0200

long switch operator was defined for the fork function,
however at the end of 2 possible cases were return and assert.

switch operator was replaced with two if cases.
---
 src/server/module.c |  201 ++++++++++++++++++++++++++-------------------------
 1 files changed, 101 insertions(+), 100 deletions(-)

diff --git a/src/server/module.c b/src/server/module.c
index 316c475..0c4157d 100644
--- a/src/server/module.c
+++ b/src/server/module.c
@@ -106,9 +106,12 @@ load_output_module(char* mod_name, char* mod_prog, char* 
mod_cfgfile, char* mod_
 
 
     fr = fork();
-    switch(fr){
-    case -1: printf("Can't fork, error! Module not loaded."); return NULL;
-    case 0:
+    if (fr == -1) {
+        printf("Can't fork, error! Module not loaded.");
+        return NULL;
+    }
+
+    if (fr == 0) {
         ret = dup2(module->pipe_in[0], 0);
         close(module->pipe_in[0]);
         close(module->pipe_in[1]);
@@ -133,120 +136,118 @@ load_output_module(char* mod_name, char* mod_prog, 
char* mod_cfgfile, char* mod_
             }
         }
         assert(0);
-    default:
+    }
 
-       if (cfg) g_free(arg1);
+    if (cfg) g_free(arg1);
 
-        module->pid = fr;
-        close(module->pipe_in[0]);
-        close(module->pipe_out[1]);
+    module->pid = fr;
+    close(module->pipe_in[0]);
+    close(module->pipe_out[1]);
 
-        usleep(100);            /* So that the other child has at least time 
to fail
+    usleep(100);            /* So that the other child has at least time to 
fail
                                    with the execlp */
-        ret = waitpid(module->pid, NULL, WNOHANG);
-        if (ret != 0){
-            MSG(2, "ERROR: Can't load output module %s with binary %s. Bad 
filename in configuration?", 
-               module->name, module->filename);
-            destroy_module(module);
-            return NULL;
-        }
+    ret = waitpid(module->pid, NULL, WNOHANG);
+    if (ret != 0){
+        MSG(2, "ERROR: Can't load output module %s with binary %s. Bad 
filename in configuration?",
+           module->name, module->filename);
+        destroy_module(module);
+        return NULL;
+    }
 
-        module->working = 1;
-        MSG(2, "Module %s loaded.", module->name);        
+    module->working = 1;
+    MSG(2, "Module %s loaded.", module->name);
 
-       /* Create a stream from the socket */
-       module->stream_out = fdopen(module->pipe_out[0], "r");
-       if (!module->stream_out) FATAL("Can't create a stream for socket, 
fdopen() failed.");
-       /* Switch to line buffering mode */
-       ret = setvbuf(module->stream_out, NULL, _IONBF, 4096);
-       if (ret) FATAL("Can't set line buffering, setvbuf failed.");
+    /* Create a stream from the socket */
+    module->stream_out = fdopen(module->pipe_out[0], "r");
+    if (!module->stream_out) FATAL("Can't create a stream for socket, fdopen() 
failed.");
 
-       MSG(4, "Trying to initialize %s.", module->name);
-       if (output_send_data("INIT\n", module, 0) != 0){
-           MSG(1, "ERROR: Something wrong with %s, can't initialize", 
module->name);
-           output_close(module);
-           return NULL;
-       }else{
-           char *rep_line = g_malloc(1024);
-           FILE *f;
-           size_t n = 1024;
-           char s;
-           GString *reply;
-
-           reply = g_string_new("\n---------------\n");
-           f = fdopen(dup(module->pipe_out[0]), "r");
-           while(1){
-               ret = spd_getline(&rep_line, &n, f);
-               if (ret <= 0){
-                   MSG(1, "ERROR: Bad syntax from output module %s 1", 
module->name);
-                   if (rep_line != NULL)
-                        g_free(rep_line);
-                   return NULL;
-               }
-               assert(rep_line != NULL);
-               MSG(5, "Reply from output module: %d %s", n, rep_line);
-               if (ret <= 4){
-                   MSG(1, "ERROR: Bad syntax from output module %s 2", 
module->name);
-                   g_free(rep_line);
-                   return NULL;
-               }
-
-               if (rep_line[3] != '-') {
-                   s = rep_line[0];
-                   g_free(rep_line);
-                   break;
-               }
-
-               g_string_append(reply, rep_line + 4);
-           }
+    /* Switch to line buffering mode */
+    ret = setvbuf(module->stream_out, NULL, _IONBF, 4096);
+    if (ret) FATAL("Can't set line buffering, setvbuf failed.");
 
-           if (SpeechdOptions.debug){
-             MSG(4, "Switching debugging on for output module %s", 
module->name);
-             output_module_debug(module);
+    MSG(4, "Trying to initialize %s.", module->name);
+    if (output_send_data("INIT\n", module, 0) != 0){
+       MSG(1, "ERROR: Something wrong with %s, can't initialize", 
module->name);
+       output_close(module);
+       return NULL;
+    }else{
+        char *rep_line = g_malloc(1024);
+       FILE *f;
+       size_t n = 1024;
+       char s;
+       GString *reply;
+
+       reply = g_string_new("\n---------------\n");
+       f = fdopen(dup(module->pipe_out[0]), "r");
+       while(1){
+           ret = spd_getline(&rep_line, &n, f);
+           if (ret <= 0){
+               MSG(1, "ERROR: Bad syntax from output module %s 1", 
module->name);
+               if (rep_line != NULL)
+                    g_free(rep_line);
+               return NULL;
            }
-           /* Initialize audio settings */
-           ret = output_send_audio_settings(module);
-           if (ret !=0){
-             MSG(1, "ERROR: Can't initialize audio in output module, see 
reason above.");
-             module->working = 0;
-             kill(module->pid, 9);
-             waitpid(module->pid, NULL, WNOHANG);
-             destroy_module(module);
-             return NULL;
+           assert(rep_line != NULL);
+           MSG(5, "Reply from output module: %d %s", n, rep_line);
+           if (ret <= 4){
+               MSG(1, "ERROR: Bad syntax from output module %s 2", 
module->name);
+               g_free(rep_line);
+               return NULL;
            }
 
-           /* Send log level configuration setting */
-           ret = output_send_loglevel_setting(module);
-           if (ret !=0){
-             MSG(1, "ERROR: Can't set the log level inin the output module.");
-             module->working = 0;
-             kill(module->pid, 9);
-             waitpid(module->pid, NULL, WNOHANG);
-             destroy_module(module);
-             return NULL;
+           if (rep_line[3] != '-') {
+               s = rep_line[0];
+               g_free(rep_line);
+               break;
            }
 
-           /* Get a list of supported voices */
-           _output_get_voices(module);
-           fclose(f);
-           g_string_append_printf(reply, "---------------\n");
-
-           if (s == '2') MSG(2, "Module %s started sucessfully with message: 
%s", module->name, reply->str);
-           else if (s == '3'){
-               MSG(1, "ERROR: Module %s failed to initialize. Reason: %s", 
module->name, reply->str);
-               module->working = 0;
-               kill(module->pid, 9);
-               waitpid(module->pid, NULL, WNOHANG);
-               destroy_module(module);
-               return NULL;
-           }
-           g_string_free(reply, 1);
+           g_string_append(reply, rep_line + 4);
+        }
+
+       if (SpeechdOptions.debug){
+            MSG(4, "Switching debugging on for output module %s", 
module->name);
+            output_module_debug(module);
+       }
+       /* Initialize audio settings */
+       ret = output_send_audio_settings(module);
+       if (ret !=0){
+           MSG(1, "ERROR: Can't initialize audio in output module, see reason 
above.");
+           module->working = 0;
+           kill(module->pid, 9);
+           waitpid(module->pid, NULL, WNOHANG);
+           destroy_module(module);
+           return NULL;
        }
 
-        return module;
+       /* Send log level configuration setting */
+       ret = output_send_loglevel_setting(module);
+       if (ret !=0){
+           MSG(1, "ERROR: Can't set the log level inin the output module.");
+           module->working = 0;
+           kill(module->pid, 9);
+           waitpid(module->pid, NULL, WNOHANG);
+           destroy_module(module);
+           return NULL;
+       }
+
+       /* Get a list of supported voices */
+       _output_get_voices(module);
+       fclose(f);
+       g_string_append_printf(reply, "---------------\n");
+
+       if (s == '2') MSG(2, "Module %s started sucessfully with message: %s", 
module->name, reply->str);
+       else if (s == '3'){
+           MSG(1, "ERROR: Module %s failed to initialize. Reason: %s", 
module->name, reply->str);
+           module->working = 0;
+           kill(module->pid, 9);
+           waitpid(module->pid, NULL, WNOHANG);
+           destroy_module(module);
+           return NULL;
+       }
+       g_string_free(reply, 1);
     }
 
-    assert(0);
+    return module;
 }
 
 int
-- 
1.6.0.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]