Advertisement
kolton

Untitled

Jan 30th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. /* Fixes:
  2. * - value of 2nd argument was also used for 3rd argument
  3. * - file gets only rewinded when using isLines true (rewind was in the else block)
  4. * - file seek used from CUR_POS but always has added ftell(cur byte pos) to seek value
  5. * - switched rewind() to locked safe seek 0 from begin (same method as used for File::reset)
  6. * - fseek was called twice on each use of seek
  7. * - switched type of bytes from int32 to uint32 to allow negativ numbers (step back from cur_pos)
  8. *
  9. * PLZ CHECK: JS_ValueToUint32 and unit32 value type, idk if this function/type exists
  10. */
  11.  
  12. JSAPI_FUNC(file_seek)
  13. {
  14. FileData* fdata = (FileData*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &file_class, NULL);
  15. if(fdata && fdata->fptr)
  16. {
  17. if(argc > 0)
  18. {
  19. uint32 bytes;
  20. bool isLines = false, fromStart = false;
  21. if(JS::ToUint32(cx, JS_ARGV(cx, vp)[0], &bytes) == JS_FALSE)
  22. THROW_ERROR(cx, "Could not convert parameter 1");
  23. if(argc > 1 && JSVAL_IS_BOOLEAN(JS_ARGV(cx, vp)[1]))
  24. isLines = !!JSVAL_TO_BOOLEAN(JS_ARGV(cx, vp)[1]);
  25. if(argc > 2 && JSVAL_IS_BOOLEAN(JS_ARGV(cx, vp)[2]))
  26. fromStart = !!JSVAL_TO_BOOLEAN(JS_ARGV(cx, vp)[2]);
  27.  
  28. if(fromStart)
  29. {
  30. if(fdata->locked && fseek(fdata->fptr, 0L, SEEK_SET))
  31. THROW_ERROR(cx, _strerror("Seek fromStart failed"));
  32. if(!fdata->locked && _fseek_nolock(fdata->fptr, 0L, SEEK_SET))
  33. THROW_ERROR(cx, _strerror("Seek fromStart failed"));
  34. }
  35.  
  36. if(!isLines)
  37. {
  38. if(fdata->locked && fseek(fdata->fptr, bytes, SEEK_CUR))
  39. THROW_ERROR(cx, _strerror("Seek failed"));
  40. if(!fdata->locked && _fseek_nolock(fdata->fptr, bytes, SEEK_CUR))
  41. THROW_ERROR(cx, _strerror("Seek failed"));
  42. }
  43. else
  44. {
  45. // semi-ugly hack to seek to the specified line
  46. // if I were unlazy I wouldn't be allocating/deallocating all this memory, but for now it's ok
  47. while(bytes--)
  48. delete[] readLine(fdata->fptr, fdata->locked);
  49. }
  50. }
  51. else
  52. THROW_ERROR(cx, "Not enough parameters");
  53. }
  54. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(JS_THIS_OBJECT(cx, vp)));
  55. return JS_TRUE;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement