|
| 1 | +## STDGET - A User-Friendly C String Input Library |
| 2 | +#### Overview π |
| 3 | +The stdget library provides a simple yet robust set of functions for acquiring string input from the user in C programming. Designed to be more user-friendly than standard C input functions, `stdget` offers a dynamic `getstrf()` function that supports printf-like formatting for prompts and automatically handles dynamic memory allocation for user input. |
| 4 | +#### Features β¨ |
| 5 | + * **Formatted Prompts:** Display prompts to the user using familiar printf-style format strings. |
| 6 | + * **Dynamic Input Buffer:** Automatically expands the input buffer to accommodate strings of any length, preventing buffer overflows. |
| 7 | + * **Cross-Platform Line Ending Handling:** Gracefully handles common line endings (`\n`, `\r\n`). |
| 8 | + * **Robust Error Handling:** Returns NULL on memory allocation failures or immediate End-Of-File (EOF). |
| 9 | + * **Simplified String Type:** Introduces a `str` type alias for `char *` for enhanced readability. |
| 10 | + |
| 11 | +### Installation π οΈ |
| 12 | +To use the `stdget` library in your C projects, follow these steps to compile it into a static library (`.a` file) and link it with your applications. |
| 13 | + |
| 14 | +**1. Download the Library Files π₯** |
| 15 | + |
| 16 | +Download the latest version of the stdget library header (`getstr.h`) and source (`getstr.c`) files from the official GitHub repository: [GITHUB/LIBGETSTR](https://github.com/codebysumit/libgetstr/tree/master/src) |
| 17 | + |
| 18 | +Make sure both `getstr.h` and `getstr.c` are placed in the same directory. |
| 19 | + |
| 20 | +**2. Compile the Source Code βοΈ** |
| 21 | + |
| 22 | +Navigate to the directory where you saved `getstr.c` and `getstr.h` in your terminal or command prompt. Compile the `getstr.c` file using GCC (or your preferred C compiler): |
| 23 | + |
| 24 | +```bash |
| 25 | +gcc -c getstr.c |
| 26 | +``` |
| 27 | + |
| 28 | +This command creates a compiled object file named `getstr.o` in the same directory. |
| 29 | + |
| 30 | +**3. Create the Static Library π¦** |
| 31 | + |
| 32 | +Use the `ar` (GNU Binutils) tool to create an archive static library file. On Windows, MinGW compilers typically have `ar` installed by default. |
| 33 | + |
| 34 | +```bash |
| 35 | +ar rcs libgetstr.a getstr.o |
| 36 | +``` |
| 37 | + |
| 38 | +This command creates a static library file named `libgetstr.a`. |
| 39 | + |
| 40 | +Note: You can now delete the `getstr.o` file as it's no longer needed after creating the static library. |
| 41 | + |
| 42 | +**4. Organize File Structure (Optional but Recommended) ποΈ** |
| 43 | + |
| 44 | +For better project organization, you can create a specific directory structure to manage your library files: |
| 45 | + |
| 46 | +```bash |
| 47 | +. |
| 48 | +βββ include/ |
| 49 | +β βββ getstr.h |
| 50 | +βββ lib/ |
| 51 | +β βββ libgetstr.a |
| 52 | +βββ your_application.c |
| 53 | +``` |
| 54 | + |
| 55 | +Copy the `getstr.h` header file to the `include` directory. Move the static library `libgetstr.a` to the `lib` directory. |
| 56 | + |
| 57 | +**5. Compile Your Application with the Library π»** |
| 58 | + |
| 59 | +When compiling your application (`your_application.c` in the example above) that uses `stdget`, you need to tell the compiler where to find the header files and the library file. |
| 60 | + |
| 61 | +```bash |
| 62 | +gcc -I./include -L./lib your_application.c -o your_application -lgetstr |
| 63 | +``` |
| 64 | +**Explanation of flags:** |
| 65 | + * **`-I./include`:** Specifies the directory containing the header files (here, `./include`). |
| 66 | + * **`-L./lib`:** Specifies the directory containing the libraries (here, `./lib`). |
| 67 | + * **`your_application.c`:** The source code file of your application. |
| 68 | + * **`-o your_application`:** Sets the output executable file name to `your_application`. |
| 69 | + * **`-lgetstr`:** Links the program with the static library `libgetstr.a` (the `-l` flag expects the library name without the `lib` prefix and `.a` suffix). |
| 70 | + |
| 71 | +### Usage π‘ |
| 72 | +Once installed, you can include `getstr.h` in your C source files and use the `getstrf()` function. |
| 73 | + |
| 74 | +**Example:** |
| 75 | + |
| 76 | +Here's a simple example demonstrating how to use `getstrf()`: |
| 77 | + |
| 78 | +Link with `-lgetstr`. |
| 79 | + |
| 80 | +```c |
| 81 | +#include <getstr.h> |
| 82 | +#include <stdio.h> |
| 83 | + |
| 84 | + |
| 85 | +int main() |
| 86 | +{ |
| 87 | + str string = getstrf("Prompt: "); |
| 88 | + printf("Output string: %s!\n", string); |
| 89 | + return 0; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +**Running the Example** |
| 94 | + |
| 95 | +After compiling the example (e.g., as test using the installation steps), run it from your terminal: |
| 96 | + |
| 97 | +```bash |
| 98 | +gcc -I./include -L./lib test.c -o test -lgetstr |
| 99 | +``` |
| 100 | + |
| 101 | +```bash |
| 102 | +./test |
| 103 | +``` |
| 104 | + |
| 105 | +**Example Output:** |
| 106 | + |
| 107 | +```bash |
| 108 | +Prompt: This is a String. |
| 109 | +Output string: This is a String. |
| 110 | +``` |
| 111 | + |
| 112 | +### Memory Management πΎ |
| 113 | + |
| 114 | +The `getstrf()` function dynamically allocates memory on the heap to store the user's input. It is crucial that you `free()` this memory using `free()` from `stdlib.h` once you are finished with the string to prevent memory leaks. |
| 115 | + |
| 116 | +**Example:** |
| 117 | +```c |
| 118 | +#include <getstr.h> // For str keyword and getstrf() |
| 119 | +#include <stdio.h> // For printf() |
| 120 | +#include <stdlib.h> // For free() |
| 121 | + |
| 122 | + |
| 123 | +int main() |
| 124 | +{ |
| 125 | + str string = getstrf("Prompt: "); |
| 126 | + printf("Before using free(): %s\n", string); |
| 127 | + free(string); |
| 128 | + printf("After using free(): %s\n", string); |
| 129 | + return 0; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Reference π |
| 134 | + |
| 135 | +For a visual guide on compiling and linking C libraries, you can refer to this YouTube video tutorial: [Elatronion](https://youtu.be/m6RVY84_mQE) |
| 136 | + |
| 137 | +### License π |
| 138 | + |
| 139 | +This project is licensed under the MIT License - see the LICENSE section above for details. |
| 140 | + |
| 141 | +### Contributing π€ |
| 142 | + |
| 143 | +Contributions are welcome! If you find bugs, have suggestions for improvements, or would like to contribute new features, please open an issue or submit a pull request on the GitHub repository. |
| 144 | + |
| 145 | +### Contact π¬ |
| 146 | + |
| 147 | +For any questions or feedback, please reach out to the project maintainer: |
| 148 | + * Project Maintainer π¨βπ»: Sumit Das |
| 149 | + * Email βοΈ: [codebysumitdas@gmail.com](mailto:codebysumit@gmail.com) |
0 commit comments