]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
refactor handler init variables and abort contitions
authorBastian Dehn <hhaalo@arcor.de>
Thu, 1 Aug 2024 07:33:49 +0000 (09:33 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Thu, 1 Aug 2024 07:33:49 +0000 (09:33 +0200)
src/kds_s2000w_handler.c

index 6733d130eb93cc1bc0604a7842733f2dd477937d..ee3bc32b2b6b2c01432bc608f922850d95304a6e 100644 (file)
@@ -17,21 +17,16 @@ void _get_current_metadata(handler* h)
        debug_printf(ALL, "get_current_metadata");
 #endif
 
-       json_object* metadata = NULL;
-       json_object* config = NULL;
-       blobdata* scanner_image = NULL;
-       blobdata* pnm_image = NULL;
-       image_metadata* mdata = NULL;
-
-       mdata = malloc(sizeof(image_metadata));
+       image_metadata* mdata = malloc(sizeof(image_metadata));
+       json_object* config = json_object_object_get(h->current_scanner_config, "Configuration");
+       json_object* metadata = json_object_object_get(config, "ColorMode");
+       const char* color_value = json_object_get_string(metadata);
+       blobdata* scanner_image = (blobdata*) h->scanner_image;
+       blobdata* pnm_image = (blobdata*) h->pnm_image;
 
        h->current_metadata->valid = 0;
        h->current_metadata->channels = 1;
 
-       config = json_object_object_get(h->current_scanner_config, "Configuration");
-       metadata = json_object_object_get(config, "ColorMode");
-       const char* color_value = json_object_get_string(metadata);
-
        if (strcmp(color_value, "Color") == 0)
                h->current_metadata->format = 1;
 
@@ -44,9 +39,6 @@ void _get_current_metadata(handler* h)
        if (h->current_metadata->format == 1)
                h->current_metadata->channels = 3;
 
-       scanner_image = (blobdata*) h->scanner_image;
-       pnm_image = (blobdata*) h->pnm_image;
-
        kds_s2000w_metadata_from_image(scanner_image, mdata);
        h->current_metadata->depth = mdata->depth;
        h->current_metadata->pixels_per_line = mdata->width;
@@ -81,16 +73,16 @@ void _delete_current_image(handler* h)
        debug_printf(ALL, "delete_current_image");
 #endif
 
-       response* resp = NULL;
-
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_delete_image(h->sessionid, h->current_scan_status->current_image_number, resp);
+
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
+       h->current_scan_status->available_images--;
+
        kds_s2000w_client_response_free(resp);
        resp = NULL;
-       h->current_scan_status->available_images--;
 }
 
 void _download_current_image(handler* h)
@@ -99,22 +91,22 @@ void _download_current_image(handler* h)
        debug_printf(ALL, "download_current_image");
 #endif
 
-       response* resp = NULL;
-
        if (h->scanner_image->size > 0) {
                free(h->scanner_image->data);
                h->scanner_image->data = NULL;
                h->scanner_image->size = 0;
        }
 
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_get_image(h->sessionid, h->current_scan_status->current_image_number, resp);
+
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
        h->scanner_image->size = resp->size;
        h->scanner_image->data = malloc(sizeof(char) * resp->size);
        memcpy(h->scanner_image->data, resp->data, resp->size);
+
        kds_s2000w_client_response_free(resp);
        resp = NULL;
        h->current_scan_status->downloaded_images++;
@@ -126,15 +118,10 @@ void _get_current_scan_status(handler* h)
        debug_printf(ALL, "get_current_scan_status");
 #endif
 
-       json_object* status_resp_obj = NULL;
-       json_object* status_obj = NULL;
-       json_object* status_value_obj = NULL;
-       response* resp = NULL;
-
        if (h->current_scan_status->available_images > 1)
                return;
 
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_status_session(h->sessionid, resp);
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
@@ -143,11 +130,12 @@ void _get_current_scan_status(handler* h)
        debug_printf(DEBUG, resp->data);
 #endif
 
-       status_resp_obj = json_tokener_parse(resp->data);
-       status_obj = json_object_object_get(status_resp_obj, "Status");
-       status_value_obj = json_object_object_get(status_obj, "NumImagesStored");
+       json_object* status_resp_obj = json_tokener_parse(resp->data);
+       json_object* status_obj = json_object_object_get(status_resp_obj, "Status");
+       json_object* status_value_obj = json_object_object_get(status_obj, "NumImagesStored");
        h->current_scan_status->available_images = json_object_get_int(status_value_obj);
        status_value_obj = NULL;
+
        status_value_obj = json_object_object_get(status_obj, "State");
        const char* scanner_state = json_object_get_string(status_value_obj);
        if (strcmp(scanner_state, "Scanning") != 0)
@@ -261,41 +249,44 @@ void reset_handler(handler* h)
 void kds_s2000w_handler_recreate_session(handler* h)
 {
        kds_s2000w_client_close_session(h->sessionid);
-       response* resp = NULL;
-       json_object* sessionJson = NULL;
-       json_object* value_object = NULL;
 
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_open_session(resp);
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
-       if (resp->code == 200) {
-               sessionJson = json_tokener_parse(resp->data);
-               json_object_object_get_ex(sessionJson, "SessionId", &value_object);
-               h->sessionid = json_object_get_int64(value_object);
-               h->state = OPENED;
-               value_object = NULL;
-               json_object_put(sessionJson);
-               sessionJson = NULL;
+       if (resp->code != 200) {
+               kds_s2000w_client_response_free(resp);
+               resp = NULL;
+
+               return;
+       }
+
+       json_object* value_object = NULL;
+       json_object* sessionJson = json_tokener_parse(resp->data);
+       json_object_object_get_ex(sessionJson, "SessionId", &value_object);
+       h->sessionid = json_object_get_int64(value_object);
+       h->state = OPENED;
+       value_object = NULL;
+       json_object_put(sessionJson);
+       sessionJson = NULL;
 
 #ifndef NODEBUG
-               debug_printf_long(INFO, "SessionId", h->sessionid);
+       debug_printf_long(INFO, "SessionId", h->sessionid);
 #endif
 
-               // set old config parameters
-               kds_s2000w_client_response_free(resp);
-               resp = NULL;
-               const char* json_string = json_object_to_json_string_ext(h->current_scanner_config, JSON_C_TO_STRING_PLAIN);
-               resp = kds_s2000w_client_response_init();
-               resp->size = sizeof(char) * strlen(json_string);
-               resp->code = 0;
-               resp->data = realloc(resp->data, resp->size);
-               resp->data = memcpy(resp->data, json_string, resp->size);
-               kds_s2000w_client_set_option(h->sessionid, resp);
-               if (resp->error_size > 0)
-                       fprintf(stderr, "%s\n", resp->error_status);
-       }
+       // set old config parameters
+       kds_s2000w_client_response_free(resp);
+       resp = NULL;
+       const char* json_string = json_object_to_json_string_ext(h->current_scanner_config, JSON_C_TO_STRING_PLAIN);
+       resp = kds_s2000w_client_response_init();
+       resp->size = sizeof(char) * strlen(json_string);
+       resp->code = 0;
+       resp->data = realloc(resp->data, resp->size);
+       resp->data = memcpy(resp->data, json_string, resp->size);
+       kds_s2000w_client_set_option(h->sessionid, resp);
+       if (resp->error_size > 0)
+               fprintf(stderr, "%s\n", resp->error_status);
 
        kds_s2000w_client_response_free(resp);
        resp = NULL;
@@ -310,19 +301,12 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
        if (strcmp(devicename, "kds_s2000w_net") != 0)
                return;
 
-       handler* h = NULL;
-       response* resp = NULL;
-       json_object* value_object = NULL;
-       h = init_handler();
+       handler* h = init_handler();
        *handle = h;
 
-       h->scanner_image->size = 0;
-       h->scanner_image->data = NULL;
-       h->pnm_image->size = 0;
-       h->pnm_image->data = NULL;
-
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        int result = kds_s2000w_client_open_session(resp);
+
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
@@ -345,16 +329,27 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
        if (resp->code == 423)
                h->state = BUSY;
 
-       if (resp->code == 200) {
-               h->current_scanner_config = json_tokener_parse(resp->data);
-               json_object_object_get_ex(h->current_scanner_config, "SessionId", &value_object);
-               h->sessionid = json_object_get_int64(value_object);
-               h->state = OPENED;
+       if (resp->code != 200) {
+               kds_s2000w_client_response_free(resp);
+               resp = NULL;
+
+               return;
+       }
+
+       h->scanner_image->size = 0;
+       h->scanner_image->data = NULL;
+       h->pnm_image->size = 0;
+       h->pnm_image->data = NULL;
+
+       h->current_scanner_config = json_tokener_parse(resp->data);
+       json_object* value_object = NULL;
+       json_object_object_get_ex(h->current_scanner_config, "SessionId", &value_object);
+       h->sessionid = json_object_get_int64(value_object);
+       h->state = OPENED;
 #ifndef NODEBUG
-               debug_printf_long(INFO, "SessionId", h->sessionid);
+       debug_printf_long(INFO, "SessionId", h->sessionid);
 #endif
-               value_object = NULL;
-       }
+       value_object = NULL;
 
        json_object_put(h->current_scanner_config);
        h->current_scanner_config = NULL;
@@ -386,10 +381,9 @@ void kds_s2000w_handler_start_scan(handler* h)
        debug_printf(ALL, "kds_s2000w_handler_start_scan");
 #endif
 
-       response* resp = NULL;
-
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_start_scan(h->sessionid, resp);
+
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
@@ -403,9 +397,7 @@ void kds_s2000w_handler_stop_scan(handler* h)
        debug_printf(ALL, "kds_s2000w_handler_stop_scan");
 #endif
 
-       response* resp = NULL;
-
-       resp = kds_s2000w_client_response_init();
+       response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_stop_scan(h->sessionid, resp);
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);