s_string String_insert(char* word) { s_string s_temp; s_temp.input = mem_alloc(sizeof(char)*String_length(word),id_string); memcpy(s_temp.input,word,sizeof(char)*String_length(word)); s_temp.length = String_length(s_temp.input); s_temp.size = s_temp.length; return s_temp; }
String File_appendFileNameBuffer(String fileName, const char *buffer, ulong bufferLength) { assert(fileName != NULL); if (String_length(fileName) > 0) { if (String_index(fileName,String_length(fileName)-1) != FILES_PATHNAME_SEPARATOR_CHAR) { String_appendChar(fileName,FILES_PATHNAME_SEPARATOR_CHAR); } } String_appendBuffer(fileName,buffer,bufferLength); return fileName; }
String File_appendFileNameChar(String fileName, char ch) { assert(fileName != NULL); if (String_length(fileName) > 0) { if (String_index(fileName,String_length(fileName)-1) != FILES_PATHNAME_SEPARATOR_CHAR) { String_appendChar(fileName,FILES_PATHNAME_SEPARATOR_CHAR); } } String_appendChar(fileName,ch); return fileName; }
s_string Input() { s_string s_input; int32_t c; char* i_temp_ptr; int32_t i_buffer = 64; int32_t i = 0; i_temp_ptr = mem_alloc(sizeof(char)*i_buffer,id_string); printf("input fce! ---------------- \n"); while((c = getchar()) != '\n') { if( i == i_buffer - 2) { i_buffer*=2; i_temp_ptr = mem_realloc(i_temp_ptr,sizeof(char)*i_buffer); } i_temp_ptr[i] = (char)c; i++; i_temp_ptr[i] = '\0'; } s_input.input = i_temp_ptr; s_input.length = String_length(s_input.input); s_input.size = i_buffer; return s_input; }
ulong chunks_getSize(const void *data, int definition[]) { int z; ulong size; assert(data != NULL); assert(definition != NULL); size = 0; for (z = 0; definition[z] != 0; z++) { switch (definition[z]) { case CHUNK_DATATYPE_UINT8: case CHUNK_DATATYPE_INT8: size += 1; data = ((char*)data) + 4; break; case CHUNK_DATATYPE_UINT16: case CHUNK_DATATYPE_INT16: size += 2; data = ((char*)data) + 4; break; case CHUNK_DATATYPE_UINT32: case CHUNK_DATATYPE_INT32: size += 4; data = ((char*)data) + 4; break; case CHUNK_DATATYPE_UINT64: case CHUNK_DATATYPE_INT64: size += 8; data = ((char*)data) + 8; break; case CHUNK_DATATYPE_NAME: { String s; s = (*((String*)data)); assert(s != NULL); size += 2 + String_length(s); //pointer size??? data = ((char*)data) + 4; } break; case CHUNK_DATATYPE_DATA: break; default: HALT_INTERAL_ERROR_UNHANDLED_SWITCH_CASE(); break; } } return size; }
int main() { String s1 = String_new("abcd"); String s2 = String_new("xyz"); String s3 = String_concat(s1, s2); String_free(s1); String s4 = String_copy(s2); String_free(s2); char *c1 = String_toCharArray(s3); char *c2 = String_toCharArray(s4); if (String_length(s3) > String_length(s4)) printf("%s is longer than %s\n", c1, c2); else printf("%s is longer than %s\n", c2, c1); if (String_compare(s3, s4) > 0) printf("%s is greater than %s\n", c1, c2); else if (String_compare(s3, s4) < 0) printf("%s is greater than %s\n", c2, c1); else printf("%s and %s are the same\n", c2, c1); }
static void Lua_Init() { log_info("Loading IO streams"); char *Path = malloc(sizeof(char) * (String_length(Executable_Path) - String_length(Project_Name) + String_length("Input.txt"))); memcpy(Path, Executable_Path, sizeof(char) * (String_length(Executable_Path) - String_length(Project_Name))); Path[String_length(Executable_Path) - String_length(Project_Name)] = '\0'; String_Add(Path, "Log.txt"); Log = fopen(Path, "w+"); String_Remove(Path, "Log.txt"); String_Add(Path, "Input.txt"); Input = fopen(Path, "rb"); if(!Input) { Input = fopen(Path, "w"); fclose(Input); Input = fopen(Path, "rb"); } log_info("Loading Lua"); Lua_State = luaL_newstate(Log, Input); /* opens Lua */ luaL_openlibs(Lua_State); log_info("Loading Andrua's libs"); LuaLibrary_Load(Log); log_info("Finished Loading in Lua"); Lua_requestClose = false; }
LOCAL bool readProcessIO(int fd, String line) { #if defined(PLATFORM_LINUX) int n; #elif defined(PLATFORM_WINDOWS) u_long n; #endif /* PLATFORM_... */ char ch; do { // check if data available #if defined(PLATFORM_LINUX) ioctl(fd,FIONREAD,&n); #elif defined(PLATFORM_WINDOWS) // NYI ??? #warning not implemented #endif /* PLATFORM_... */ // read data until EOL found while (n > 0) { if (read(fd,&ch,1) == 1) { switch (ch) { case '\n': case '\r': case '\b': if (String_length(line) > 0L) return TRUE; break; default: String_appendChar(line,ch); break; } n--; } else { n = 0; } } } while (n > 0); return FALSE; }
Errors File_printLine(FileHandle *fileHandle, const char *format, ... ) { String line; va_list arguments; Errors error; assert(fileHandle != NULL); assert(fileHandle->file != NULL); assert(format != NULL); /* initialise variables */ line = String_new(); /* format line */ va_start(arguments,format); String_vformat(line,format,arguments); va_end(arguments); /* write line */ error = File_write(fileHandle,String_cString(line),String_length(line)); if (error != ERROR_NONE) { String_delete(line); return error; } error = File_write(fileHandle,"\n",1); if (error != ERROR_NONE) { String_delete(line); return error; } /* free resources */ String_delete(line); return ERROR_NONE; }
void Password_setString(Password *password, const String string) { uint length; #ifdef HAVE_GCRYPT #else /* not HAVE_GCRYPT */ uint z; #endif /* HAVE_GCRYPT */ assert(password != NULL); length = MIN(String_length(string),MAX_PASSWORD_LENGTH); #ifdef HAVE_GCRYPT memcpy(password->data,String_cString(string),length); #else /* not HAVE_GCRYPT */ for (z = 0; z < length; z++) { password->data[z] = String_index(string,z)^obfuscator[z]; } #endif /* HAVE_GCRYPT */ password->data[length] = '\0'; password->length = length; }
LOCAL bool readProcessIO(int fd, String line) { int n; char ch; do { // check if data available ioctl(fd,FIONREAD,&n); // read data until EOL found while (n > 0) { if (read(fd,&ch,1) == 1) { switch (ch) { case '\n': case '\r': case '\b': if (String_length(line) > 0L) return TRUE; break; default: String_appendChar(line,ch); break; } n--; } else { n = 0; } } } while (n > 0); return FALSE; }
Errors File_writeLine(FileHandle *fileHandle, const String line ) { Errors error; assert(fileHandle != NULL); assert(fileHandle->file != NULL); assert(line != NULL); error = File_write(fileHandle,String_cString(line),String_length(line)); if (error != ERROR_NONE) { return error; } error = File_write(fileHandle,"\n",1); if (error != ERROR_NONE) { return error; } return ERROR_NONE; }
void Terminal_printString(const String s) { for (u32 i = 0; i < String_length(s); i++) Terminal_printChar(s[i]); }
int main(int argc, const char *argv[]) { Definition definition; Definition definitions[MAX_DEFINITIONS]; uint definitionCount; int z; struct stat statBuffer; uint64 size; const char *inputFileName; FILE *inputHandle; uint deleteCount; byte data; uint64 n; /* parse command line */ CmdOption_init(COMMAND_LINE_OPTIONS,SIZE_OF_ARRAY(COMMAND_LINE_OPTIONS)); if (!CmdOption_parse(argv,&argc, COMMAND_LINE_OPTIONS,SIZE_OF_ARRAY(COMMAND_LINE_OPTIONS), CMD_PRIORITY_ANY, stderr,NULL ) ) { return 1; } if (versionFlag) { printf("Destroyer version %s\n",VERSION); return 0; } if (helpFlag) { printUsage(argv[0]); return 0; } if (argc <= 1) { fprintf(stderr,"ERROR: No input file given!\n"); exit(1); } inputFileName = argv[1]; /* get file size */ if (stat(inputFileName,&statBuffer) != 0) { fprintf(stderr,"ERROR: Cannot detect size of file '%s' (error: %s)\n", inputFileName, strerror(errno) ); exit(1); } size = statBuffer.st_size; /* parse definitions */ initRandom(size); definitionCount = 0; for (z = 2; z < argc; z++) { if (!parseDefinition(argv[z],&definition,size)) { exit(1); } if (definitionCount >= MAX_DEFINITIONS) { fprintf(stderr,"ERROR: to many definitions! Max. %d possible.\n",MAX_DEFINITIONS); } definitions[definitionCount] = definition; definitionCount++; } /* open input file */ inputHandle = fopen(inputFileName,"r"); if (inputHandle == NULL) { fprintf(stderr,"ERROR: Cannot open file '%s' (error: %s)\n", inputFileName, strerror(errno) ); exit(1); } /* destroy and write to stdout */ deleteCount = 0; for (n = 0; n < size; n++) { /* read byte */ data = fgetc(inputHandle); if (deleteCount == 0) { /* find matching definition */ z = 0; while ((z < definitionCount) && (n != definitions[z].position)) { z++; } /* output byte */ if (z < definitionCount) { switch (definitions[z].type) { case DEFINITION_TYPE_MODIFY: case DEFINITION_TYPE_RANDOMIZE: fwrite(String_cString(definitions[z].value),1,String_length(definitions[z].value),stdout); deleteCount = String_length(definitions[z].value)-1; break; case DEFINITION_TYPE_INSERT: fwrite(String_cString(definitions[z].value),1,String_length(definitions[z].value),stdout); fputc(data,stdout); break; case DEFINITION_TYPE_DELETE: deleteCount = definitions[z].length-1; break; } } else { fputc(data,stdout); } } else { deleteCount--; } } /* close files */ fclose(inputHandle); /* free resources */ CmdOption_done(COMMAND_LINE_OPTIONS,SIZE_OF_ARRAY(COMMAND_LINE_OPTIONS)); return 0; }
void Util_Init(struct Window *Window, void (*error_cb)(int error, const char* description), void (*keyboard_cb)(GLFWwindow *window, int key, int scancode, int action, int mods), void (*character_cb)(GLFWwindow* window, unsigned int codepoint), void (*mouse_pos_cb)(GLFWwindow *window, double x, double y), void (*mouse_button_cb)(GLFWwindow *Window, int button, int action, int mods), void (*mouse_scroll_cb)(GLFWwindow *Window, double x, double y)) { log_info("Initializing Util library"); Mouse.x = 0; Mouse.y = 0; Mouse.justReleased = Mouse.isPressed = Mouse.justPressed = Mouse.isLongedPressed = false; Mouse.isSuper_longPress = Mouse.justSuper_longReleased = false; Mouse.justQuickPressed = false; Mouse.pressedCooldown = 0; Mouse.Faked_Input = false; Game_FPS = 0; Frame_Time_Passed = 0; Keyboard.justPressed = Keyboard.justReleased = Keyboard.justTyped = false; glfwSetErrorCallback(error_cb); glfwSetKeyCallback(Window->Window, keyboard_cb); glfwSetCharCallback(Window->Window, character_cb); glfwSetCursorPosCallback(Window->Window, mouse_pos_cb); glfwSetMouseButtonCallback(Window->Window, mouse_button_cb); glfwSetScrollCallback(Window->Window, mouse_scroll_cb); log_info("Done setting up GLFW"); #ifdef _WIN32 char Path[101]; int PathLength = 100; GetModuleFileName(NULL, &Path[0], PathLength); Path[100] = '\0'; int Length = String_length(&Path[0]) - EXECUTABLE_NAME - 4; #ifndef DEBUG Executable_Path = malloc(sizeof(char) * (Length + 1)); for(int Index = 0; Index < Length; Index++) Executable_Path[Index] = Path[Index]; Executable_Path[Length] = '\0'; #else // DEBUG #ifndef CODE_BLOCKS Length -= 6; Executable_Path = malloc(sizeof(char) * (Length + 1)); for (int Index = 0; Index < Length; Index++) Executable_Path[Index] = Path[Index]; Executable_Path[Length] = '\0'; #else Length -= 10; Executable_Path = malloc(sizeof(char) * (Length + 1)); for (int Index = 0; Index < Length; Index++) Executable_Path[Index] = Path[Index]; Executable_Path[Length] = '\0'; printf("%s\n", Executable_Path); #endif #endif #else char Path[101] = { ' ' }; int PathLength = 100; readlink("/proc/self/exe", &Path[0], PathLength * sizeof(char)); Path[PathLength] = '\0'; int Length = String_length(&Path[0]) - EXECUTABLE_NAME; #ifndef DEBUG Executable_Path = malloc(sizeof(char) * (Length + 1)); for(int Index = 0; Index < Length; Index++) Executable_Path[Index] = Path[Index]; Executable_Path[Length] = '\0'; #else Length -= 6; Executable_Path = malloc(sizeof(char) * (Length + 1)); for (int Index = 0; Index < Length; Index++) Executable_Path[Index] = Path[Index]; Executable_Path[Length] = '\0'; #endif #endif Asset_Path = malloc(sizeof(char) * (String_length(Executable_Path) + String_length("Assets/") + 1)); Asset_Path = memcpy(Asset_Path, Executable_Path, sizeof(char) * (String_length(Executable_Path))); memcpy(Asset_Path + String_length(Executable_Path), "Assets/", sizeof(char) * (String_length("Assets/") + 1)); log_info("Done initializing path"); DefaultFontManager = NULL; DefaultFontManager = Font_Init(); check_mem(DefaultFontManager); HighDefaultFont = Font_Add(DefaultFontManager, "Font/Cousine-Regular.ttf", 50); check(HighDefaultFont >= 0, "Couldn't load default font : %s", "Font/Cousine-Regular.ttf"); log_info("Done loading high definition font"); LowDefaultFont = Font_Add(DefaultFontManager, "Font/Cousine-Regular.ttf", 25); check(LowDefaultFont >= 0, "Couldn't load default font : %s", "Font/Cousine-Regular.ttf"); log_info("Done loading low definition font"); Font_Use(DefaultFontManager, HighDefaultFont); ImageEngine_SetUp(); Renderer_SetUp(); gettimeofday(&Frame_TimePassed, NULL); log_info("Done initializing util library"); return; error: Application_Error(); return; }
void Serial_printString(const String s) { for (u32 i = 0; i < String_length(s); i++) Serial_printChar(s[i]); }
Errors File_makeDirectory(const String pathName, uint32 userId, uint32 groupId, uint32 permission ) { mode_t currentCreationMask; StringTokenizer pathNameTokenizer; String directoryName; uid_t uid; gid_t gid; Errors error; String name; assert(pathName != NULL); /* get current umask */ currentCreationMask = umask(0); umask(currentCreationMask); /* create directory including parent directories */ directoryName = File_newFileName(); File_initSplitFileName(&pathNameTokenizer,pathName); if (File_getNextSplitFileName(&pathNameTokenizer,&name)) { if (String_length(name) > 0) { File_setFileName(directoryName,name); } else { File_setFileNameChar(directoryName,FILES_PATHNAME_SEPARATOR_CHAR); } } if (!File_exists(directoryName)) { if (mkdir(String_cString(directoryName),0777 & ~currentCreationMask) != 0) { error = ERRORX(IO_ERROR,errno,String_cString(directoryName)); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } if ( (userId != FILE_DEFAULT_USER_ID) || (groupId != FILE_DEFAULT_GROUP_ID) ) { uid = (userId != FILE_DEFAULT_USER_ID ) ? (uid_t)userId : -1; gid = (groupId != FILE_DEFAULT_GROUP_ID) ? (gid_t)groupId : -1; if (chown(String_cString(directoryName),uid,gid) != 0) { error = ERRORX(IO_ERROR,errno,String_cString(directoryName)); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } } if (permission != FILE_DEFAULT_PERMISSION) { if (chmod(String_cString(directoryName),(permission|S_IXUSR|S_IXGRP|S_IXOTH) & ~currentCreationMask) != 0) { error = ERROR(IO_ERROR,errno); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } } } while (File_getNextSplitFileName(&pathNameTokenizer,&name)) { if (String_length(name) > 0) { File_appendFileName(directoryName,name); if (!File_exists(directoryName)) { if (mkdir(String_cString(directoryName),0777 & ~currentCreationMask) != 0) { error = ERRORX(IO_ERROR,errno,String_cString(directoryName)); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } if ( (userId != FILE_DEFAULT_USER_ID) || (groupId != FILE_DEFAULT_GROUP_ID) ) { uid = (userId != FILE_DEFAULT_USER_ID ) ? (uid_t)userId : -1; gid = (groupId != FILE_DEFAULT_GROUP_ID) ? (gid_t)groupId : -1; if (chown(String_cString(directoryName),uid,gid) != 0) { error = ERRORX(IO_ERROR,errno,String_cString(directoryName)); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } } if (permission != FILE_DEFAULT_PERMISSION) { if (chmod(String_cString(directoryName),(permission|S_IXUSR|S_IXGRP|S_IXOTH) & ~currentCreationMask) != 0) { error = ERRORX(IO_ERROR,errno,String_cString(directoryName)); File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return error; } } } } } File_doneSplitFileName(&pathNameTokenizer); File_deleteFileName(directoryName); return ERROR_NONE; }
void Terminal_putString(const String s, u8 x, u8 y) { for (u32 i = 0; i < String_length(s); i++, x++) Terminal_putChar(s[i], x, y); }
bool chunks_write(ChunkInfoBlock *chunkInfoBlock, const void *data, int definition[]) { int z; assert(chunkInfoBlock != NULL); assert(data != NULL); assert(definition != NULL); for (z = 0; definition[z] != 0; z++) { switch (definition[z]) { case CHUNK_DATATYPE_UINT8: case CHUNK_DATATYPE_INT8: { uint8 n; n = (*((uint8*)data)); if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,&n,1)) { return FALSE; } chunkInfoBlock->size += 1; chunkInfoBlock->index += 1; data = ((char*)data) + 4; } break; case CHUNK_DATATYPE_UINT16: case CHUNK_DATATYPE_INT16: { uint16 n; n = htons(*((uint16*)data)); if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,&n,2)) { return FALSE; } chunkInfoBlock->size += 2; chunkInfoBlock->index += 2; data = ((char*)data) + 4; } break; case CHUNK_DATATYPE_UINT32: case CHUNK_DATATYPE_INT32: { uint32 n; n = htonl(*((uint32*)data)); if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,&n,4)) { return FALSE; } chunkInfoBlock->size += 4; chunkInfoBlock->index += 4; data = ((char*)data) + 4; } break; case CHUNK_DATATYPE_UINT64: case CHUNK_DATATYPE_INT64: { uint64 n; uint32 l[2]; n = (*((uint64*)data)); l[0] = htonl((n & 0xFFFFffff00000000LL) >> 32); l[1] = htonl((n & 0x00000000FFFFffffLL) >> 0); if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,l,8)) { return FALSE; } chunkInfoBlock->size += 8; chunkInfoBlock->index += 8; data = ((char*)data) + 8; } break; case CHUNK_DATATYPE_NAME: { String s; ulong length; uint16 n; s = (*((String*)data)); assert(s != NULL); length = String_length(s); n = htons(length); if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,&n,2)) { return FALSE; } if (!chunkInfoBlock->writeFile(chunkInfoBlock->userData,String_cString(s),length)) { return FALSE; } chunkInfoBlock->size += 2+length; chunkInfoBlock->index += 2+length; //pointer size??? data = ((char*)data) + 4; } break; case CHUNK_DATATYPE_DATA: break; default: HALT_INTERAL_ERROR_UNHANDLED_SWITCH_CASE(); break; } } return TRUE; }