Robotran C Documentation
Functions
mbs_path.h File Reference

Go to the source code of this file.

Functions

int mbs_unix_separator (char *path)
 In-place replacement of windows-style path separator (\) by Unix separator (/). More...
 
char * find_user_path (const char *mbsfile)
 Find the user folder corresponding to the given *.mbs file. More...
 
int find_project_path (const char *mbsfile, char *prjpath)
 Find the project directory (containing dataR, symbolicR, ...) from Mbs filepath. More...
 
char * find_project_path_from_cwd (const char *optionnal_path, int verbose)
 Find the project directory from current working directory (cwd) or provided path. More...
 
char * explore_dir_for_project_path (const char *optionnal_path, int verbose)
 Find the project directory from current working directory (cwd) or provided path. More...
 
char * mbs_basename (const char *path, int verbose)
 mbs_basename returns the trailing part of the path (UNIX and MAC). More...
 
char * mbs_dirname (const char *path, int verbose)
 mbs_dirname returns the leading part of the path (UNIX and MAC). More...
 
int mbs_splitext (const char *path, char **root, char **ext)
 Split the path into a pair (root, ext). More...
 
int remove_prjpath (char **path, char *project_source_dir)
 If the path start with PRJPATH, an new path is allocated replacing it. More...
 
char * mbs_getcwd ()
 Return the Current Working Directory (cwd). More...
 
int mbs_is_dir_or_file (const char *path)
 Check if the path is valid and lead to a file or directory. More...
 
int mbs_makedirs (char *path)
 Create (recursively) the specified directory. More...
 

Function Documentation

◆ explore_dir_for_project_path()

char* explore_dir_for_project_path ( const char *  optionnal_path,
int  verbose 
)

Find the project directory from current working directory (cwd) or provided path.

The function parse the cwd or the provided path until finding either:

  • a folder containing a subfolder named "dataR";
  • a folder containing a subfolder named "workR"; then the folder its returned (without the subfolder);

The caller is responsible for memory freeing.

Parameters
[in]optionnal_pathIf not NULL, this path is used to search the project path. If the path starts with ".", the current working directory (see function mbs_getcwd()) is added as prefix.
[in]verboseVerbosity level, from error only (0) to all (2).
Returns
The path to the project directory or NULL in case of error.

◆ find_project_path()

int find_project_path ( const char *  mbsfile,
char *  prjpath 
)

Find the project directory (containing dataR, symbolicR, ...) from Mbs filepath.

The function analyze the path by splitting at separator ("/" or "\\") and analyze if the mbs filepath is located in "dataR" folder

If the provided filename:

  • only contain the mbs file (with or without separator), the project directory is assumed to be the parent ("..");
  • starts with "dataR" (with or without separator), the returned directory path is an empty string ("\0");

prjpath should be large enough to contain at least mbsfile.

Parameters
[in]mbsfilethe path and filename to the .mbs file
[out]prjpaththe path to the root of the mbs project
Returns
err <0 if error occurs

◆ find_project_path_from_cwd()

char* find_project_path_from_cwd ( const char *  optionnal_path,
int  verbose 
)

Find the project directory from current working directory (cwd) or provided path.

The function parse the cwd or the provided path until finding a folder named "workR" or "dataR". The project directory is assumed to be the parent of the located folder.

The caller is responsible for memory freeing.

Parameters
[in]optionnal_pathIf not NULL, this path is used to search the project path. If the path starts with ".", the current working directory (see function mbs_getcwd()) is added as prefix.
[in]verboseVerbosity level, from error only (0) to all (2).
Returns
The path to the project directory or NULL in case of error.

◆ find_user_path()

char* find_user_path ( const char *  mbsfile)

Find the user folder corresponding to the given *.mbs file.

This function allocate a char* that must be freed by the user.

Parameters
[in]mbsfilethe path and filename to the .mbs file
Returns
pointer to char array containing the path to the userfctR folder.

◆ mbs_basename()

char* mbs_basename ( const char *  path,
int  verbose 
)

mbs_basename returns the trailing part of the path (UNIX and MAC).

If the path ends on a folder, it returns the terminal folder. If it is a file it returns the filename and its extension.

If the path is the root folder it returns '/'. However under Windows we do not check if the path only contains the path to a drive (such as 'C:/').

Parameters
pathThe path from which the basename must be extracted.
[in]verboseVerbosity level, from error only (0) to all (2).
Returns
The base name of the given path.

◆ mbs_dirname()

char* mbs_dirname ( const char *  path,
int  verbose 
)

mbs_dirname returns the leading part of the path (UNIX and MAC).

If the path ends on a folder, it returns the previous folder. If it is a file it returns the path to the folder containing the file.

If the path is the root folder it returns '/'. However under Windows we do not check if the path only contains the path to a drive (such as 'C:/').

If the given path does not contain separator, the current directory (".") is returned.

Parameters
[in]pathThe path from which the leading part must be extracted.
[in]verboseVerbosity level, from error only (0) to all (2).
Returns
A pointer to the leading part of the path, the caller must free the returned memory.

◆ mbs_getcwd()

char* mbs_getcwd ( )

Return the Current Working Directory (cwd).

The caller is responsible for memory freeing.

This function recursively increase the buffer to store the results of the system getcwd (or _getcwd for Windows) until a sufficient buffer size is reached.

Returns
NULL if system function encounter an errno different of ERANGE (buffer too small) or the cwd.

◆ mbs_is_dir_or_file()

int mbs_is_dir_or_file ( const char *  path)

Check if the path is valid and lead to a file or directory.

Parameters
[in]pathPointer to the path to be checked.
Returns
Status:
  • 2: The path leads to an existing file.
  • 1: The path leads to an existing directory.
  • 0: The path is invalid (nothing exists).
  • -1: Invalid argument.
  • -2: Unexpected error in system function.
  • -3: The path is valid but is neither a directory nor file.

◆ mbs_makedirs()

int mbs_makedirs ( char *  path)

Create (recursively) the specified directory.

On Unix system permission are "0755" (only owner can write, everybody can read and execute).

Parameters
[in]pathThe directory to be created.
Returns
Status:
  • 0: Normal exists, folder already exists or has been created;
  • -1: Specified error, read message;
  • -2: Unspecified error.

◆ mbs_splitext()

int mbs_splitext ( const char *  path,
char **  root,
char **  ext 
)

Split the path into a pair (root, ext).

A the exit, root + ext == path, and the extension, ext, is an empty string or begins with a period and contains at most one period.

If root of ext is NULL, the corresponding part of path will not be returned.

Both root and ext are pointer to char pointer, the 'char' memory will be allocated by this function. If they already points to previously allocated memory, it will be freed to avoid leak.

The path separator is uniformized to unix style.

Parameters
[in]pathThe full path.
[in,out]rootPointer to pointer to store the root of the path.
[in,out]extPointer to pointer to store the extension of the path.
Returns
0 for normal output, #_MBS_ERR_LOW_FILES in case of error.

◆ mbs_unix_separator()

int mbs_unix_separator ( char *  path)

In-place replacement of windows-style path separator (\) by Unix separator (/).

Parameters
[in,out]pathThe path to be adapted, modifications occurs in-place.
Returns
0 for normal output, #_MBS_ERR_LOW_FILES for error.

◆ remove_prjpath()

int remove_prjpath ( char **  path,
char *  project_source_dir 
)

If the path start with PRJPATH, an new path is allocated replacing it.

The path separator are not replaced by unix-style.

Parameters
[in,out]pathPointer to the pointer of the path to be cleared. The path separator after "PRJPATH" must be present.
[in]project_source_dirThe path of the project source dir, must not contains the final separator.
Returns
#MBS_INFO_SUCCESS for normal output, #MBS_INFO_FAILURE value for error.