const char *font_init(font_t *self, const char *filename, int ptsize) { const char *errmsg = NULL; void *data = NULL; FILE *fp = NULL; memset(self, 0, sizeof(*self)); /* Load font file */ int size; data = filesystem_read(filename, &size); if (!data) { errmsg = "could not open font file"; goto fail; } /* Init font */ errmsg = initFont(self, data, ptsize); if (errmsg) { goto fail; } /* Free font data */ filesystem_free(data); data = NULL; return NULL; fail: if (fp) fclose(fp); filesystem_free(data); return errmsg; }
static int l_filesystem_load(lua_State* state) { char const* filename = l_tools_toStringOrError(state, 1); char* data = NULL; int len = filesystem_read(filename, &data); if(len < 0) { lua_pushstring(state, "could not read file"); return lua_error(state); } luaL_loadstring(state, data); free(data); return 1; }
static int l_filesystem_read(lua_State* state) { // TODO implement max length char const* filename = l_tools_toStringOrError(state, 1); char* data = NULL; int len = filesystem_read(filename, &data); if(len < 0) { lua_pushstring(state, "could not read file"); return lua_error(state); } lua_pushstring(state, data); free(data); lua_pushnumber(state, len); return 2; }
static void handle_filesystem_request(int so) { int error; int operation; char key[(NAME_MAX + 1) + sizeof(union webdav_request)]; size_t num_bytes; char *bytes; union webdav_reply reply; /* get the request from the socket */ error = get_request(so, &operation, key, sizeof(key)); if ( !error ) { #if DEBUG LogMessage(kTrace, "handle_filesystem_request: %s(%d)\n", (operation==WEBDAV_LOOKUP) ? "LOOKUP" : (operation==WEBDAV_CREATE) ? "CREATE" : (operation==WEBDAV_OPEN) ? "OPEN" : (operation==WEBDAV_CLOSE) ? "CLOSE" : (operation==WEBDAV_GETATTR) ? "GETATTR" : (operation==WEBDAV_SETATTR) ? "SETATTR" : (operation==WEBDAV_READ) ? "READ" : (operation==WEBDAV_WRITE) ? "WRITE" : (operation==WEBDAV_FSYNC) ? "FSYNC" : (operation==WEBDAV_REMOVE) ? "REMOVE" : (operation==WEBDAV_RENAME) ? "RENAME" : (operation==WEBDAV_MKDIR) ? "MKDIR" : (operation==WEBDAV_RMDIR) ? "RMDIR" : (operation==WEBDAV_READDIR) ? "READDIR" : (operation==WEBDAV_STATFS) ? "STATFS" : (operation==WEBDAV_UNMOUNT) ? "UNMOUNT" : (operation==WEBDAV_INVALCACHES) ? "INVALCACHES" : "???", operation ); #endif bzero((void *)&reply, sizeof(union webdav_reply)); /* If the connection is down just return EBUSY, but always let UNMOUNT and INVALCACHES requests */ /* go through regardless of the state of the connection. */ if ( (get_connectionstate() == WEBDAV_CONNECTION_DOWN) && (operation != WEBDAV_UNMOUNT) && (operation != WEBDAV_INVALCACHES) ) { error = ETIMEDOUT; send_reply(so, (void *)&reply, sizeof(union webdav_reply), error); } else { /* call the function to handle the request */ switch ( operation ) { case WEBDAV_LOOKUP: error = filesystem_lookup((struct webdav_request_lookup *)key, (struct webdav_reply_lookup *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_lookup), error); break; case WEBDAV_CREATE: error = filesystem_create((struct webdav_request_create *)key, (struct webdav_reply_create *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_create), error); break; case WEBDAV_OPEN: error = filesystem_open((struct webdav_request_open *)key, (struct webdav_reply_open *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_open), error); break; case WEBDAV_CLOSE: error = filesystem_close((struct webdav_request_close *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_GETATTR: error = filesystem_getattr((struct webdav_request_getattr *)key, (struct webdav_reply_getattr *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_getattr), error); break; case WEBDAV_READ: bytes = NULL; num_bytes = 0; error = filesystem_read((struct webdav_request_read *)key, &bytes, &num_bytes); send_reply(so, (void *)bytes, (int)num_bytes, error); if (bytes) { free(bytes); } break; case WEBDAV_FSYNC: error = filesystem_fsync((struct webdav_request_fsync *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_REMOVE: error = filesystem_remove((struct webdav_request_remove *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_RENAME: error = filesystem_rename((struct webdav_request_rename *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_MKDIR: error = filesystem_mkdir((struct webdav_request_mkdir *)key, (struct webdav_reply_mkdir *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_mkdir), error); break; case WEBDAV_RMDIR: error = filesystem_rmdir((struct webdav_request_rmdir *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_READDIR: error = filesystem_readdir((struct webdav_request_readdir *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_STATFS: error = filesystem_statfs((struct webdav_request_statfs *)key, (struct webdav_reply_statfs *)&reply); send_reply(so, (void *)&reply, sizeof(struct webdav_reply_statfs), error); break; case WEBDAV_UNMOUNT: webdav_kill(-2); /* tell the main select loop to exit */ send_reply(so, (void *)0, 0, error); break; case WEBDAV_INVALCACHES: error = filesystem_invalidate_caches((struct webdav_request_invalcaches *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_WRITESEQ: error = filesystem_write_seq((struct webdav_request_writeseq *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_DUMP_COOKIES: dump_cookies((struct webdav_request_cookies *)key); send_reply(so, (void *)0, 0, error); break; case WEBDAV_CLEAR_COOKIES: reset_cookies((struct webdav_request_cookies *)key); send_reply(so, (void *)0, 0, error); break; default: error = ENOTSUP; break; } } #if DEBUG LogMessage(kError, "handle_filesystem_request: error %d, %s(%d)\n", error, (operation==WEBDAV_LOOKUP) ? "LOOKUP" : (operation==WEBDAV_CREATE) ? "CREATE" : (operation==WEBDAV_OPEN) ? "OPEN" : (operation==WEBDAV_CLOSE) ? "CLOSE" : (operation==WEBDAV_GETATTR) ? "GETATTR" : (operation==WEBDAV_SETATTR) ? "SETATTR" : (operation==WEBDAV_READ) ? "READ" : (operation==WEBDAV_WRITE) ? "WRITE" : (operation==WEBDAV_FSYNC) ? "FSYNC" : (operation==WEBDAV_REMOVE) ? "REMOVE" : (operation==WEBDAV_RENAME) ? "RENAME" : (operation==WEBDAV_MKDIR) ? "MKDIR" : (operation==WEBDAV_RMDIR) ? "RMDIR" : (operation==WEBDAV_READDIR) ? "READDIR" : (operation==WEBDAV_STATFS) ? "STATFS" : (operation==WEBDAV_UNMOUNT) ? "UNMOUNT" : (operation==WEBDAV_INVALCACHES) ? "INVALCACHES" : "???", operation ); #endif } else { LogMessage(kError, "handle_filesystem_request: get_request failed %d\n", error); send_reply(so, NULL, 0, error); } close(so); }
int static l_graphics_newShader(lua_State* state) { char const* vertexSrc = l_tools_toStringOrError(state, 1); char const* fragmentSrc = NULL; char * loadedFile1 = NULL; char * loadedFile2 = NULL; if(lua_isstring(state, 2)) { fragmentSrc = lua_tostring(state, 2); if(!isVertexShader(vertexSrc)) { // TODO int loadedFile1Size = filesystem_read(vertexSrc, &loadedFile1); (void) loadedFile1Size; if(!loadedFile1 || !isVertexShader(loadedFile1)) { free(loadedFile1); lua_pushstring(state, "input 1 is not a valid vertex shader"); return lua_error(state); } vertexSrc = loadedFile1; } if(!isSingleFragmentShader(fragmentSrc)) { // TODO int loadedFile2Size = filesystem_read(fragmentSrc, &loadedFile2); (void)loadedFile2Size; if(!loadedFile2 || !isSingleFragmentShader(loadedFile2)) { free(loadedFile1); free(loadedFile2); lua_pushstring(state, "input 2 is not a valid fragment shader"); return lua_error(state); } fragmentSrc = loadedFile2; } } else { if(isVertexShader(vertexSrc)) { // nothing required } else if(isSingleFragmentShader(vertexSrc)) { fragmentSrc = vertexSrc; vertexSrc = NULL; } else { // TODO int loadedFile1Size = filesystem_read(vertexSrc, &loadedFile1); (void) loadedFile1Size; if(!loadedFile1) { lua_pushstring(state, "could not open file"); return lua_error(state); } if(isSingleFragmentShader(loadedFile1)) { fragmentSrc = loadedFile1; vertexSrc = NULL; } else if(isVertexShader(loadedFile1)) { vertexSrc = loadedFile1; fragmentSrc = NULL; } else { free(loadedFile1); lua_pushstring(state, "input is not a valid shader"); return lua_error(state); } } } l_graphics_Shader * shader = lua_newuserdata(state, sizeof(l_graphics_Shader)); graphics_ShaderCompileStatus status = graphics_Shader_new(&shader->shader, vertexSrc, fragmentSrc); if(status != graphics_ShaderCompileStatus_okay) { pushShaderInfoLog(state, &shader->shader); return lua_error(state); } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.shaderMT); lua_setmetatable(state, -2); free(loadedFile1); free(loadedFile2); int const textureUnits = shader->shader.textureUnitCount; shader->referencedTextures = malloc(textureUnits * sizeof(int)); for(int i = 0; i < textureUnits; ++i) { shader->referencedTextures[i] = LUA_NOREF; } return 1; }