Simple and lightweight pathname parser for C. This module helps to parse dirname, basename, filename and file extension .

Path Module For C

File name and extension parsing functionality are removed because it's difficult to distinguish between a hidden dir (ex: .git) and filename. Their code are not completely removed only commented. So if still you want use that functionality uncomment and use it.

Building

If you want to use this on your project just place path.c and path.h then compile it along with your project. But if you want test it before using it on your project clone this repo and their is a Makefile from where you can compile the source and test file together.

  • clone this repo
    git clone https://github.com/PrajwalCH/path_module
    cd path_module
  • Compile it by running make or for debug build make DEBUG=1

API

There are only 2 functions. one for parsing and one for debug printing the path structure.

// path structure
struct Path {
    char *dirname;
    char *basename;
    size_t dirname_len;
    size_t basename_len;
};

struct Path path_parse(char *pathname);
void path_dbgln(char *pathname, struct Path *path);

Examples

There is also a test file in src dir where you can see example.

// main.c

#include "path.h"

int main(void)
{
    char *pathname = "/projects/path_module/path.c";

    struct Path path = path_parse(pathname);
    path_dbgln(pathname, &path);
}

Output: example-output

If you want to print or copy individual parts by yourself..

int main(void)
{
    char *pathname = "/projects/path_module/path.c";

    struct Path path = path_parse(pathname);

    // there is no need to use length while printing for basename but it's recommend to use if want to copy on another buffer for safe.

    printf("dirname: %.*s\n", (int)path.dirname_len, path.dirname);
    printf("basename: %s\n", path.basename);

    // copy to another buffer
    char dirname[path.dirname_len + 1];
    memset(dirname, 0, sizeof(dirname));

    memcpy(dirname, path.dirname, path.dirname_len);
    printf("your dirname: %s\n", dirname);
}
Similar Resources

An asynchronous directory file change watcher module for Windows, macOS and Linux wrapped for V

A V module for asynchronously watching for file changes in a directory. The module is essentially a wrapper for septag/dmon. It works for Windows, macOS and Linux.

Dec 14, 2022

Python module to reduce a cmake file to an AST

CMake AST Status Travis CI (Ubuntu) AppVeyor (Windows) Coverage PyPI Licence cmake-ast has been tested against every single CMake module that ships wi

Sep 14, 2022

MiniCalculator with a simple parser.

MiniCalculator with a simple parser.

MiniCalculator with a simple parser. This is a homework-expanded project. To learn something about parser and basic theory of programmi

Oct 9, 2021

A simple YAML parser which produces a Node Tree Object representation of YAML Documents

A simple YAML parser which produces a Node Tree Object representation of YAML Documents and includes a find method to locate individual Nodes within the parsed Node Tree.

Sep 18, 2022

Extension to Keyboard.h to allow non-US keyboards and Unicode characters

KeyboardUTF8 This Arduino library adds a mapping layer on top of the Keyboard library (for Leonardo/Micro/Due) to allow non-US keyboards and 'typing'

Sep 4, 2022

Documenting the development of a simple first module.

Your First Module This guide will look at writing a complete module, with many common features in a reduced form. This includes the module initialisat

Jun 3, 2021

convert elf file to single c/c++ header file

elf-to-c-header Split ELF to single C/C++ header file

Nov 4, 2021

tiny recursive descent expression parser, compiler, and evaluation engine for math expressions

tiny recursive descent expression parser, compiler, and evaluation engine for math expressions

TinyExpr TinyExpr is a very small recursive descent parser and evaluation engine for math expressions. It's handy when you want to add the ability to

Dec 30, 2022

Very fast Markdown parser and HTML generator implemented in WebAssembly, based on md4c

Very fast Markdown parser and HTML generator implemented in WebAssembly, based on md4c

Dec 24, 2022
AlleyWind is an advanced Win32-based and open-sourced utility that helps you to manage system's windows
AlleyWind is an advanced Win32-based and open-sourced utility that helps you to manage system's windows

AlleyWind AlleyWind is an advanced Win32-based and open-sourced utility that helps you to manage system's windows. AlleyWind could: Displays a graphic

Oct 20, 2022
Small header-only C++ library that helps to initialize Vulkan instance and device object

Vulkan Extensions & Features Help, or VkExtensionsFeaturesHelp, is a small, header-only, C++ library for developers who use Vulkan API.

Oct 12, 2022
parse sql statements as strings to be used/modified in cpp.

parse sql statements as strings to be used/modified in cpp.

Mar 28, 2022
Small Header only library to parse argv for flags

Small Header only library to parse argv for flags

Nov 9, 2022
pg_cron is a simple cron-based job scheduler for PostgreSQL that runs inside the database as an extension.
pg_cron is a simple cron-based job scheduler for PostgreSQL that runs inside the database as an extension.

pg_cron is a simple cron-based job scheduler for PostgreSQL (9.5 or higher) that runs inside the database as an extension.

Jan 3, 2023
ini file parser

Iniparser 4 I - Overview This modules offers parsing of ini files from the C level. See a complete documentation in HTML format, from this directory o

Jan 1, 2023
Small configuration file parser library for C.

libConfuse Introduction Documentation Examples Build & Install Origin & References Introduction libConfuse is a configuration file parser library writ

Dec 14, 2022
A PE parser written as an exercise to study the PE file structure.

Description A PE parser written as an exercise to study the PE file structure. It parses the following parts of PE32 and PE32+ files: DOS Header Rich

Nov 18, 2022
A TreeSitter parser for the Neorg File Format

NFF TreeSitter Parser A TreeSitter grammar for Neorg. Available Commands Command Result yarn installs needed dependencies (only do if you don't have t

Dec 7, 2022
Locate the current executable and the current module/library on the file system

Where Am I? A drop-in two files library to locate the current executable and the current module on the file system. Supported platforms: Windows Linux

Dec 27, 2022