/* Hey EMACS -*- linux-c -*- */ /* $Id: misc.c 1130 2005-05-25 15:14:40Z roms $ */ /* libtifiles - Ti File Format library, a part of the TiLP project * Copyright (C) 1999-2004 Romain Lievin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* This unit contains some miscellaneous but useful functions. */ #include #include #include #include "stdints.h" #include //#include "config.h" #include "export.h" #include "file_int.h" #include "printl.h" #define bswap_16(a) (a >> 8) | (a << 8) #define bswap_32(x) (x >> 24) | (x & 0xff0000) >> 8 | (x & 0xff00) << 8 | (x & 0xff) << 24 /* Dump into hexadecimal format the content of a buffer - ptr [in]: a pointer on some data to dump - len [in]: the number of bytes to dump - [out]: always 0 */ TIEXPORT int TICALL hexdump(uint8_t * ptr, int len) { int i; for (i = 0; i < len; i++) printl3(0, "%02X ", ptr[i]); printl3(0, "\n"); return 0; } /**********************/ /* Read/Write strings */ /**********************/ /* Read a string of 'n' chars from a file - s [out]: a buffer for storing the string - f [in]: a file descriptor - [out]: the result of the operation (0 if failed) */ int fread_n_chars(FILE * f, int n, char *s) { int i; if (s == NULL) { for (i = 0; i < n; i++) fgetc(f); } else { for (i = 0; i < n; i++) s[i] = 0xff & fgetc(f); s[i] = '\0'; } return 0; } /* Write a string of 'n' chars (NULL padded) to a file - s [in]: a string - f [in]: a file descriptor - [out]: always different of 0 */ #define FOO int fwrite_n_chars(FILE * f, int n, const char *s) { #ifdef FOO int i; int l = n; l = strlen(s); if (l > n) { printl3(2, "string passed in 'write_string8' is too long (>n chars).\n"); printl3(2, "s = <%s>, len(s) = %i\n", s, strlen(s)); hexdump((uint8_t *) s, (strlen(s) < 9) ? 9 : strlen(s)); abort(); } for (i = 0; i < l; i++) fputc(s[i], f); for (i = l; i < n; i++) { fputc(0x00, f); } #else int i; for (i = 0; i < n; i++) fputc((int) s[i], f); #endif return 0; } int fread_8_chars(FILE * f, char *s) { return fread_n_chars(f, 8, s); } int fwrite_8_chars(FILE * f, const char *s) { return fwrite_n_chars(f, 8, s); } int fskip(FILE * f, int n) { /* int i; for(i=0; i no path) - varname [in]: the variable name - [out]: aalways 0. */ extern TicalcType tifiles_calc_type; int TICALL tifiles_build_fullname(char *full_name, const char *fldname, const char *varname) { if (tifiles_has_folder(tifiles_calc_type)) { if (strcmp(fldname, "")) { strcpy(full_name, fldname); strcat(full_name, "\\"); } strcat(full_name, varname); } else strcpy(full_name, varname); return 0; } int is_regfile(const char *filename) { #ifndef __WIN32__ struct stat buf; if (stat(filename, &buf) < 0) return 0; if (S_ISREG(buf.st_mode)) return !0; else return 0; #else return !0; #endif }