/* * Read bytes from buffer * (buffer, number, boolean?) -> string */ int rb_read(lua_State *L) { ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); size_t r = luaL_checkinteger(L, 2); int peek = lua_toboolean(L, 3); if(r > b->blen) { lua_pushnil(L); return 1; } if((b->rpos + r) > b->alen) { /* Substring wraps around to the beginning of the buffer */ lua_pushlstring(L, &b->buffer[b->rpos], b->alen - b->rpos); lua_pushlstring(L, b->buffer, r - (b->alen - b->rpos)); lua_concat(L, 2); } else { lua_pushlstring(L, &b->buffer[b->rpos], r); } if(!peek) { b->blen -= r; b->rpos += r; modpos(b); } return 1; }
/* * Move read position forward without returning the data * (buffer, number) -> boolean */ int rb_discard(lua_State *L) { ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); size_t r = luaL_checkinteger(L, 2); if(r > b->blen) { lua_pushboolean(L, 0); return 1; } b->blen -= r; b->rpos += r; modpos(b); lua_pushboolean(L, 1); return 1; }
/* * Write bytes into the buffer * (buffer, string) -> integer */ int rb_write(lua_State *L) { size_t l, w = 0; ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); const char *s = luaL_checklstring(L, 2, &l); /* Does `l` bytes fit? */ if((l + b->blen) > b->alen) { lua_pushnil(L); return 1; } while(l-- > 0) { writechar(b, *s++); w++; } modpos(b); lua_pushinteger(L, w); return 1; }
double fz(int i, int j) //force on i due to j, z component { return (G*m[j]*posz(i,j)/pow(modpos(i,j),3)); }
double fx(int i, int j) //acceleration on i due to j, x component { return (G*m[j]*posx(i,j)/pow(modpos(i,j),3)); }