Добавлена обработка ошибок

This commit is contained in:
cfif 2025-09-11 10:12:02 +03:00
parent 8567a5bb2a
commit 0945cfd7a6
2 changed files with 25 additions and 18 deletions

View File

@ -41,22 +41,24 @@ int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: conv fileIn fileOut\n");
fflush(stdout);
return 1;
}
FILE *input_file = fopen(argv[1], "r");
if (input_file == NULL) {
printf("Error: Failed to open file %s\n", argv[1]);
fflush(stdout);
return 1;
}
#define BUFFER_SIZE 4096
#define BUFFER_SIZE 4096
char json_string[BUFFER_SIZE] = {0};
size_t bytes_read = fread(json_string, 1, BUFFER_SIZE, input_file);
printf("Read %zu bytes:\n", bytes_read);
printf("%.*s\n", (int)bytes_read, json_string);
printf("Read from file: \"%s\", %zu bytes:\n", argv[1], bytes_read);
printf("%.*s\n", (int) bytes_read, json_string);
fclose(input_file);
@ -72,6 +74,7 @@ int main(int argc, char *argv[]) {
if (json == NULL) {
printf("Error parsing JSON\n");
fflush(stdout);
return 1;
}
@ -108,6 +111,7 @@ int main(int argc, char *argv[]) {
}
} else {
printf("Parsing timestamp (UTC) error");
fflush(stdout);
return 1;
}
@ -117,11 +121,13 @@ int main(int argc, char *argv[]) {
} else {
printf("Parsing timestamp error");
fflush(stdout);
return 1;
}
} else {
printf("Parsing JSON timestamp error");
fflush(stdout);
return 1;
}
@ -181,10 +187,12 @@ int main(int argc, char *argv[]) {
} else {
printf("Parsing coordinate error (1)");
fflush(stdout);
return 1;
}
} else {
printf("Parsing JSON coordinate error");
fflush(stdout);
return 1;
}
@ -192,6 +200,7 @@ int main(int argc, char *argv[]) {
printf("phone (JSON): %s\n", phoneJS->valuestring);
} else {
printf("Parsing JSON phone error");
fflush(stdout);
return 1;
}
@ -201,7 +210,7 @@ int main(int argc, char *argv[]) {
char VIN[17] = {0};
memcpy(VIN, idJS->valuestring, 5);
memcpy(&VIN[5], "1111111111", 10);
// memcpy(&VIN[5], "1111111111", 10);
EraGlonassMsdSetVIN(&msd, VIN, sizeof(VIN));
@ -209,6 +218,7 @@ int main(int argc, char *argv[]) {
} else {
printf("Parsing JSON id error");
fflush(stdout);
return 1;
}
@ -225,18 +235,24 @@ int main(int argc, char *argv[]) {
FILE *output_file = fopen(argv[2], "w");
if (output_file == NULL) {
printf("Error: Failed to open file %s\n", argv[1]);
printf("Error: Failed to open file %s\n", argv[2]);
fflush(stdout);
return 1;
}
size_t bytes_written = fwrite(egtsHexStr, 1, egtsHexStrLen, output_file);
printf("Write %zu bytes:\n", bytes_written);
printf("%.*s\n", (int)bytes_written, egtsHexStr);
if (bytes_written != egtsHexStrLen) {
printf("Error: Failed to write file %s\n", argv[2]);
fflush(stdout);
return 1;
}
printf("Write to file: \"%s\" %zu bytes: %.*s\n", argv[2], bytes_written, (int) bytes_written, egtsHexStr);
fclose(output_file);
fflush(stdout);
return 0;

View File

@ -5,16 +5,7 @@ set(CMAKE_CXX_STANDARD 17)
include(modular.cmake)
#link_directories(/usr/local/opt/libpq/lib/)
#include_directories(/usr/local/opt/libpq/include)
#link_directories(/usr/lib/x86_64-linux-gnu/)
#include_directories(/usr/include/postgresql)
#link_libraries(pq)
#link_libraries(tcp)
#link_libraries(pthread)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -funsigned-char -pipe -Wl,-O0 -std=gnu++2a -Wall -Wextra")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -funsigned-char -pipe -Wl,-O0 -std=gnu++2a -Wall -Wextra")
add_executable(conv ${SOURCES})
target_link_libraries(conv PRIVATE m)