comparison sidlib.c @ 265:05bbe428a0ab

Unify SIDLib error messages.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 06 Jan 2020 00:05:16 +0200
parents 5afa6052f796
children 254828e9213b
comparison
equal deleted inserted replaced
264:5afa6052f796 265:05bbe428a0ab
112 !thfread_be16(ctx, &psid->nSongs) || 112 !thfread_be16(ctx, &psid->nSongs) ||
113 !thfread_be16(ctx, &psid->startSong) || 113 !thfread_be16(ctx, &psid->startSong) ||
114 !thfread_be32(ctx, &psid->speed)) 114 !thfread_be32(ctx, &psid->speed))
115 { 115 {
116 ret = th_io_error(ctx, ctx->status, 116 ret = th_io_error(ctx, ctx->status,
117 "Could not read PSID/RSID header from '%s': %s.", 117 "Could not read PSID/RSID header.");
118 ctx->filename, th_error_str(ctx->status));
119 goto exit; 118 goto exit;
120 } 119 }
121 120
122 psid->magic[SIDLIB_PSID_MAGIC_LEN] = 0; 121 psid->magic[SIDLIB_PSID_MAGIC_LEN] = 0;
123 122
124 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') || 123 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') ||
125 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' || 124 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' ||
126 psid->version < 1 || psid->version > 4) 125 psid->version < 1 || psid->version > 4)
127 { 126 {
128 ret = th_io_error(ctx, THERR_NOT_SUPPORTED, 127 ret = th_io_error(ctx, THERR_NOT_SUPPORTED,
129 "Not a supported PSID or RSID file: '%s'", 128 "Not a supported PSID or RSID file.");
130 ctx->filename);
131 goto exit; 129 goto exit;
132 } 130 }
133 131
134 psid->isRSID = psid->magic[0] == 'R'; 132 psid->isRSID = psid->magic[0] == 'R';
135 133
136 if (!sidlib_fread_str(ctx, &psid->sidName, SIDLIB_PSID_STR_LEN) || 134 if (!sidlib_fread_str(ctx, &psid->sidName, SIDLIB_PSID_STR_LEN) ||
137 !sidlib_fread_str(ctx, &psid->sidAuthor, SIDLIB_PSID_STR_LEN) || 135 !sidlib_fread_str(ctx, &psid->sidAuthor, SIDLIB_PSID_STR_LEN) ||
138 !sidlib_fread_str(ctx, &psid->sidCopyright, SIDLIB_PSID_STR_LEN)) 136 !sidlib_fread_str(ctx, &psid->sidCopyright, SIDLIB_PSID_STR_LEN))
139 { 137 {
140 ret = th_io_error(ctx, ctx->status, 138 ret = th_io_error(ctx, ctx->status,
141 "Error reading SID file header from '%s': %s.", 139 "Error reading SID file header data.");
142 ctx->filename, th_error_str(ctx->status));
143 goto exit; 140 goto exit;
144 } 141 }
145 142
146 // Check if we need to load PSIDv2NG header ... 143 // Check if we need to load PSIDv2NG header ...
147 if (psid->version >= 2) 144 if (psid->version >= 2)
152 !thfread_u8(ctx, &psid->pageLength) || 149 !thfread_u8(ctx, &psid->pageLength) ||
153 !thfread_u8(ctx, &psid->sid2Addr) || 150 !thfread_u8(ctx, &psid->sid2Addr) ||
154 !thfread_u8(ctx, &psid->sid3Addr)) 151 !thfread_u8(ctx, &psid->sid3Addr))
155 { 152 {
156 ret = th_io_error(ctx, ctx->status, 153 ret = th_io_error(ctx, ctx->status,
157 "Error reading PSID/RSID v2+ extra header data from '%s': %s.", 154 "Error reading PSID/RSID v2+ extra header data.");
158 ctx->filename, th_error_str(ctx->status));
159 goto exit; 155 goto exit;
160 } 156 }
161 } 157 }
162 158
163 hdrEnd = thftell(ctx); 159 hdrEnd = thftell(ctx);
341 337
342 // Allocate new node 338 // Allocate new node
343 if ((*pnode = node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode))) == NULL) 339 if ((*pnode = node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode))) == NULL)
344 { 340 {
345 ret = th_io_error(ctx, THERR_MALLOC, 341 ret = th_io_error(ctx, THERR_MALLOC,
346 "Error allocating new SLDB node for SongLengthDB file '%s' line #%d:\n%s", 342 "Error allocating new SLDB node.");
347 ctx->filename, ctx->line, line);
348 goto exit; 343 goto exit;
349 } 344 }
350 345
351 // Get hash value 346 // Get hash value
352 for (pos = 0, i = 0; i < TH_MD5HASH_LENGTH; i++, pos += 2) 347 for (pos = 0, i = 0; i < TH_MD5HASH_LENGTH; i++, pos += 2)
359 // Get playtimes 354 // Get playtimes
360 th_findnext(line, &pos); 355 th_findnext(line, &pos);
361 if (line[pos] != '=') 356 if (line[pos] != '=')
362 { 357 {
363 ret = th_io_error(ctx, THERR_INVALID_DATA, 358 ret = th_io_error(ctx, THERR_INVALID_DATA,
364 "'=' expected on column %d of SongLengthDB file '%s' line #%d:\n%s", 359 "'=' expected on column %d.",
365 pos, ctx->filename, ctx->line, line); 360 pos);
366 goto exit; 361 goto exit;
367 } 362 }
368 363
369 // First playtime is after '=' 364 // First playtime is after '='
370 savePos = ++pos; 365 savePos = ++pos;
390 } 385 }
391 386
392 if ((node->lengths = (int *) th_calloc(node->nlengths, sizeof(int))) == NULL) 387 if ((node->lengths = (int *) th_calloc(node->nlengths, sizeof(int))) == NULL)
393 { 388 {
394 ret = th_io_error(ctx, THERR_MALLOC, 389 ret = th_io_error(ctx, THERR_MALLOC,
395 "Error allocating SLDB length data for SongLengthDB file '%s' line #%d:\n%s", 390 "Error allocating SLDB entry length data.");
396 ctx->filename, ctx->line, line);
397 goto exit; 391 goto exit;
398 } 392 }
399 393
400 // Read lengths in 394 // Read lengths in
401 for (i = 0, pos = savePos, isOK = TRUE; 395 for (i = 0, pos = savePos, isOK = TRUE;
461 for (hashLen = 0; line[pos] && th_isxdigit(line[pos]); hashLen++, pos++); 455 for (hashLen = 0; line[pos] && th_isxdigit(line[pos]); hashLen++, pos++);
462 456
463 if (hashLen != TH_MD5HASH_LENGTH_CH) 457 if (hashLen != TH_MD5HASH_LENGTH_CH)
464 { 458 {
465 ret = th_io_error(ctx, THERR_INVALID_DATA, 459 ret = th_io_error(ctx, THERR_INVALID_DATA,
466 "Invalid MD5-hash in SongLengthDB file '%s' line #%d:\n%s", 460 "Invalid MD5-hash.");
467 ctx->filename, ctx->line, line);
468 goto exit; 461 goto exit;
469 } 462 }
470 else 463 else
471 { 464 {
472 // Parse and add node to db 465 // Parse and add node to db
478 } 471 }
479 else 472 else
480 if (line[pos] != ';' && line[pos] != '[' && line[pos] != 0) 473 if (line[pos] != ';' && line[pos] != '[' && line[pos] != 0)
481 { 474 {
482 ret = th_io_error(ctx, THERR_INVALID_DATA, 475 ret = th_io_error(ctx, THERR_INVALID_DATA,
483 "Invalid line in SongLengthDB file '%s' line #%d:\n%s", 476 "Invalid line / unexpected data at column #%d.",
484 ctx->filename, ctx->line, line); 477 pos);
485 goto exit; 478 goto exit;
486 } 479 }
487 } 480 }
488 481
489 exit: 482 exit:
892 } 885 }
893 else 886 else
894 { 887 {
895 // Error! Invalid character found 888 // Error! Invalid character found
896 ret = th_io_error(fh, THERR_INVALID_DATA, 889 ret = th_io_error(fh, THERR_INVALID_DATA,
897 "Unexpected character '%c' on line #%d.", 890 "Unexpected character '%c'.",
898 ctx.ch, fh->line); 891 ctx.ch);
899 goto out; 892 goto out;
900 } 893 }
901 } 894 }
902 else 895 else
903 { 896 {
926 { 919 {
927 VADDCH(ctx.ch) 920 VADDCH(ctx.ch)
928 else 921 else
929 { 922 {
930 ret = th_io_error(fh, THERR_INVALID_DATA, 923 ret = th_io_error(fh, THERR_INVALID_DATA,
931 "Entry filename too long on line #%d.", 924 "STIL entry filename too long.");
932 fh->line);
933 goto out; 925 goto out;
934 } 926 }
935 ctx.ch = -1; 927 ctx.ch = -1;
936 } 928 }
937 break; 929 break;
945 tmpStr[strPos] = 0; 937 tmpStr[strPos] = 0;
946 938
947 if (!th_get_int(tmpStr, (unsigned int *) &subtune, &neg) || neg) 939 if (!th_get_int(tmpStr, (unsigned int *) &subtune, &neg) || neg)
948 { 940 {
949 ret = th_io_error(fh, THERR_INVALID_DATA, 941 ret = th_io_error(fh, THERR_INVALID_DATA,
950 "Entry '%s' subtune indicator not a valid integer on line #%d: '%s'", 942 "Entry '%s' subtune indicator '%s' is not a valid integer.",
951 entry->filename, fh->line, tmpStr); 943 entry->filename, tmpStr);
952 goto out; 944 goto out;
953 } 945 }
954 946
955 if (subtune <= 0 || subtune > 128) 947 if (subtune <= 0 || subtune > 128)
956 { 948 {
957 th_io_error(fh, THERR_INVALID_DATA, 949 th_io_error(fh, THERR_INVALID_DATA,
958 "Entry '%s' subtune number %d is invalid on line #%d.", 950 "Entry '%s' subtune number #%d is out of range/invalid.",
959 entry->filename, subtune, fh->line); 951 entry->filename, subtune);
960 952
961 subtune = -1; 953 subtune = -1;
962 } 954 }
963 955
964 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); 956 sidlib_stildb_set_parsemode(&ctx, PM_IDLE);
971 963
972 VADDCH(ctx.ch) 964 VADDCH(ctx.ch)
973 else 965 else
974 { 966 {
975 ret = th_io_error(fh, THERR_INVALID_DATA, 967 ret = th_io_error(fh, THERR_INVALID_DATA,
976 "Subtune indicator too long on line #%d.", 968 "Subtune indicator too long.");
977 fh->line);
978 goto out; 969 goto out;
979 } 970 }
980 } 971 }
981 else 972 else
982 if ((ctx.ch == '#' && strPos > 0) || (ctx.ch != '#' && !th_isspace(ctx.ch))) 973 if ((ctx.ch == '#' && strPos > 0) || (ctx.ch != '#' && !th_isspace(ctx.ch)))
983 { 974 {
984 sub_unexpected: 975 sub_unexpected:
985 ret = th_io_error(fh, THERR_INVALID_DATA, 976 ret = th_io_error(fh, THERR_INVALID_DATA,
986 "Unexpected character '%c' in subtune indicator on line #%d.", 977 "Unexpected character '%c' in subtune indicator.",
987 ctx.ch, fh->line); 978 ctx.ch);
988 goto out; 979 goto out;
989 } 980 }
990 981
991 ctx.ch = -1; 982 ctx.ch = -1;
992 ctx.lineStart = FALSE; 983 ctx.lineStart = FALSE;
1001 field = sidlib_stildb_get_field(tmpStr); 992 field = sidlib_stildb_get_field(tmpStr);
1002 993
1003 if (entry == NULL) 994 if (entry == NULL)
1004 { 995 {
1005 ret = th_io_error(fh, THERR_INVALID_DATA, 996 ret = th_io_error(fh, THERR_INVALID_DATA,
1006 "No STIL entry allocated, but field '%s' found on line #%d.", 997 "No STIL entry allocated, but field '%s' found.",
1007 fieldName, fh->line); 998 fieldName);
1008 goto out; 999 goto out;
1009 } 1000 }
1010 1001
1011 sidlib_stildb_set_next_parsemode(&ctx, PM_FIELD_DATA); 1002 sidlib_stildb_set_next_parsemode(&ctx, PM_FIELD_DATA);
1012 ctx.lineStart = FALSE; 1003 ctx.lineStart = FALSE;
1016 { 1007 {
1017 VADDCH(ctx.ch) 1008 VADDCH(ctx.ch)
1018 else 1009 else
1019 { 1010 {
1020 ret = th_io_error(fh, THERR_INVALID_DATA, 1011 ret = th_io_error(fh, THERR_INVALID_DATA,
1021 "Field name too long on line #%d.", 1012 "Field name too long.");
1022 fieldName, fh->line);
1023 goto out; 1013 goto out;
1024 } 1014 }
1025 } 1015 }
1026 1016
1027 ctx.ch = -1; 1017 ctx.ch = -1;
1049 // Supported field, add it 1039 // Supported field, add it
1050 if ((ret = sidlib_stildb_entry_realloc( 1040 if ((ret = sidlib_stildb_entry_realloc(
1051 entry, subtune, TRUE, field, tmpStr)) != THERR_OK) 1041 entry, subtune, TRUE, field, tmpStr)) != THERR_OK)
1052 { 1042 {
1053 ret = th_io_error(fh, THERR_MALLOC, 1043 ret = th_io_error(fh, THERR_MALLOC,
1054 "Could not allocate memory for field '%s' on line #%d.", 1044 "Could not allocate memory for field '%s'.",
1055 fieldName, fh->line); 1045 fieldName);
1056 goto out; 1046 goto out;
1057 } 1047 }
1058 } 1048 }
1059 1049
1060 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); 1050 sidlib_stildb_set_parsemode(&ctx, PM_IDLE);
1064 ctx.lineStart = FALSE; 1054 ctx.lineStart = FALSE;
1065 VADDCH(ctx.ch) 1055 VADDCH(ctx.ch)
1066 else 1056 else
1067 { 1057 {
1068 ret = th_io_error(fh, THERR_INVALID_DATA, 1058 ret = th_io_error(fh, THERR_INVALID_DATA,
1069 "Field '%s' data too long on line #%d.", 1059 "Field '%s' data too long.",
1070 fieldName, fh->line); 1060 fieldName);
1071 goto out; 1061 goto out;
1072 } 1062 }
1073 ctx.ch = - 1; 1063 ctx.ch = - 1;
1074 } 1064 }
1075 break; 1065 break;