Code Monkey home page Code Monkey logo

strid's Introduction

STRID

Generate random id 10 char long, by default. Uses base 36 encoding and include a checksum. It can be useful for application that need to send id through HTTP or any character-based protocol.

Usage

Initialize the random number generator (use drand48()) by passing any string to strid_init(). Initialization with a static string will produce predictible results. You don't need to initialize, just you should.

int main(void) {
    Strid myId;

    strid_init("Not So Random String");
    strid_generate_random(myId);

    printf("%s\n", myId);

    return 0;
}

You can prefix your ID with strid_generate_prefix().

int main(void) {
	/* ... */
	
	strid_generate_prefix(myId, "hostname.example.com");
	
	/* ... */
}

If the id comes from an extern source, you might want to verify it.

int main(void) {
    /* ... */

    if(strid_valid(externId)) {
        printf("Valid ID");
    }

    /* ... */
}

Two id can be compared with strcmp but, in case characters have been lowered, you might well use strid_equal.

int main(void) {

    /* ... */

    if(strid_equal(Id1, Id2)) {
        printf("ID1 equals ID2");
    }

    /* ... */
    
}

Please note that strid_equal doesn't validate the id at any point. So check first both ids with strid_valid and then strid_equal.

You can copy an id into another id with strid_copy.

int main(void) {
    /* ... */

   strid_copy(idDest, idSource);

   /* ... */
}

It's just a memcpy, see in strid.h :

#define strid_copy(dest, src)           memcpy((dest), (src), STRID_MAX_SIZE)

License

BSD 2-clauses, see LICENSE.txt

strid's People

Contributors

artnum avatar

Watchers

 avatar

Forkers

qingchen1984

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.