📄 mes.c
字号:
mes_file_win (logfile); } if (line >= 0) mes_aux (MES_FLAG_FILE_WIN, ": line %d", line); if (logfile && strlen (txt)) { mes_file_win (" ("); mes_file_win (txt); mes_file_win (")\n"); } else mes_file_win ("\n");} /* ighmm_mes_fformat */#endif /* GHMM_UNSUPORTED *//*============================================================================*/void ighmm_mes_err (char *txt, int error_nr, char *proc_info){ ighmm_mes_time (); if (proc_info && strlen (proc_info)) { mes_file_win (proc_info); mes_file_win (":"); } if (error_nr >= 0 && error_nr < sizeof (mes_err_txt) / sizeof (*mes_err_txt)) { mes_file_win (mes_err_txt[error_nr]); } if (txt) { mes_file_win (" ("); mes_file_win (txt); mes_file_win (")\n"); } else mes_file_win ("\n");} /* ighmm_mes_err */#ifdef GHMM_UNSUPORTED/*============================================================================*/void ighmm_mes_proc_start (char *proc_info){ mes_t *mpc = mes_process_get (); int i; ighmm_mes_time (); if (proc_info) { mes_file (proc_info); mes_file (":"); } mes_file (" ***** PROGRAM STARTED *****\n"); if (mpc) for (i = 0; i < mpc->argc; i++) { if (!i) mes_file ("program call name is : "); else mes_aux (MES_FLAG_FILE, "parameter %10d : ", i); mes_file (mpc->argv[i]); mes_file ("\n"); }} /* ighmm_mes_proc_start *//*============================================================================*/int ighmm_mes_insert (FILE * fp, char src, int cnt){ int i = cnt; if (fp && fp - stdout) for (i = 0; i < cnt && !ighmm_mes_fputc (fp, src); i++); else for (i = 0; i < cnt; i++) ighmm_mes_smart (MES_FLAG_WIN, &src, 1); if (i == cnt) return (0); return (-1);} /* ighmm_mes_insert */#endif /* GHMM_UNSUPORTED *//******************************************************************************//******************************************************************************//*============================================================================*/void *ighmm_malloc (int bytes){ void *res; if (bytes <= 0) bytes = 1; res = malloc (bytes); if (res) return (res); else mes_aux (MES_FLAG_TIME_WIN, "malloc: could not allocate %d bytes\n", bytes); return (NULL);} /* ighmm_malloc *//*============================================================================*/void *ighmm_calloc (int bytes){ void *res; if (bytes <= 0) bytes = 1; res = calloc (1, bytes); if (res) return (res); else mes_aux (MES_FLAG_TIME_WIN, "calloc: could not allocate %d bytes\n", bytes); return (NULL);} /* ighmm_calloc *//*============================================================================*/int ighmm_realloc (void **mem, int bytes){ void *res; if (bytes <= 0) bytes = 1; if (!mem) return (-1); if (!*mem) res = malloc (bytes); else res = realloc (*mem, bytes); if (res) { *mem = res; return (0); } else mes_aux (MES_FLAG_TIME_WIN, "realloc: could not reallocate %d bytes\n", bytes); return (-1);} /* ighmm_realloc *//*============================================================================*/FILE *ighmm_mes_fopen (const char *filename, char *attrstr){ FILE *fp; if (mes_filename_check (filename)) goto STOP; if (!attrstr) goto STOP; if (!strcmp (filename, "stdout")) { return (stdout); } fp = fopen (filename, attrstr); if (fp) { if (!strchr (attrstr, 'b') && !strchr (attrstr, 't')) { mes_file_win ("fopen: file \""); mes_file_win (filename); mes_file_win ("\" opened with ambiguous attributes \""); mes_file_win (attrstr); mes_file_win ("\"\n"); } return (fp); }STOP: ighmm_mes_time (); mes_file_win ("fopen: could not open file \""); mes_file_win (filename); mes_file_win ("\" with attribute \""); mes_file_win (attrstr); mes_file_win ("\"\n"); return (NULL);} /* ighmm_mes_fopen */#ifdef GHMM_UNSUPORTED/*============================================================================*/int ighmm_mes_fread_quiet (FILE * fp, void *mem, int bytes){ if (!bytes) return (0); if (mem && fp) return (fread (mem, 1, bytes, fp)); else mes_aux (MES_FLAG_TIME_WIN, "fread: could not read %d bytes from FILE(%p) to mem(%p)\n", bytes, fp, mem); return (-1);} /* ighmm_mes_fread_quiet *//*============================================================================*/int ighmm_mes_fread (FILE * fp, void *mem, int bytes){ if (!bytes) return (0); if (mem && fp && fread (mem, 1, bytes, fp) == (unsigned int) bytes) return (0); else mes_aux (MES_FLAG_TIME_WIN, "fread: could not read %d bytes from FILE(%p) to mem(%p)\n", bytes, fp, mem); return (-1);} /* ighmm_mes_fread *//*============================================================================*/int ighmm_mes_fwrite (FILE * fp, void *mem, int bytes){ if (!fp || !mem) bytes = -1; else if (bytes < 0) bytes = strlen (mem); if (!bytes) return (0); if (bytes > 0 && fwrite (mem, 1, bytes, fp) == (unsigned int) bytes) return (0); else mes_aux (MES_FLAG_TIME_WIN, "fwrite: could not write %d bytes from mem(%p) to FILE(%p)\n", bytes, mem, fp); return (-1);} /* ighmm_mes_fwrite *//*============================================================================*/int ighmm_mes_fputc (FILE * fp, char chr){ if (fp && fputc (chr, fp) != EOF) return (0); else mes_aux (MES_FLAG_TIME_WIN, "fputc: could not write byte %X to FILE(%p)\n", (int) chr & 0xFF, fp); return (-1);} /* ighmm_mes_fputc *//*============================================================================*/int ighmm_mes_fputs (FILE * fp, char *str){ if (fp && str && fputs (str, fp) != EOF) return (0); if (str) mes_aux (MES_FLAG_TIME_WIN, "fputs: could not write string %s\n", str); else mes_aux (MES_FLAG_TIME_WIN, "fputs: could not write 0 pointer\n"); return (-1);} /* ighmm_mes_fputs *//*============================================================================*/int ighmm_mes_fgetc (FILE * fp){ int res = fp ? fgetc (fp) : EOF; if (res != EOF) return (res); else mes_aux (MES_FLAG_TIME_WIN, "fgetc: end of FILE(%p)\n", fp); return (res);} /* ighmm_mes_fgetc *//*============================================================================*/int ighmm_mes_fflush (FILE * fp){ int res = fp ? fflush (fp) : -1; if (!res) return (0); else mes_aux (MES_FLAG_TIME_WIN, "fflush: could not flush FILE(%p)\n", fp); return (res);} /* ighmm_mes_fflush *//*============================================================================*/int ighmm_mes_fprintf (FILE * fp, char *format, ...){ va_list args; char *txt; if (!format) return (0); va_start (args, format); txt = ighmm_mprintf_va (NULL, 0, format, args); if (!txt) { ighmm_mes_time (); mes_file_win ("sprintf_va: call with format string\""); mes_file_win (format); mes_file_win ("\" without success\n"); return (-1); } if (fp && fp - stdout) ighmm_mes_fputs (fp, txt); else mes_win (txt); free (txt); return (1);} /* ighmm_mes_fprintf *//*============================================================================*/int ighmm_mes_fseek (FILE * fp, long offset, int fromwhere){ int res = fp ? fseek (fp, offset, fromwhere) : -1; if (!res) return (0); mes_aux (MES_FLAG_TIME_WIN, "fseek: could not position FILE(%p) at %ld", fp, offset); switch (fromwhere) { case SEEK_SET: mes_aux (MES_FLAG_FILE_WIN, "\n"); break; case SEEK_END: mes_aux (MES_FLAG_FILE_WIN, " from the end\n"); break; case SEEK_CUR: mes_aux (MES_FLAG_FILE_WIN, " from current position\n"); break; default: mes_aux (MES_FLAG_FILE_WIN, " with undefinded offset %d\n", fromwhere); break; } return (res);} /* ighmm_mes_fseek *//*============================================================================*/#ifdef WIN32int mes_fseek64 (FILE * fp, unsigned int uoff, unsigned int loff, int fromwhere){/* lower 32bit in lpos, upper 32bit in upos */ int fh; /* file handle */ __int64 pos64; __int64 off64; if (!fp) return (0); fh = _fileno (fp); off64 = (((__int64) uoff) << 32) + (__int64) loff; pos64 = _lseeki64 (fh, off64, fromwhere); if (pos64 == -1L) { mes_file_win ("_lseeki64 failed:"); mes_file_win (strerror (errno)); return (0); } return (1);}/*============================================================================*/int mes_ftell64 (int fh, unsigned int *upos, unsigned int *lpos){ /* lower 32bit in *lpos, upper 32bit in *upos */ __int64 pos64; if (fh < 0) return (0); pos64 = _lseeki64 (fh, 0, SEEK_CUR); if (pos64 == -1L) { mes_file_win ("_lseeki64 failed:"); mes_file_win (strerror (errno)); return (0); } *upos = (unsigned int) (pos64 >> 32); *lpos = (unsigned int) pos64; return (1);}#endif /* WIN32 *//*============================================================================*/int ighmm_mes_ftell (FILE * fp){ int res = fp ? ftell (fp) : -1; if (res != -1) return (res); else mes_aux (MES_FLAG_TIME_WIN, "ftell: could not find current position of FILE(%p)\n", fp); return (res);} /* ighmm_mes_ftell *//*============================================================================*/int ighmm_mes_remove (char *filename){#define CUR_PROC "ighmm_mes_remove" int res = -1; if (mes_filename_check (filename)) goto STOP; res = remove (filename); if (!res) return (0);STOP: ighmm_mes_time (); mes_file_win ("remove: could not remove file \""); mes_file_win (filename); mes_file_win ("\";"); if (res != -1) mes_file_win (strerror (errno)); mes_file_win ("\n"); return (res);#undef CUR_PROC} /* ighmm_mes_remove *//*============================================================================*/int ighmm_mes_rename (char *oldname, char *newname){#define CUR_PROC "ighmm_mes_rename" int res = -1; if (mes_filename_check (oldname)) goto STOP; if (mes_filename_check (newname)) goto STOP;#if defined(_PPC_) || defined(WIN32) if (strcmp (oldname, newname)) { FILE *fp; fp = fopen (newname, "rb"); if (fp) { fclose (fp); ighmm_mes_remove (newname); } } else return (0);#endif res = rename (oldname, newname); if (!res) return (res);STOP: ighmm_mes_time (); mes_file_win ("rename: could not rename \""); mes_file_win (oldname); mes_file_win ("\" -> \""); mes_file_win (newname); mes_file_win ("\"; "); if (res != -1) mes_file_win (strerror (errno)); mes_file_win ("\n"); return (res);#undef CUR_PROC} /* ighmm_mes_rename *//*============================================================================*/int ighmm_mes_move (char *oldname, char *newname){#define CUR_PROC "ighmm_mes_move" int res = -1; int tmp; if (mes_filename_check (oldname)) goto STOP; if (mes_filename_check (newname)) goto STOP; if (!strcmp (oldname, newname)) goto STOP; /* first try to rename */ tmp = ighmm_mes_ability (0); if (!ighmm_mes_rename (oldname, newname)) { res = 0; ighmm_mes_ability (tmp); goto STOP; } ighmm_mes_ability (tmp); /* need to copy */ if (ighmm_mes_copy (oldname, newname)) goto STOP; ighmm_mes_remove (oldname); res = 0;STOP: if (res < 0) { ighmm_mes_time (); mes_file_win ("move: could not move "); mes_file_win (oldname); mes_file_win (" -> "); mes_file_win (newname); mes_file_win ("\n"); } return (res);#undef CUR_PROC} /* ighmm_mes_move *//*============================================================================*/int ighmm_mes_copy (char *oldname, char *newname){#define CUR_PROC "ighmm_mes_copy" int res = -1; FILE *dst = NULL; FILE *src = NULL; char *buf = NULL; if ((dst = ighmm_mes_fopen (newname, "wb")) == NULL) goto STOP; if ((src = ighmm_mes_fopen (oldname, "rb")) == NULL) goto STOP; if ((buf = ighmm_malloc (0x10000 * sizeof (char))) == NULL) goto STOP; while (!feof (src)) { int cnt = fread (buf, 1, 0x10000, src); if (cnt <= 0) break; if (fwrite (buf, 1, cnt, dst) <= 0) goto STOP; } res = 0;STOP: if (buf) free (buf); if (src) fclose (src); if (dst) fclose (dst); return (res);# undef CUR_PROC} /* ighmm_mes_copy *//*============================================================================*/FILE *ighmm_mes_tmpfopen (char *path){# define CUR_PROC "ighmm_mes_tmpfopen" FILE *fp; char name[16]; char tmpname[L_tmpnam + 16]; int i; if (path) strncpy (tmpname, path, L_tmpnam); else tmpname[0] = 0; for (i = 0; i < 0x10000; i++) { sprintf (name, "%80X.TMP", rand ()); strcat (tmpname, name); fp = fopen (tmpname, "rb"); if (fp) { fclose (fp); continue; } fp = fopen (tmpname, "w+b"); if (fp) return (fp); break; } ighmm_mes_time (); mes_file_win ("tmpfopen: no success\n"); return (NULL);# undef CUR_PROC} /* ighmm_mes_tmpfopen *//*============================================================================*/int ighmm_mes_tmpfclose (FILE ** fp){ if (!fp) return (0); if (!*fp) return (0); fclose (*fp); (*fp) = 0; return (0);} /* ighmm_mes_tmpfclose *//*============================================================================*/FILE *ighmm_mes_tmpfile (void){# define CUR_PROC "ighmm_mes_tmpfile" FILE *fp = tmpfile (); if (fp) return (fp); else { ighmm_mes_time (); mes_file_win ("tmpfile: no success\n"); } return (NULL);# undef CUR_PROC} /* ighmm_mes_tmpfile */#endif /* GHMM_UNSUPORTED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -