Here is a code file (I use it as a stand alone header file to be included in my projects) that contains macros to generate ANSI encoded strings that when printed are colorized. Also, the module contains useful string functions that are found in PHP standard library: strpos, stripos and substring functions: substr, subnstr.
I would like this module to be reviewed for glitches because I was unable to find any but as soon as I think that someone will hopefully find one so I can fix it!
#ifndef __JP__STRINGS_
#define __JP__STRINGS_
/* AUTHOR JACOB PSIMOS 2016 */
/* [email protected] */
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
/*
Colorize String Macros
Use like this:
char buffer[26]; //Note at least +11 extra buffer size to store ANSI color code
GREEN("hello world!", buffer, 26);
printf("%s\n", buffer);
*/
#define RED(s,b,bs) snprintf(b,bs,"\33[0;31m%s\33[0m",s)
#define GREEN(s,b,bs) snprintf(b,bs,"\33[0;32m%s\33[0m",s)
#define YELLOW(s,b,bs) snprintf(b,bs,"\33[0;33m%s\33[0m",s)
#define BLUE(s,b,bs) snprintf(b,bs,"\33[0;34m%s\33[0m",s)
#define MAGENTA(s,b,bs) snprintf(b,bs,"\33[0;35m%s\33[0m",s)
#define CYAN(s,b,bs) snprintf(b,bs,"\33[0;36m%s\33[0m",s)
#define LIGHT_RED(s,b,bs) snprintf(b,bs,"\33[1;31m%s\33[0m",s)
#define LIGHT_GREEN(s,b,bs) snprintf(b,bs,"\33[1;32m%s\33[0m",s)
#define LIGHT_YELLOW(s,b,bs) snprintf(b,bs,"\33[1;33m%s\33[0m",s)
#define LIGHT_BLUE(s,b,bs) snprintf(b,bs,"\33[1;34m%s\33[0m",s)
#define LIGHT_MAGENTA(s,b,bs) snprintf(b,bs,"\33[1;35m%s\33[0m",s)
#define LIGHT_CYAN(s,b,bs) snprintf(b,bs,"\33[1;36m%s\33[0m",s)
/* subnstr - substring
* Fills char *out with a substring from char *str starting at 'start' for 'count' chars
* Returns the length of the substring
* bufsize = maximum size of *out buffer
*/
const size_t subnstr(const char *str, char *out, const size_t bufsize, const size_t start, const size_t count){
if(bufsize < 1 || out == NULL || str == NULL){
return -1;
}
const size_t len = strlen(str);
size_t i;
size_t pos = 0;
for(i = start; pos < count && i < len && pos < bufsize - 1; i++){
out[pos] = str[i];
pos++;
}
out[pos] = '\0';
return (size_t)pos;
}
/* substr - substring
* Fills char *out with a substring from char *str starting at size_t start until the end of char *str
* Returns the length of the substring
* bufsize = maximum size of *out buffer
*/
const size_t substr(const char *str, char *out, const size_t bufsize, const size_t start){
if(str == NULL || out == NULL || bufsize < 1) {
return -1;
}
const size_t len = strlen(str);
size_t i;
size_t pos = 0;
for(i = start; i < len && pos < bufsize - 1; i++){
out[pos] = str[i];
pos++;
}
out[pos] = '\0';
return (size_t)pos;
}
/* strpos - string position
* Returns the position of 'needle' within 'haystack' starting at 'skip' or returns -1 if non existant
* or buffer position does not fall within 'bufsize' bounds
*/
const size_t strpos(const char *needle, const char *haystack, const size_t bufsize, const size_t skip){
if(skip <= bufsize - strlen(needle)){
const char *ptr = strstr(haystack + skip, needle);
const size_t pos = ptr != NULL ? (size_t)((unsigned long)ptr - (unsigned long)haystack) : -1;
return (size_t)(pos > bufsize || pos < (unsigned long)haystack ? -1 : pos);
}
return -1;
}
/* stripos - case insensitive string position
* Returns the position of 'needle' within 'haystack' starting at 'skip' or returns -1 if non existant
* or buffer position does not fall within 'bufsize' bounds
* bufsize - maximum size of haystack buffer (prevent out of bounds)
*/
const size_t stripos(const char *needle, const char *haystack, const size_t bufsize, const size_t skip){
if(skip <= bufsize - strlen(needle)){
const char *ptr = (const char*)strcasestr(haystack + skip, needle);
const size_t pos = ptr != NULL ? (size_t)((unsigned long)ptr - (unsigned long)haystack) : -1;
return (size_t)(pos > bufsize || pos < (unsigned long)haystack ? -1 : pos);
}
return -1;
}
const short strequals(const char *str1, const char *str2){
int l1 = strlen(str1);
int l2 = strlen(str2);
if(l1 != l2){ return 0; }
int i;
for(i = 0; i < l1; i++){
if(str1[i] != str2[i]){
return 0;
}
}
return 1;
}
#endif