Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Fixes:
- * - value of 2nd argument was also used for 3rd argument
- * - file gets only rewinded when using isLines true (rewind was in the else block)
- * - file seek used from CUR_POS but always has added ftell(cur byte pos) to seek value
- * - switched rewind() to locked safe seek 0 from begin (same method as used for File::reset)
- * - fseek was called twice on each use of seek
- * - switched type of bytes from int32 to uint32 to allow negativ numbers (step back from cur_pos)
- *
- * PLZ CHECK: JS_ValueToUint32 and unit32 value type, idk if this function/type exists
- */
- JSAPI_FUNC(file_seek)
- {
- FileData* fdata = (FileData*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &file_class, NULL);
- if(fdata && fdata->fptr)
- {
- if(argc > 0)
- {
- uint32 bytes;
- bool isLines = false, fromStart = false;
- if(JS::ToUint32(cx, JS_ARGV(cx, vp)[0], &bytes) == JS_FALSE)
- THROW_ERROR(cx, "Could not convert parameter 1");
- if(argc > 1 && JSVAL_IS_BOOLEAN(JS_ARGV(cx, vp)[1]))
- isLines = !!JSVAL_TO_BOOLEAN(JS_ARGV(cx, vp)[1]);
- if(argc > 2 && JSVAL_IS_BOOLEAN(JS_ARGV(cx, vp)[2]))
- fromStart = !!JSVAL_TO_BOOLEAN(JS_ARGV(cx, vp)[2]);
- if(fromStart)
- {
- if(fdata->locked && fseek(fdata->fptr, 0L, SEEK_SET))
- THROW_ERROR(cx, _strerror("Seek fromStart failed"));
- if(!fdata->locked && _fseek_nolock(fdata->fptr, 0L, SEEK_SET))
- THROW_ERROR(cx, _strerror("Seek fromStart failed"));
- }
- if(!isLines)
- {
- if(fdata->locked && fseek(fdata->fptr, bytes, SEEK_CUR))
- THROW_ERROR(cx, _strerror("Seek failed"));
- if(!fdata->locked && _fseek_nolock(fdata->fptr, bytes, SEEK_CUR))
- THROW_ERROR(cx, _strerror("Seek failed"));
- }
- else
- {
- // semi-ugly hack to seek to the specified line
- // if I were unlazy I wouldn't be allocating/deallocating all this memory, but for now it's ok
- while(bytes--)
- delete[] readLine(fdata->fptr, fdata->locked);
- }
- }
- else
- THROW_ERROR(cx, "Not enough parameters");
- }
- JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(JS_THIS_OBJECT(cx, vp)));
- return JS_TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement