#include #include #include #include #include #include #include #include #include "utils.h" #include "defines.h" /////////////////////////////////////// int prefixMatch(char* pre, char* str) { return (strncasecmp(pre,str,strlen(pre))==0); } int suffixMatch(char* suf, char* str) { int len = strlen(suf); int offset = strlen(str)-len; return (offset>=0 && strncasecmp(suf, str+offset, len)==0); } int exactMatch(char* str1, char* str2) { int len1 = strlen(str1); if (len1!=strlen(str2)) return 0; return (strncmp(str1,str2,len1)==0); } int hide(char* file_name) { return file_name[0]=='.'; } void getDisplayName(const char* in_name, char* out_name) { char* tmp; strcpy(out_name, in_name); // extract just the filename if necessary tmp = strrchr(in_name, '/'); if (tmp) strcpy(out_name, tmp+1); // remove extension tmp = strrchr(out_name, '.'); if (tmp && strlen(tmp)<=4) tmp[0] = '\0'; // 3 letter extension plus dot // remove trailing parens (round and square) char safe_name[256]; strcpy(safe_name,out_name); while ((tmp=strrchr(out_name, '('))!=NULL || (tmp=strrchr(out_name, '['))!=NULL) { if (tmp==out_name) break; tmp[0] = '\0'; tmp = out_name; } // make sure we haven't nuked the entire name if (out_name[0]=='\0') strcpy(out_name, safe_name); // remove trailing whitespace tmp = out_name + strlen(out_name) - 1; while(tmp>out_name && isspace((unsigned char)*tmp)) tmp--; tmp[1] = '\0'; } void getEmuName(const char* in_name, char* out_name) { // NOTE: both char arrays need to be MAX_PATH length! char* tmp; strcpy(out_name, in_name); tmp = out_name; // extract just the Roms folder name if necessary if (prefixMatch(ROMS_PATH, tmp)) { tmp += strlen(ROMS_PATH) + 1; char* tmp2 = strchr(tmp, '/'); if (tmp2) tmp2[0] = '\0'; } // finally extract pak name from parenths if present tmp = strrchr(tmp, '('); if (tmp) { tmp += 1; strcpy(out_name, tmp); tmp = strchr(out_name,')'); tmp[0] = '\0'; } } void normalizeNewline(char* line) { int len = strlen(line); if (len>1 && line[len-1]=='\n' && line[len-2]=='\r') { // windows! line[len-2] = '\n'; line[len-1] = '\0'; } } void trimTrailingNewlines(char* line) { int len = strlen(line); while (len>0 && line[len-1]=='\n') { line[len-1] = '\0'; // trim newline len -= 1; } } /////////////////////////////////////// int exists(char* path) { return access(path, F_OK)==0; } void touch(char* path) { close(open(path, O_RDWR|O_CREAT, 0777)); } void putFile(char* path, char* contents) { FILE* file = fopen(path, "w"); if (file) { fputs(contents, file); fclose(file); } } void getFile(char* path, char* buffer, size_t buffer_size) { FILE *file = fopen(path, "r"); if (file) { fseek(file, 0L, SEEK_END); size_t size = ftell(file); if (size>buffer_size-1) size = buffer_size - 1; rewind(file); fread(buffer, sizeof(char), size, file); fclose(file); buffer[size] = '\0'; } } char* allocFile(char* path) { // caller must free! char* contents = NULL; FILE *file = fopen(path, "r"); if (file) { fseek(file, 0L, SEEK_END); size_t size = ftell(file); contents = calloc(size+1, sizeof(char)); fseek(file, 0L, SEEK_SET); fread(contents, sizeof(char), size, file); fclose(file); contents[size] = '\0'; } return contents; } int getInt(char* path) { int i = 0; FILE *file = fopen(path, "r"); if (file!=NULL) { fscanf(file, "%i", &i); fclose(file); } return i; } void putInt(char* path, int value) { char buffer[8]; sprintf(buffer, "%d", value); putFile(path, buffer); } uint64_t getMicroseconds(void) { uint64_t ret; struct timeval tv; gettimeofday(&tv, NULL); ret = (uint64_t)tv.tv_sec * 1000000; ret += (uint64_t)tv.tv_usec; return ret; }