X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=examples%2Fhandle_errors.c;h=16b91d256d2938ebdbcb3359f787c776846d2ce5;hb=refs%2Fheads%2Fmaster;hp=da8e8e961ded2dd2c8490c7d9bbc74471ab08ad3;hpb=4720053160d21db8c4da8202a139be1935990e5e;p=libserialport.git diff --git a/examples/handle_errors.c b/examples/handle_errors.c index da8e8e9..16b91d2 100644 --- a/examples/handle_errors.c +++ b/examples/handle_errors.c @@ -31,6 +31,7 @@ int check(enum sp_return result) { int error_code; char *error_message; + switch (result) { /* Handle each of the four negative error codes that can be returned. @@ -38,61 +39,61 @@ int check(enum sp_return result) * In this example, we will end the program on any error, using * a different return code for each possible class of error. */ - case SP_ERR_ARG: - /* When SP_ERR_ARG is returned, there was a problem with one - * or more of the arguments passed to the function, e.g. a null - * pointer or an invalid value. This generally implies a bug in - * the calling code. */ - printf("Error: Invalid argument.\n"); - end_program(1); - - case SP_ERR_FAIL: - /* When SP_ERR_FAIL is returned, there was an error from the OS, - * which we can obtain the error code and message for. These - * calls must be made in the same thread as the call that - * returned SP_ERR_FAIL, and before any other system functions - * are called in that thread, or they may not return the - * correct results. */ - error_code = sp_last_error_code(); - error_message = sp_last_error_message(); - printf("Error: Failed: OS error code: %d, message: '%s'\n", - error_code, error_message); - /* The error message should be freed after use. */ - sp_free_error_message(error_message); - end_program(2); - - case SP_ERR_SUPP: - /* When SP_ERR_SUPP is returned, the function was asked to do - * something that isn't supported by the current OS or device, - * or that libserialport doesn't know how to do in the current - * version. */ - printf("Error: Not supported.\n"); - end_program(3); - - case SP_ERR_MEM: - /* When SP_ERR_MEM is returned, libserialport wasn't able to - * allocate some memory it needed. Since the library doesn't - * normally use any large data structures, this probably means - * the system is critically low on memory and recovery will - * require very careful handling. The library itself will - * always try to handle any allocation failure safely. - * - * In this example, we'll just try to exit gracefully without - * calling printf, which might need to allocate further memory. */ - end_program(4); - - case SP_OK: - default: - /* A return value of SP_OK, defined as zero, means that the - * operation succeeded. */ - printf("Operation succeeded.\n"); - - /* Some fuctions can also return a value greater than zero to - * indicate a numeric result, such as the number of bytes read by - * sp_blocking_read(). So when writing an error handling wrapper - * function like this one, it's helpful to return the result so - * that it can be used. */ - return result; + case SP_ERR_ARG: + /* When SP_ERR_ARG is returned, there was a problem with one + * or more of the arguments passed to the function, e.g. a null + * pointer or an invalid value. This generally implies a bug in + * the calling code. */ + printf("Error: Invalid argument.\n"); + end_program(1); + + case SP_ERR_FAIL: + /* When SP_ERR_FAIL is returned, there was an error from the OS, + * which we can obtain the error code and message for. These + * calls must be made in the same thread as the call that + * returned SP_ERR_FAIL, and before any other system functions + * are called in that thread, or they may not return the + * correct results. */ + error_code = sp_last_error_code(); + error_message = sp_last_error_message(); + printf("Error: Failed: OS error code: %d, message: '%s'\n", + error_code, error_message); + /* The error message should be freed after use. */ + sp_free_error_message(error_message); + end_program(2); + + case SP_ERR_SUPP: + /* When SP_ERR_SUPP is returned, the function was asked to do + * something that isn't supported by the current OS or device, + * or that libserialport doesn't know how to do in the current + * version. */ + printf("Error: Not supported.\n"); + end_program(3); + + case SP_ERR_MEM: + /* When SP_ERR_MEM is returned, libserialport wasn't able to + * allocate some memory it needed. Since the library doesn't + * normally use any large data structures, this probably means + * the system is critically low on memory and recovery will + * require very careful handling. The library itself will + * always try to handle any allocation failure safely. + * + * In this example, we'll just try to exit gracefully without + * calling printf, which might need to allocate further memory. */ + end_program(4); + + case SP_OK: + default: + /* A return value of SP_OK, defined as zero, means that the + * operation succeeded. */ + printf("Operation succeeded.\n"); + + /* Some fuctions can also return a value greater than zero to + * indicate a numeric result, such as the number of bytes read by + * sp_blocking_read(). So when writing an error handling wrapper + * function like this one, it's helpful to return the result so + * that it can be used. */ + return result; } }