X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=bcd8197cfdecbdf474267dedb232941e9339db11;hb=37a06df577bbfd00f4b361f92cacb0d97036ba93;hp=6f6d68b3fa0831a50a2419ff83c0dfc0ae7023f8;hpb=ee749a764df3dfa944c1f9de740ccbeb1cfdef40;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 6f6d68b3..bcd8197c 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -52,6 +52,12 @@ void printf_line(char *line_string, int line_length) fprintf_line(stdout, line_string, line_length); } +void printf_line_with_prefix(char *prefix, char *line_string, int line_length) +{ + fprintf(stdout, "%s", prefix); + fprintf_line(stdout, line_string, line_length); +} + /* int2str() returns a number converted to a string; the used memory is static, but will be overwritten by later calls, @@ -925,6 +931,25 @@ inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2) *y2 = help_y; } +/* the "put" variants of the following file access functions check for the file + pointer being != NULL and return the number of bytes they have or would have + written; this allows for chunk writing functions to first determine the size + of the (not yet written) chunk, write the correct chunk size and finally + write the chunk itself */ + +int getFile8BitInteger(FILE *file) +{ + return fgetc(file); +} + +int putFile8BitInteger(FILE *file, int value) +{ + if (file != NULL) + fputc(value, file); + + return 1; +} + int getFile16BitInteger(FILE *file, int byte_order) { if (byte_order == BYTE_ORDER_BIG_ENDIAN) @@ -935,18 +960,23 @@ int getFile16BitInteger(FILE *file, int byte_order) (fgetc(file) << 8)); } -void putFile16BitInteger(FILE *file, int value, int byte_order) +int putFile16BitInteger(FILE *file, int value, int byte_order) { - if (byte_order == BYTE_ORDER_BIG_ENDIAN) - { - fputc((value >> 8) & 0xff, file); - fputc((value >> 0) & 0xff, file); - } - else /* BYTE_ORDER_LITTLE_ENDIAN */ + if (file != NULL) { - fputc((value >> 0) & 0xff, file); - fputc((value >> 8) & 0xff, file); + if (byte_order == BYTE_ORDER_BIG_ENDIAN) + { + fputc((value >> 8) & 0xff, file); + fputc((value >> 0) & 0xff, file); + } + else /* BYTE_ORDER_LITTLE_ENDIAN */ + { + fputc((value >> 0) & 0xff, file); + fputc((value >> 8) & 0xff, file); + } } + + return 2; } int getFile32BitInteger(FILE *file, int byte_order) @@ -963,22 +993,27 @@ int getFile32BitInteger(FILE *file, int byte_order) (fgetc(file) << 24)); } -void putFile32BitInteger(FILE *file, int value, int byte_order) +int putFile32BitInteger(FILE *file, int value, int byte_order) { - if (byte_order == BYTE_ORDER_BIG_ENDIAN) - { - fputc((value >> 24) & 0xff, file); - fputc((value >> 16) & 0xff, file); - fputc((value >> 8) & 0xff, file); - fputc((value >> 0) & 0xff, file); - } - else /* BYTE_ORDER_LITTLE_ENDIAN */ + if (file != NULL) { - fputc((value >> 0) & 0xff, file); - fputc((value >> 8) & 0xff, file); - fputc((value >> 16) & 0xff, file); - fputc((value >> 24) & 0xff, file); + if (byte_order == BYTE_ORDER_BIG_ENDIAN) + { + fputc((value >> 24) & 0xff, file); + fputc((value >> 16) & 0xff, file); + fputc((value >> 8) & 0xff, file); + fputc((value >> 0) & 0xff, file); + } + else /* BYTE_ORDER_LITTLE_ENDIAN */ + { + fputc((value >> 0) & 0xff, file); + fputc((value >> 8) & 0xff, file); + fputc((value >> 16) & 0xff, file); + fputc((value >> 24) & 0xff, file); + } } + + return 4; } boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size, @@ -1035,6 +1070,22 @@ void putFileVersion(FILE *file, int version) fputc(version_build, file); } +void ReadBytesFromFile(FILE *file, byte *buffer, unsigned long bytes) +{ + int i; + + for(i = 0; i < bytes && !feof(file); i++) + buffer[i] = fgetc(file); +} + +void WriteBytesToFile(FILE *file, byte *buffer, unsigned long bytes) +{ + int i; + + for(i = 0; i < bytes; i++) + fputc(buffer[i], file); +} + void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes) { while (bytes-- && !feof(file)) @@ -1693,7 +1744,7 @@ static boolean string_has_parameter(char *s, char *s_contained) return string_has_parameter(substring, s_contained); } -int get_parameter_value(char *suffix, char *value_raw, int type) +int get_parameter_value(char *value_raw, char *suffix, int type) { char *value = getStringToLower(value_raw); int result = 0; /* probably a save default value */ @@ -1703,18 +1754,20 @@ int get_parameter_value(char *suffix, char *value_raw, int type) result = (strcmp(value, "left") == 0 ? MV_LEFT : strcmp(value, "right") == 0 ? MV_RIGHT : strcmp(value, "up") == 0 ? MV_UP : - strcmp(value, "down") == 0 ? MV_DOWN : MV_NO_MOVING); + strcmp(value, "down") == 0 ? MV_DOWN : MV_NONE); } else if (strcmp(suffix, ".anim_mode") == 0) { - result = (string_has_parameter(value, "none") ? ANIM_NONE : - string_has_parameter(value, "loop") ? ANIM_LOOP : - string_has_parameter(value, "linear") ? ANIM_LINEAR : - string_has_parameter(value, "pingpong") ? ANIM_PINGPONG : - string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 : - string_has_parameter(value, "random") ? ANIM_RANDOM : - string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL : - string_has_parameter(value, "vertical") ? ANIM_VERTICAL : + result = (string_has_parameter(value, "none") ? ANIM_NONE : + string_has_parameter(value, "loop") ? ANIM_LOOP : + string_has_parameter(value, "linear") ? ANIM_LINEAR : + string_has_parameter(value, "pingpong") ? ANIM_PINGPONG : + string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 : + string_has_parameter(value, "random") ? ANIM_RANDOM : + string_has_parameter(value, "ce_value") ? ANIM_CE_VALUE : + string_has_parameter(value, "ce_score") ? ANIM_CE_SCORE : + string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL : + string_has_parameter(value, "vertical") ? ANIM_VERTICAL : ANIM_DEFAULT); if (string_has_parameter(value, "reverse")) @@ -1744,7 +1797,7 @@ int get_auto_parameter_value(char *token, char *value_raw) if (suffix == NULL) suffix = token; - return get_parameter_value(suffix, value_raw, TYPE_INTEGER); + return get_parameter_value(value_raw, suffix, TYPE_INTEGER); } static void FreeCustomArtworkList(struct ArtworkListInfo *,