Skip to content

Minimal, safe and high level alternative to console-based string input (most of them teached from libc)

Notifications You must be signed in to change notification settings

ciroDourado/read

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

read

Due to the complexity that writing some code that expects a string input from the user, I've made this tiny library which can be used in any case (e.g.: the programmer doesn't need to worry anymore of how many characters/bytes the user must give, neither the boring details of handling NULL or memory allocation).

With these functions, you should only expect some string (any size) or nothing, that simple.

obs.: this is a refactored version. You can check my first attempt to code this at:

input_without_overflow

obs. 2: if you are interested in another tool for string manipulation, check the following repository. It's about extracting a slice from a given String.

slice

obs. 3: wanna check out a variadic version (more flexible way of giving args) of this function? Click on the link bellow that goes to another repository

read_variadic

Sections

  1. Applying into your code
  2. How to use it
  3. Contribuing

1. Applying into your code

Using this library requires nothing more than moving the source-code at src/ into your project diretory that hold your .c files.

Finally, just the obvious, use GCC to compile all the code inside :

gcc *.c -o [executable_name]

Execute with (if you are in a Windows OS, use a backslash instead):

./[executable_name]

Back into sections

2. How to use it

Function Parameter Return
read String literal to be printed before your reading process, null to print nothing String with user input
clean String reference Nothing

If you want to take an example, check the code inside test/, at main.c.

"read( )" expects some hardcoded string to be passed, which will serve as some sort of label. For example

printf("Say your name: \n");
char* yourName = read(" >>> ");

Will be printed as follows:

$ Say your name:
$  >>> Kimi no na wa

If you don't want any message, just pass as null:

printf("Say your name: \n");
char* yourName = read(NULL);
$ Say your name:
$ Kimi no na wa

"clean( )" only expects a dynamically-allocated string to be freed. If null is passed, nothing will happen.

Back into sections

3. Contribuing

If you want to leave a comment, suggest an update (or anything else), mail me: ciro dot brz at gmail dot com . Btw, I'm brazilian; my deepest apologies if I wrote something wrong, or mispelled something.

Back into sections

About

Minimal, safe and high level alternative to console-based string input (most of them teached from libc)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages