/* $Id: text.c,v 1.5 2006/09/15 13:34:06 toad32767 Exp $ */ /** ** 2005, 2006 by Marco Trillo ** This file is part of UModPlayer, and is released by ** its autors to the Public Domain. ** In case it's not legally possible, its autors grant ** anyone the right to use, redistribute and modify ** this software for any purpose, without any conditions, ** unless such conditions are required by law. ** ** THIS FILE COMES WITHOUT ANY WARRANTY. THE AUTHORS ** SHALL NOT BE LIABLE FOR ANY DAMAGE RESULTING BY THE ** USE OR MISUSE OF THIS SOFTWARE. **/ /* * ===================== * TEXT-RELATED STUFF * ===================== */ #include #include #include EXPORT char * strcopyof(char *str) { char *ptr; size_t len; len = (size_t) strlen(str) + 1; ptr = malloc(len); if (ptr == NULL) return NULL; memcpy(ptr, str, len); /* includes NUL */ return ptr; } EXPORT char * ReadStringFromFile(FILE * fp) { char ret[128]; char *ptr = (char *) ret; int c, i = 0; c = getc(fp); while (c != EOF && c != '\n' && (i++) < 127) { *ptr++ = (char) c; c = getc(fp); } *ptr = '\0'; return strcopyof(ret); } EXPORT char * ReadString() { return ReadStringFromFile(stdin); } EXPORT void TrimString(char *string) { /* Remove final blank spaces on strings */ int l; l = strlen(string) - 1; for (;;) { if (l < 0) break; if (string[l] == ' ' || string[l] == '\t') string[l] = '\0'; else break; l--; } return; } EXPORT int CalcLen(char **array, int size) { int i, a, L = 0; for (i = 0; i < size; ++i) { a = strlen(array[i]); if (a > L) { L = a; } } return L; } /* sound options ordered by table position */ LOCAL int sndopts[4] = {MODPLUG_ENABLE_NOISE_REDUCTION, MODPLUG_ENABLE_REVERB, MODPLUG_ENABLE_MEGABASS, MODPLUG_ENABLE_SURROUND}; EXPORT void MyTextCallback(int id, int row, int col, char *buf) { if (id == 0) { if (col == 0) strcpy(buf, rr[row]); else { if (row == 0) sprintf(buf, "%.3f kHz", ((double) sets.samplerate) / 1000.0); else if (row == 1) { switch (sets.resampling) { case 0: strcpy(buf, "No Interpolation"); break; case 1: strcpy(buf, "Linear"); break; case 2: strcpy(buf, "Spline"); break; case 3: strcpy(buf, "Fir Filter"); break; } } else if (row == 6) { if (sets.channels == 1) strcpy(buf, "Mono"); else if (sets.channels == 2) strcpy(buf, "Stereo"); else strcpy(buf, "Invalid"); } else if (row == 7) { sprintf(buf, "%u", sets.vol); } else { /* * sndopts[] is ordered in the same * order as the option apparitions * in the table. */ if (sets.flags & sndopts[row - 2]) strcpy(buf, "YES"); else strcpy(buf, "NO"); } } return; } else if (id == 1) { if (col == 1) strcpy(buf, rr[row]); else { switch (row) { case 0: strcpy(buf, "Length:"); break; case 1: strcpy(buf, "Format:"); break; case 2: strcpy(buf, "Instruments:"); break; case 3: strcpy(buf, "Samples:"); break; case 4: strcpy(buf, "Channels:"); break; case 5: strcpy(buf, "Patterns:"); break; } } return; } else if (id == 2) { if (col == 0) { switch (row) { case 0: strcpy(buf, "Reverb Depth"); break; case 1: strcpy(buf, "Reverb Delay"); break; case 2: strcpy(buf, "Bass Amount"); break; case 3: strcpy(buf, "Bass Range"); break; case 4: strcpy(buf, "Surround Depth"); break; case 5: strcpy(buf, "Surround Delay"); break; case 6: strcpy(buf, "Endianness"); break; case 7: strcpy(buf, "Appareance"); break; case 8: strcpy(buf, "Verbosity"); break; } } else strcpy(buf, rr[row]); return; } else if (id == 3) { strcpy(buf, rr[row]); return; } return; } EXPORT int TotalLines(const char *text, int length, int maxchars) { int i, lines = 0, j = 0; for (i = 0; i < length; ++i) { ++j; if (text[i] == '\r' || text[i] == '\n' || j == maxchars) { ++lines; j = 0; } } return lines; } EXPORT void PaginateText(const char *text, int length) { unsigned int i, j, k, line, tlines; unsigned short klines, kcolumns; /* FIXME: getting lines & columns from system instead of using the * default (80x24) */ klines = 24; kcolumns = 80; klines--; tlines = TotalLines(text, length, kcolumns); j = 0; k = 0; line = 0; for (i = 0; i < length; ++i) { if (j == klines) { notice(MESSAGE_PAGINATING, ((line * 100) / tlines)); fflush(stdout); (void) getchar(); j = 0; } if (text[i] != '\r' && text[i] != '\n') putchar(text[i]); /* Process newlines */ if (text[i] == '\n' || text[i] == '\r' || k == kcolumns) { putchar('\n'); ++j; ++line; k = 0; } ++k; } return; } EXPORT void DisplaySongComments() { char *comments; comments = ModPlug_GetMessage(file.mod); if (comments) { PaginateText(comments, strlen(comments)); putchar('\n'); } else { puts(MESSAGE_NO_MESSAGE); } return; } EXPORT void SaveSongComments(char *fname) { FILE *fp; int i, length; char *comments; comments = ModPlug_GetMessage(file.mod); notice("Exporting song text message to '%s'\n", fname); if (comments) { fp = fopen(fname, "w+"); if (fp == NULL) { puts(MESSAGE_OPEN_ERROR); free(fname); return; } length = strlen(comments); for (i = 0; i < length; ++i) { if (comments[i] == '\r') { putc('\n', fp); } else { putc((int) comments[i], fp); } } fclose(fp); notice("%d bytes written\n", i); } else { puts(MESSAGE_NO_MESSAGE); } return; } EXPORT void DisplayInstruments(int saveToFile) { unsigned int instrus, i, bytes_in, length = 0; char *nbuf; instrus = ModPlug_NumInstruments(file.mod); if (instrus < 1) { puts(MESSAGE_NO_INSTRUMENTS); return; } nbuf = (char *) malloc(47); if (nbuf == NULL) return; for (i = 1; i <= instrus; ++i) { sprintf(nbuf + length, "%3d. ", i); bytes_in = ModPlug_InstrumentName(file.mod, i, nbuf + length + 6); length += bytes_in + 6; if (bytes_in > 40) { free(nbuf); return; } nbuf[length] = '\n'; ++length; nbuf = (char *) realloc(nbuf, length + 47); if (nbuf == NULL) return; } nbuf = (char *) realloc(nbuf, length); if (nbuf == NULL) return; if (saveToFile == 0) PaginateText(nbuf, length); else { notice("Exporting instrument names...\n"); if (write(saveToFile, nbuf, length) < length) error("write(2) failed!\n"); else notice("%u bytes written OK...\n", length); } free(nbuf); return; } EXPORT void DisplaySamples(int saveToFile) { unsigned int ssamples, i, bytes_in, length = 0; char *nbuf; ssamples = ModPlug_NumSamples(file.mod); if (ssamples < 1) { puts(MESSAGE_NO_SAMPLES); return; } nbuf = (char *) malloc(47); if (nbuf == NULL) return; for (i = 1; i <= ssamples; ++i) { sprintf(nbuf + length, "%3d. ", i); bytes_in = ModPlug_SampleName(file.mod, i, nbuf + length + 6); length += bytes_in + 6; if (bytes_in > 40) { free(nbuf); return; } nbuf[length] = '\n'; ++length; nbuf = (char *) realloc(nbuf, length + 47); if (nbuf == NULL) return; } nbuf = (char *) realloc(nbuf, length); if (nbuf == NULL) return; if (saveToFile == 0) PaginateText(nbuf, length); else { notice("Exporting sample names...\n"); if (write(saveToFile, nbuf, length) < length) error("write(2) failed\n"); else notice("%u bytes written OK...\n", length); } free(nbuf); return; } EXPORT void GetRangeValues(char *string, int *x, int *y) { unsigned int len, i; char *p1 = NULL; char *p2 = NULL; *x = 0; *y = 0; len = (unsigned int) strlen(string); if (!len) { return; } len--; p1 = string; for (i = len; i > 0; --i) { if (string[i] == '-') { p2 = &string[i + 1]; } } if (p1) *x = atoi(p1); if (p2) *y = atoi(p2); return; }