Mapper  0.9.0
API documentation
Classes | Typedefs | Functions
file.c File Reference

(b51adba on 22 Nov 2014)

#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "libocad.h"
Include dependency graph for file.c:

Classes

struct  _IndexBuilder
 Structure for compacting entity indexes - used in the compaction callback functions. More...
 
struct  _ocad_file_bounds_data
 

Typedefs

typedef struct _IndexBuilder IndexBuilder
 Structure for compacting entity indexes - used in the compaction callback functions. More...
 
typedef struct _ocad_file_bounds_data ocad_file_bounds_data
 

Functions

int ocad_init ()
 Initializes the libocad library. More...
 
int ocad_shutdown ()
 Shuts down the libocad library. More...
 
int ocad_file_open (OCADFile **pfile, const char *filename)
 Opens a file with the given filename. More...
 
int ocad_file_open_mapped (OCADFile **pfile, const char *filename)
 Behaves exactly like ocad_file_open(), except that the system attempts to open the file via memory mapping. More...
 
int ocad_file_open_memory (OCADFile **pfile, u8 *buffer, u32 size)
 Behaves exactly like ocad_file_open(), except that the given buffer must contain the map data. More...
 
int ocad_file_close (OCADFile *pfile)
 Closes an open OCADFile. More...
 
int ocad_file_save (OCADFile *pfile)
 
int ocad_file_save_as (OCADFile *pfile, const char *filename)
 Saves an open OCADFile to the given filename. More...
 
int ocad_file_new (OCADFile **pfile)
 Creates a new OCADFile struct in memory. More...
 
int ocad_file_reserve (OCADFile *file, int amount)
 Makes sure that 'amount' number of bytes are reserved in the file's buffer in addition to the already used space. More...
 
int ocad_file_compact (OCADFile *pfile)
 Optimizes and repairs an open OCADFile. More...
 
int ocad_export (OCADFile *pfile, void *opts)
 Exports part or all of an OCAD file to a specific format. More...
 
int ocad_export_file (OCADFile *pfile, const char *filename, void *opts)
 Exports part or all of an OCAD file to a specific file. More...
 
bool ocad_file_bounds (OCADFile *file, OCADRect *rect)
 Calculates the bounding rectangle of all objects in the file. More...
 

Typedef Documentation

◆ IndexBuilder

typedef struct _IndexBuilder IndexBuilder

Structure for compacting entity indexes - used in the compaction callback functions.

◆ ocad_file_bounds_data

Function Documentation

◆ ocad_export()

int ocad_export ( OCADFile pfile,
void *  options 
)

Exports part or all of an OCAD file to a specific format.

The output stream must be preset in the options structure. You can use ocad_export_file() to export to a file. The options parameter must be binary compatible with OCADExportOptions.

◆ ocad_export_file()

int ocad_export_file ( OCADFile pfile,
const char *  filename,
void *  options 
)

Exports part or all of an OCAD file to a specific file.

The "output" field in the options is ignored and will be overwritten by a stream for the provided filename. The options parameter must be binary compatible with OCADExportOptions.

◆ ocad_file_bounds()

bool ocad_file_bounds ( OCADFile file,
OCADRect *  rect 
)

Calculates the bounding rectangle of all objects in the file.

◆ ocad_file_close()

int ocad_file_close ( OCADFile pfile)

Closes an open OCADFile.

The memory map is cleared, the file is closed, and all memory buffers used by the object are deallocated. After calling this function, the OCADFile object can be reused by ocad_file_open() without causing a memory leak.

Always returns OCAD_OK for success.

◆ ocad_file_compact()

int ocad_file_compact ( OCADFile pfile)

Optimizes and repairs an open OCADFile.

The file is reordered into Header, Colors, Setup, Symbols, Objects, and Strings. Entities of the same type are brought together into a contiguous section of file and any free space is compacted. The entity indexes are likewise compacted and cached data in the entities and index entries is regenerated, as follows:

Symbols: The "extent" field is recalculated from the symbol properties.

Objects: The bounding rectangle is recalculated from the path and symbol.

◆ ocad_file_new()

int ocad_file_new ( OCADFile **  pfile)

Creates a new OCADFile struct in memory.

Unused parts of the file buffer are set to zero.

Returns OCAD_OK on success, or OCAD_OUT_OF_MEMORY.

◆ ocad_file_open()

int ocad_file_open ( OCADFile **  pfile,
const char *  filename 
)

Opens a file with the given filename.

The file is loaded into memory and is accessible through the various ocad_file_* methods. The first argument can either be a pointer to a pre-allocated OCADFile object (e.g., on the stack), or nullptr to cause the object to be allocated on the heap. In the first case, the preallocated memory is considered to be uninitialized and will be zeroed before opening the file.

This function will verify the magic number and set up the OCAD file version.

An open OCADFile can be closed with ocad_file_close().

Returns OCAD_OK on success, or one of the following error codes:

  • OCAD_OUT_OF_MEMORY: Unable to allocate memory. errno was last set by malloc(2).
  • OCAD_FILE_WAS_READONLY: Unable to open file in read/write mode. errno was last set by open(2).
  • OCAD_MMAP_FAILED: Unable to memory map file. errno was last set by mmap(2) or read(2).
  • OCAD_INVALID_FORMAT: Unknown file format.

◆ ocad_file_open_mapped()

int ocad_file_open_mapped ( OCADFile **  pfile,
const char *  filename 
)

Behaves exactly like ocad_file_open(), except that the system attempts to open the file via memory mapping.

Returns 0 on success, one of the error codes returned by ocad_file_open(), or one of the following:

  • OCAD_MMAP_NOT_SUPPORTED: Memory mapping is not supported with this combination of system and libraries.

◆ ocad_file_open_memory()

int ocad_file_open_memory ( OCADFile **  pfile,
u8 buffer,
u32  size 
)

Behaves exactly like ocad_file_open(), except that the given buffer must contain the map data.

The buffer must be allocated with malloc(). Ownership of the buffer is transferred to libocad.

◆ ocad_file_reserve()

int ocad_file_reserve ( OCADFile file,
int  amount 
)

Makes sure that 'amount' number of bytes are reserved in the file's buffer in addition to the already used space.

Sets newly reserved memory to zero. Returns OCAD_OK or OCAD_OUT_OF_MEMORY.

WARNING: be extremely careful with this, as it might invalidate pointers to the buffer!

◆ ocad_file_save()

int ocad_file_save ( OCADFile pfile)

◆ ocad_file_save_as()

int ocad_file_save_as ( OCADFile pfile,
const char *  filename 
)

Saves an open OCADFile to the given filename.

Returns 0 on success, or one of the following error codes: -2: Unable to open file for writing errno was last set by open(2). -3: Unable to completely write data to the file. errno was last set by write(2).

◆ ocad_init()

int ocad_init ( )

Initializes the libocad library.

Currently, this only does some checks to determine that the library was compiled correctly and structures are packed appropriately. Returns 0 on success, or exits the current process on failure.

◆ ocad_shutdown()

int ocad_shutdown ( )

Shuts down the libocad library.

Currently this is a no-op, but is included as part of the API to support future enhancements. Processes that use the library should call this function when they exit. The function always returns 0 for success.