Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/core/log.c

201 lines
5.6 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* log.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/10/31 00:14:27 |___/ */
/* */
/* ************************************************************************** */
#include <libg1m/internals.h>
#include <libg1m/format.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#define min(A, B) ((A) < (B) ? (A) : (B))
/* ************************************************************************** */
/* Memory logging */
/* ************************************************************************** */
/**
* putascii:
* Put a number in ASCII-hex, in a n-dimensionned field.
*
* @arg p pointer where to put ASCII number
* @arg i ASCII number
* @arg n the size of the field
*/
static void putascii(unsigned char *p, unsigned int i, int n)
{
/* goto end of the field */
p += (n - 1);
/* for each digit */
while (n--) {
/* get end digit from this point */
int j = i % 16;
/* put it in ASCII-hex */
*p-- = j >= 10 ? j - 10 + 'A' : j + '0';
/* then go to digit that's left */
i /= 16;
}
}
/**
* log_mem_hex:
* Prints the octal interpretation of a max of two octets.
*
* @arg s the string where to put it
* @arg m the memory zone to print
* @arg n the size of the memory zone
*/
static void log_mem_hex(char *s, unsigned char *m, size_t n)
{
size_t l = 0;
while (l < 16) {
/* put the hex number */
if (n) putascii((unsigned char*)s, *m++, 2), s += 2;
else *s++ = ' ', *s++ = ' ';
/* decrement size of the memory zone to go */
n -= !!n;
/* go to next character if s is at the ending of a group */
if (l++ % 2) s++;
}
}
/**
* log_mem_asc:
* Prints the ascii interpretation of a max of two octets.
*
* @arg s the string where to put it
* @arg m the memory zone to print
* @arg n the size of the memory zone
*/
static void log_mem_asc(char *s, unsigned char *m, size_t n)
{
size_t l = 0;
/* for each byte */
while (n-- && l++ < 16) {
/* put the character (or a dot if non printable) */
if (isprint(*m++)) *s++ = *((char*)m - 1);
else *s++ = '.';
}
/* put the line ending */
*s++ = '\n', *s = '\0';
}
/**
* g1m_log_mem:
* Print memory zone.
*
* @arg prefx the line prefix
* @arg m the memory zone to print
* @arg n the size of the memory zone
*/
void g1m_log_mem(const char *prefx, void *m, size_t n)
{
/* if nothing, print it directly */
if (!n) fprintf(stderr, "%s(nothing)\n", prefx);
/* prepare line buffer */
unsigned int lineoff = strlen(prefx);
char linebuf[strlen(prefx) + 58];
memcpy(linebuf, prefx, lineoff);
/* - for spaces - */
memcpy(&linebuf[lineoff], "0000 0000 0000 0000 0000 0000 0000 0000 ", 40);
/* then loop-loop-loop-loop-loop */
unsigned char *p = m;
while (n > 0) {
/* fill in ascii-hex part */
log_mem_hex(&linebuf[lineoff], p, n);
/* fill in ascii part */
log_mem_asc(&linebuf[lineoff + 40], p, n);
/* then print line */
fputs(linebuf, stderr);
/* and increment pointer */
p += 16;
n -= min(16, n);
}
}
/* ************************************************************************** */
/* Enumerations strings */
/* ************************************************************************** */
/**
* g1m_get_type_string:
* Get string description of a type of a G1M file.
*
* @arg code the code.
* @return the string.
*/
const char *g1m_get_type_string(int main_id, int type)
{
switch (main_id) {
case g1m_mid_usbpower:
switch (type) {
case g1m_usbpower_type_addin_cg: return ("fx-CG addin");
case g1m_usbpower_type_mcs: return ("mcs");
case g1m_usbpower_type_eact: return ("e-activity");
case g1m_usbpower_type_g2r: return ("mcs (g2r)");
case g1m_usbpower_type_g3p: return ("g3p");
case g1m_usbpower_type_addin: return ("addin");
}
break;
case g1m_mid_ly755:
switch (type) {
case g1m_ly755_type_lang: return ("fx-CG language file");
}
}
return ("unknown");
}
/**
* g1m_get_mcs_ftype_string:
* Get string description of a type of a G1M part directory.
*
* @arg code the code.
* @return the string.
*/
const char *g1m_get_mcs_ftype_string(int code)
{
switch (code) {
case mcs_ftype_program: return ("program");
case mcs_ftype_list: return ("list");
case mcs_ftype_mat: return ("matrix");
case mcs_ftype_picture: return ("picture");
case mcs_ftype_capture: return ("capture");
case mcs_ftype_spreadsheet: return ("spreadsheet");
default: return ("unknown");
}
}
/**
* g1m_get_eact_ltype_string:
* Get string description of a type of an E-Act line.
*
* @arg code the code.
* @return the string.
*/
const char *g1m_get_eact_ltype_string(int code)
{
switch (code) {
case eact_ltype_calc: return ("calculation");
case eact_ltype_calc_result: return ("calculation result");
case eact_ltype_content: return ("content");
case eact_ltype_stdheading: return ("standard heading");
case eact_ltype_picture: return ("picture");
case eact_ltype_text: return ("pure text");
case eact_ltype_code: return ("text mixed with functions");
default: return ("unknown");
}
}