Skip to content

Instantly share code, notes, and snippets.

View alsamitech's full-sized avatar

Sami Alameddine alsamitech

  • Alsami Technologies
  • United States of America
View GitHub Profile
@alsamitech
alsamitech / file_count.c
Created August 7, 2021 07:11
file_count- lists the amount of files in a directory
// file_count will add one to the count because zero is reserved for errors
size_t file_count(const char* dir_nm){
DIR* dir=opendir(dir_nm);
if(!dir)return 0;
struct dirent* entity=readdir(dir);
size_t count=0;
for(;readdir(dir);count++);
closedir(dir);
return count;
}
@alsamitech
alsamitech / strcmp_until.c
Created July 27, 2021 04:30
strcmp_until - faster than doing strncmp with passing until_char
_Bool strcmp_until(const char* s1,const char* s2,const char until){
for(size_t i=0;s1[i]!=until;i++)
if(s1[i]!=s2[i])return 1;
return 0;
}
_Bool starts_with_word(const char* str, const char* word){
for(size_t i=0;;i++){
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')||str[i]=='_')){
if(word[i])return 1;
break;
}
if(str[i]!=word[i]){
return 1;
}
@alsamitech
alsamitech / starts_with_4.c
Created July 23, 2021 17:35
starts_with - starts with but even better (replace _Bool with char if you're using anything before C99)
_Bool starts_with(const char* in, const char* sw){
for(size_t i=0;sw[i];i++)
if(in[i]!=sw[i])
return 1;
return 0;
}
@alsamitech
alsamitech / get_last_ex.c
Created July 21, 2021 20:49
get_last_ex - get_last but throws excpetions
// returns position of the last byte in an array or string whose length is known.
size_t get_last_byte_ex(const char* const bytes, const char byte,size_t len, _Bool* const exists){
while(bytes[len]!=byte){
if(!len){
*exists=0;
return 0;
}
len--;
}
@alsamitech
alsamitech / get_last.c
Created July 21, 2021 15:18
returns position of last byte in a string or array of bytes
// returns position of the last byte in an array or string whose length is known.
size_t get_last_byte(const char* const bytes, const char byte,size_t len){
while(bytes[len]!=byte&&len)len--;
return len;
}
// returns position of the last character of a string whose length is not known.
size_t get_last_char(const char* const str, const char byte){
size_t pos=0;
for(size_t i=0;str[i];i++)
@alsamitech
alsamitech / fast_atokl_r1.c
Last active July 13, 2021 06:31
atokl that isn't security and error checking hell
char** atokl(const char* const InC, const char* const delim, long unsigned int* len){
// eight as that provides room for small splitting operations
long unsigned int capacity=8;
char** tok=(char**)malloc(capacity*sizeof(char**));
if(!tok)return 0x0;
tok[0]=strtok(strdup(InC), delim);
{
long unsigned int i=1;
while(tok[i-1]!=NULL){
void print_str_esc(const char* str, FILE* stream){
for(size_t i=0;str[i];i++){
switch(str[i]){
case '\n':
{
putc('\\', stream);
putc('n', stream);
break;
}
case '\t':
@alsamitech
alsamitech / read_text_file.c
Created July 8, 2021 16:46
read_text_file - quickly reads a text file from disk
char* read_text_file(const char* const filenm){
char* buf;
long unsigned int len;
FILE* const f=fopen(filenm, "r");
if(f){
fseek(f, 0, SEEK_END);
len=ftell(f);
fseek(f, 0, SEEK_SET);
buf=(char*)malloc(len+1);
if(buf)
@alsamitech
alsamitech / char_instance.c
Created July 6, 2021 04:24
char_instance - returns the position of a specified instance of ch in a Cstring
char* char_instance(char* src, char ch,size_t instance){
for(size_t c=0;*src&&c<instance;src++)
if(*src==ch)c++;
return src;
}