libyasm
Data Structures | Macros | Typedefs | Functions
file.h File Reference

YASM file helpers. More...

Go to the source code of this file.

Data Structures

struct  yasm_scanner
 Re2c scanner state. More...
 

Macros

#define YASM_LIB_DECL
 
#define yasm__splitpath(path, tail)   yasm__splitpath_unix(path, tail)
 Split a pathname into head (directory) and tail (base filename) portions. More...
 
#define yasm__combpath(from, to)   yasm__combpath_unix(from, to)
 Build a pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from". More...
 
#define yasm__createpath(path)   yasm__createpath_common(path, 0)
 Recursively create tree of directories needed for pathname. More...
 
#define YASM_WRITE_8(ptr, val)   *((ptr)++) = (unsigned char)((val) & 0xFF)
 Write an 8-bit value to a buffer, incrementing buffer pointer. More...
 
#define YASM_WRITE_16_L(ptr, val)
 Write a 16-bit value to a buffer in little endian, incrementing buffer pointer. More...
 
#define YASM_WRITE_32_L(ptr, val)
 Write a 32-bit value to a buffer in little endian, incrementing buffer pointer. More...
 
#define YASM_WRITE_16_B(ptr, val)
 Write a 16-bit value to a buffer in big endian, incrementing buffer pointer. More...
 
#define YASM_WRITE_32_B(ptr, val)
 Write a 32-bit value to a buffer in big endian, incrementing buffer pointer. More...
 
#define YASM_SAVE_8(ptr, val)   *(ptr) = (unsigned char)((val) & 0xFF)
 Write an 8-bit value to a buffer. More...
 
#define YASM_SAVE_16_L(ptr, val)
 Write a 16-bit value to a buffer in little endian. More...
 
#define YASM_SAVE_32_L(ptr, val)
 Write a 32-bit value to a buffer in little endian. More...
 
#define YASM_SAVE_16_B(ptr, val)
 Write a 16-bit value to a buffer in big endian. More...
 
#define YASM_SAVE_32_B(ptr, val)
 Write a 32-bit value to a buffer in big endian. More...
 
#define YASM_READ_8(val, ptr)   (val) = *((ptr)++) & 0xFF
 Read an 8-bit value from a buffer, incrementing buffer pointer. More...
 
#define YASM_READ_16_L(val, ptr)
 Read a 16-bit value from a buffer in little endian, incrementing buffer pointer. More...
 
#define YASM_READ_32_L(val, ptr)
 Read a 32-bit value from a buffer in little endian, incrementing buffer pointer. More...
 
#define YASM_READ_16_B(val, ptr)
 Read a 16-bit value from a buffer in big endian, incrementing buffer pointer. More...
 
#define YASM_READ_32_B(val, ptr)
 Read a 32-bit value from a buffer in big endian, incrementing buffer pointer. More...
 
#define YASM_LOAD_8(val, ptr)   (val) = *(ptr) & 0xFF
 Read an 8-bit value from a buffer. More...
 
#define YASM_LOAD_16_L(val, ptr)
 Read a 16-bit value from a buffer in little endian. More...
 
#define YASM_LOAD_32_L(val, ptr)
 Read a 32-bit value from a buffer in little endian. More...
 
#define YASM_LOAD_16_B(val, ptr)
 Read a 16-bit value from a buffer in big endian. More...
 
#define YASM_LOAD_32_B(val, ptr)
 Read a 32-bit value from a buffer in big endian. More...
 

Typedefs

typedef struct yasm_scanner yasm_scanner
 Re2c scanner state. More...
 

Functions

YASM_LIB_DECL void yasm_scanner_initialize (yasm_scanner *scanner)
 Initialize scanner state. More...
 
YASM_LIB_DECL void yasm_scanner_delete (yasm_scanner *scanner)
 Frees any memory used by scanner state; does not free state itself. More...
 
YASM_LIB_DECL int yasm_fill_helper (yasm_scanner *scanner, unsigned char **cursor, size_t(*input_func)(void *d, unsigned char *buf, size_t max), void *input_func_data)
 Fill a scanner state structure with data coming from an input function. More...
 
YASM_LIB_DECL void yasm_unescape_cstring (unsigned char *str, size_t *len)
 Unescape a string with C-style escapes. More...
 
YASM_LIB_DECL size_t yasm__splitpath_unix (const char *path, const char **tail)
 Split a UNIX pathname into head (directory) and tail (base filename) portions. More...
 
YASM_LIB_DECL size_t yasm__splitpath_win (const char *path, const char **tail)
 Split a Windows pathname into head (directory) and tail (base filename) portions. More...
 
YASM_LIB_DECL char * yasm__getcwd (void)
 Get the current working directory. More...
 
YASM_LIB_DECL char * yasm__abspath (const char *path)
 Convert a relative or absolute pathname into an absolute pathname. More...
 
YASM_LIB_DECL char * yasm__combpath_unix (const char *from, const char *to)
 Build a UNIX pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from". More...
 
YASM_LIB_DECL char * yasm__combpath_win (const char *from, const char *to)
 Build a Windows pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from". More...
 
YASM_LIB_DECL size_t yasm__createpath_common (const char *path, int win)
 Recursively create tree of directories needed for pathname. More...
 
YASM_LIB_DECL FILE * yasm_fopen_include (const char *iname, const char *from, const char *mode, char **oname)
 Try to find and open an include file, searching through include paths. More...
 
YASM_LIB_DECL void yasm_delete_include_paths (void)
 Delete any stored include paths added by yasm_add_include_path().
 
YASM_LIB_DECL const char * yasm_get_include_dir (void **iter)
 Iterate through include paths.
 
YASM_LIB_DECL void yasm_add_include_path (const char *path)
 Add an include path for use by yasm_fopen_include(). More...
 
YASM_LIB_DECL size_t yasm_fwrite_16_l (unsigned short val, FILE *f)
 Direct-to-file version of YASM_SAVE_16_L(). More...
 
YASM_LIB_DECL size_t yasm_fwrite_32_l (unsigned long val, FILE *f)
 Direct-to-file version of YASM_SAVE_32_L(). More...
 
YASM_LIB_DECL size_t yasm_fwrite_16_b (unsigned short val, FILE *f)
 Direct-to-file version of YASM_SAVE_16_B(). More...
 
YASM_LIB_DECL size_t yasm_fwrite_32_b (unsigned long val, FILE *f)
 Direct-to-file version of YASM_SAVE_32_B(). More...
 

Detailed Description

YASM file helpers.

Definition in file file.h.

Macro Definition Documentation

#define yasm__combpath (   from,
  to 
)    yasm__combpath_unix(from, to)

Build a pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from".

Result is relative if both from and to are relative. Unless otherwise defined, defaults to yasm__combpath_unix().

Parameters
fromfrom pathname
toto pathname
Returns
Combined path (newly allocated).

Definition at line 174 of file file.h.

#define yasm__createpath (   path)    yasm__createpath_common(path, 0)

Recursively create tree of directories needed for pathname.

Unless otherwise defined, defaults to yasm__createpath_unix().

Parameters
pathpathname
Returns
Length of directory portion of pathname.

Definition at line 198 of file file.h.

#define yasm__splitpath (   path,
  tail 
)    yasm__splitpath_unix(path, tail)

Split a pathname into head (directory) and tail (base filename) portions.

Unless otherwise defined, defaults to yasm__splitpath_unix().

Parameters
pathpathname
tail(returned) base filename
Returns
Length of head (directory).

Definition at line 119 of file file.h.

#define YASM_LOAD_16_B (   val,
  ptr 
)
Value:
do { \
(val) = (*(ptr) & 0xFF) << 8; \
(val) |= *((ptr)+1) & 0xFF; \
} while (0)

Read a 16-bit value from a buffer in big endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 505 of file file.h.

#define YASM_LOAD_16_L (   val,
  ptr 
)
Value:
do { \
(val) = *(ptr) & 0xFF; \
(val) |= (*((ptr)+1) & 0xFF) << 8; \
} while (0)

Read a 16-bit value from a buffer in little endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 479 of file file.h.

#define YASM_LOAD_32_B (   val,
  ptr 
)
Value:
do { \
(val) = (unsigned long)((*(ptr) & 0xFF) << 24); \
(val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 16); \
(val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 8); \
(val) |= (unsigned long)(*((ptr)+3) & 0xFF); \
} while (0)

Read a 32-bit value from a buffer in big endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 517 of file file.h.

#define YASM_LOAD_32_L (   val,
  ptr 
)
Value:
do { \
(val) = (unsigned long)(*(ptr) & 0xFF); \
(val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 8); \
(val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 16); \
(val) |= (unsigned long)((*((ptr)+3) & 0xFF) << 24); \
} while (0)

Read a 32-bit value from a buffer in little endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 491 of file file.h.

#define YASM_LOAD_8 (   val,
  ptr 
)    (val) = *(ptr) & 0xFF

Read an 8-bit value from a buffer.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val8-bit value

Definition at line 470 of file file.h.

#define YASM_READ_16_B (   val,
  ptr 
)
Value:
do { \
(val) = (*((ptr)++) & 0xFF) << 8; \
(val) |= *((ptr)++) & 0xFF; \
} while (0)

Read a 16-bit value from a buffer in big endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 445 of file file.h.

#define YASM_READ_16_L (   val,
  ptr 
)
Value:
do { \
(val) = *((ptr)++) & 0xFF; \
(val) |= (*((ptr)++) & 0xFF) << 8; \
} while (0)

Read a 16-bit value from a buffer in little endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 419 of file file.h.

#define YASM_READ_32_B (   val,
  ptr 
)
Value:
do { \
(val) = (*((ptr)++) & 0xFF) << 24; \
(val) |= (*((ptr)++) & 0xFF) << 16; \
(val) |= (*((ptr)++) & 0xFF) << 8; \
(val) |= *((ptr)++) & 0xFF; \
} while (0)

Read a 32-bit value from a buffer in big endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 457 of file file.h.

#define YASM_READ_32_L (   val,
  ptr 
)
Value:
do { \
(val) = *((ptr)++) & 0xFF; \
(val) |= (*((ptr)++) & 0xFF) << 8; \
(val) |= (*((ptr)++) & 0xFF) << 16; \
(val) |= (*((ptr)++) & 0xFF) << 24; \
} while (0)

Read a 32-bit value from a buffer in little endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 431 of file file.h.

#define YASM_READ_8 (   val,
  ptr 
)    (val) = *((ptr)++) & 0xFF

Read an 8-bit value from a buffer, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val8-bit value

Definition at line 410 of file file.h.

#define YASM_SAVE_16_B (   ptr,
  val 
)
Value:
do { \
*(ptr) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)+1) = (unsigned char)((val) & 0xFF); \
} while (0)

Write a 16-bit value to a buffer in big endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 345 of file file.h.

#define YASM_SAVE_16_L (   ptr,
  val 
)
Value:
do { \
*(ptr) = (unsigned char)((val) & 0xFF); \
*((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \
} while (0)

Write a 16-bit value to a buffer in little endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 319 of file file.h.

#define YASM_SAVE_32_B (   ptr,
  val 
)
Value:
do { \
*(ptr) = (unsigned char)(((val) >> 24) & 0xFF); \
*((ptr)+1) = (unsigned char)(((val) >> 16) & 0xFF); \
*((ptr)+2) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)+3) = (unsigned char)((val) & 0xFF); \
} while (0)

Write a 32-bit value to a buffer in big endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 357 of file file.h.

#define YASM_SAVE_32_L (   ptr,
  val 
)
Value:
do { \
*(ptr) = (unsigned char)((val) & 0xFF); \
*((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)+2) = (unsigned char)(((val) >> 16) & 0xFF); \
*((ptr)+3) = (unsigned char)(((val) >> 24) & 0xFF); \
} while (0)

Write a 32-bit value to a buffer in little endian.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 331 of file file.h.

#define YASM_SAVE_8 (   ptr,
  val 
)    *(ptr) = (unsigned char)((val) & 0xFF)

Write an 8-bit value to a buffer.

Does not increment buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val8-bit value

Definition at line 310 of file file.h.

#define YASM_WRITE_16_B (   ptr,
  val 
)
Value:
do { \
*((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)++) = (unsigned char)((val) & 0xFF); \
} while (0)

Write a 16-bit value to a buffer in big endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 284 of file file.h.

#define YASM_WRITE_16_L (   ptr,
  val 
)
Value:
do { \
*((ptr)++) = (unsigned char)((val) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \
} while (0)

Write a 16-bit value to a buffer in little endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val16-bit value

Definition at line 258 of file file.h.

#define YASM_WRITE_32_B (   ptr,
  val 
)
Value:
do { \
*((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)++) = (unsigned char)((val) & 0xFF); \
} while (0)

Write a 32-bit value to a buffer in big endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 296 of file file.h.

#define YASM_WRITE_32_L (   ptr,
  val 
)
Value:
do { \
*((ptr)++) = (unsigned char)((val) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \
*((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \
} while (0)

Write a 32-bit value to a buffer in little endian, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val32-bit value

Definition at line 270 of file file.h.

#define YASM_WRITE_8 (   ptr,
  val 
)    *((ptr)++) = (unsigned char)((val) & 0xFF)

Write an 8-bit value to a buffer, incrementing buffer pointer.

Note
Only works properly if ptr is an (unsigned char *).
Parameters
ptrbuffer
val8-bit value

Definition at line 249 of file file.h.

Typedef Documentation

typedef struct yasm_scanner yasm_scanner

Re2c scanner state.

Function Documentation

YASM_LIB_DECL char* yasm__abspath ( const char *  path)

Convert a relative or absolute pathname into an absolute pathname.

Parameters
pathpathname
Returns
Absolute version of path (newly allocated).
YASM_LIB_DECL char* yasm__combpath_unix ( const char *  from,
const char *  to 
)

Build a UNIX pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from".

Result is relative if both from and to are relative.

Parameters
fromfrom pathname
toto pathname
Returns
Combined path (newly allocated).
YASM_LIB_DECL char* yasm__combpath_win ( const char *  from,
const char *  to 
)

Build a Windows pathname that is equivalent to accessing the "to" pathname when you're in the directory containing "from".

Result is relative if both from and to are relative.

Parameters
fromfrom pathname
toto pathname
Returns
Combined path (newly allocated).
YASM_LIB_DECL size_t yasm__createpath_common ( const char *  path,
int  win 
)

Recursively create tree of directories needed for pathname.

Parameters
pathpathname
winhandle windows paths
Returns
Length of directory portion of pathname.
YASM_LIB_DECL char* yasm__getcwd ( void  )

Get the current working directory.

Returns
Current working directory pathname (newly allocated).
YASM_LIB_DECL size_t yasm__splitpath_unix ( const char *  path,
const char **  tail 
)

Split a UNIX pathname into head (directory) and tail (base filename) portions.

Parameters
pathpathname
tail(returned) base filename
Returns
Length of head (directory).
YASM_LIB_DECL size_t yasm__splitpath_win ( const char *  path,
const char **  tail 
)

Split a Windows pathname into head (directory) and tail (base filename) portions.

Parameters
pathpathname
tail(returned) base filename
Returns
Length of head (directory).
YASM_LIB_DECL void yasm_add_include_path ( const char *  path)

Add an include path for use by yasm_fopen_include().

If path is relative, it is treated by yasm_fopen_include() as relative to the current working directory.

Parameters
pathpath to add
YASM_LIB_DECL int yasm_fill_helper ( yasm_scanner scanner,
unsigned char **  cursor,
size_t(*)(void *d, unsigned char *buf, size_t max)  input_func,
void *  input_func_data 
)

Fill a scanner state structure with data coming from an input function.

Parameters
scannerRe2c scanner state
cursorRe2c scan cursor
input_funcInput function to read data; takes buffer and maximum number of bytes, returns number of bytes read.
input_func_dataData to pass as the first parameter to input_func
Returns
1 if this was the first time this function was called on this scanner state, 0 otherwise.
YASM_LIB_DECL FILE* yasm_fopen_include ( const char *  iname,
const char *  from,
const char *  mode,
char **  oname 
)

Try to find and open an include file, searching through include paths.

First iname is looked for relative to the directory containing "from", then it's looked for relative to each of the include paths.

All pathnames may be either absolute or relative; from, oname, and include paths, if relative, are relative from the current working directory.

First match wins; the full pathname (newly allocated) to the opened file is saved into oname, and the fopen'ed FILE * is returned. If not found, NULL is returned.

Parameters
inamefile to include
fromfile doing the including
modefopen mode string
onamefull pathname of included file (may be relative). NULL may be passed if this is unwanted.
Returns
fopen'ed include file, or NULL if not found.
YASM_LIB_DECL size_t yasm_fwrite_16_b ( unsigned short  val,
FILE *  f 
)

Direct-to-file version of YASM_SAVE_16_B().

Note
Using the macro multiple times with a single fwrite() call will probably be faster than calling this function many times.
Parameters
val16-bit value
ffile
Returns
1 if the write was successful, 0 if not (just like fwrite()).
YASM_LIB_DECL size_t yasm_fwrite_16_l ( unsigned short  val,
FILE *  f 
)

Direct-to-file version of YASM_SAVE_16_L().

Note
Using the macro multiple times with a single fwrite() call will probably be faster than calling this function many times.
Parameters
val16-bit value
ffile
Returns
1 if the write was successful, 0 if not (just like fwrite()).
YASM_LIB_DECL size_t yasm_fwrite_32_b ( unsigned long  val,
FILE *  f 
)

Direct-to-file version of YASM_SAVE_32_B().

Note
Using the macro multiple times with a single fwrite() call will probably be faster than calling this function many times.
Parameters
val32-bit value
ffile
Returns
1 if the write was successful, 0 if not (just like fwrite()).
YASM_LIB_DECL size_t yasm_fwrite_32_l ( unsigned long  val,
FILE *  f 
)

Direct-to-file version of YASM_SAVE_32_L().

Note
Using the macro multiple times with a single fwrite() call will probably be faster than calling this function many times.
Parameters
val32-bit value
ffile
Returns
1 if the write was successful, 0 if not (just like fwrite()).
YASM_LIB_DECL void yasm_scanner_delete ( yasm_scanner scanner)

Frees any memory used by scanner state; does not free state itself.

Parameters
scannerRe2c scanner state
YASM_LIB_DECL void yasm_scanner_initialize ( yasm_scanner scanner)

Initialize scanner state.

Parameters
scannerRe2c scanner state
YASM_LIB_DECL void yasm_unescape_cstring ( unsigned char *  str,
size_t *  len 
)

Unescape a string with C-style escapes.

Handles b, f, n, r, t, and hex and octal escapes. String is updated in-place. Edge cases:

  • hex escapes: reads as many hex digits as possible, takes last 2 as value.
  • oct escapes: takes up to 3 digits 0-9 and scales appropriately, with warning.
    Parameters
    strC-style string (updated in place)
    lenlength of string (updated with new length)