adopt to winsock
[melted] / src / melted / melted_unit_commands.c
index 43d82af..6ce8e6e 100644 (file)
 #include "melted_commands.h"
 #include "melted_log.h"
 
-int melted_load( command_argument cmd_arg )
-{
-       melted_unit unit = melted_get_unit(cmd_arg->unit);
-       char *filename = (char*) cmd_arg->argument;
-       char fullname[1024];
-       int flush = 1;
-       char *service;
 
-       if ( filename[0] == '!' )
-       {
-               flush = 0;
-               filename ++;
-       }
+void get_fullname( command_argument cmd_arg, char *fullname, size_t len, char *filename )
+{
+       char *service = strchr( filename, ':' );
 
-       service = strchr( filename, ':' );
        if ( service != NULL )
        {
                service = filename;
                filename = strchr( service, ':' );
                *filename ++ = '\0';
-               
+
                if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
                        filename++;
-       
-               snprintf( fullname, 1023, "%s:%s%s", service, cmd_arg->root_dir, filename );
+
+               snprintf( fullname, len, "%s:%s%s", service, cmd_arg->root_dir, filename );
        }
        else
        {
                if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
                        filename++;
 
-               snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
+               snprintf( fullname, len, "%s%s", cmd_arg->root_dir, filename );
        }
-       
+}
+
+int melted_load( command_argument cmd_arg )
+{
+       melted_unit unit = melted_get_unit(cmd_arg->unit);
+       char *filename = (char*) cmd_arg->argument;
+       char fullname[1024];
+       int flush = 1;
+
+       if ( filename[0] == '!' )
+       {
+               flush = 0;
+               filename ++;
+       }
+       get_fullname( cmd_arg, fullname, sizeof(fullname), filename );
+
        if (unit == NULL)
                return RESPONSE_INVALID_UNIT;
        else
@@ -122,11 +127,8 @@ int melted_insert( command_argument cmd_arg )
        char *filename = (char*) cmd_arg->argument;
        char fullname[1024];
 
-       if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
-               filename++;
+       get_fullname( cmd_arg, fullname, sizeof(fullname), filename );
 
-       snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
-       
        if (unit == NULL)
                return RESPONSE_INVALID_UNIT;
        else
@@ -242,11 +244,8 @@ int melted_append( command_argument cmd_arg )
        char *filename = (char*) cmd_arg->argument;
        char fullname[1024];
 
-       if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
-               filename++;
+       get_fullname( cmd_arg, fullname, sizeof(fullname), filename );
 
-       snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
-       
        if (unit == NULL)
                return RESPONSE_INVALID_UNIT;
        else
@@ -271,7 +270,9 @@ int melted_append( command_argument cmd_arg )
 int melted_push( command_argument cmd_arg, mlt_service service )
 {
        melted_unit unit = melted_get_unit(cmd_arg->unit);
-       if ( unit != NULL && service != NULL )
+       if ( !unit )
+               return RESPONSE_INVALID_UNIT;
+       if ( service != NULL )
                if ( melted_unit_append_service( unit, service ) == mvcp_ok )
                        return RESPONSE_SUCCESS;
        return RESPONSE_BAD_FILE;
@@ -280,7 +281,9 @@ int melted_push( command_argument cmd_arg, mlt_service service )
 int melted_receive( command_argument cmd_arg, char *doc )
 {
        melted_unit unit = melted_get_unit(cmd_arg->unit);
-       if ( unit != NULL )
+       if ( unit == NULL )
+               return RESPONSE_INVALID_UNIT;
+       else
        {
                // Get the consumer's profile
                mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
@@ -343,7 +346,9 @@ int melted_rewind( command_argument cmd_arg )
        melted_unit unit = melted_get_unit(cmd_arg->unit);
        if ( unit == NULL )
                return RESPONSE_INVALID_UNIT;
-       else 
+       else if ( melted_unit_has_terminated( unit ) )
+               melted_unit_change_position( unit, 0, 0 );
+       else
                melted_unit_play( unit, -2000 );
        return RESPONSE_SUCCESS;
 }
@@ -379,7 +384,9 @@ int melted_ff( command_argument cmd_arg )
        melted_unit unit = melted_get_unit(cmd_arg->unit);
        if ( unit == NULL )
                return RESPONSE_INVALID_UNIT;
-       else 
+       else if ( melted_unit_has_terminated( unit ) )
+               melted_unit_change_position( unit, 0, 0 );
+       else
                melted_unit_play( unit, 2000 );
        return RESPONSE_SUCCESS;
 }
@@ -461,11 +468,16 @@ int melted_get_unit_property( command_argument cmd_arg )
 {
        melted_unit unit = melted_get_unit(cmd_arg->unit);
        char *name = (char*) cmd_arg->argument;
-       char *value = melted_unit_get( unit, name );
        if (unit == NULL)
+       {
                return RESPONSE_INVALID_UNIT;
-       else if ( value != NULL )
-               mvcp_response_printf( cmd_arg->response, 1024, "%s\n", value );
+       }
+       else
+       {
+               char *value = melted_unit_get( unit, name );
+               if ( value != NULL )
+                       mvcp_response_printf( cmd_arg->response, 1024, "%s\n", value );
+       }
        return RESPONSE_SUCCESS;
 }