Git Product home page Git Product logo

path_module's Introduction

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);
}

path_module's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.