[IPLUG/AAX] remove IPlugAAX::SetParameterFromGUI()
[wdl/wdl-ol.git] / WDL / fileread.h
blobebad18999b10ace77967274928ffb69035b06d3a
1 /*
2 WDL - fileread.h
3 Copyright (C) 2005 and later Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 This file provides the WDL_FileRead object, which can be used to read files.
23 On windows systems it supports reading synchronous, asynchronous, memory mapped, and asynchronous unbuffered.
24 On non-windows systems it acts as a wrapper for fopen()/etc.
30 #ifndef _WDL_FILEREAD_H_
31 #define _WDL_FILEREAD_H_
36 #include "ptrlist.h"
40 #if defined(_WIN32) && !defined(WDL_NO_WIN32_FILEREAD)
41 #ifndef WDL_WIN32_NATIVE_READ
42 #define WDL_WIN32_NATIVE_READ
43 #endif
44 #else
45 #ifdef WDL_WIN32_NATIVE_READ
46 #undef WDL_WIN32_NATIVE_READ
47 #endif
49 #if !defined(WDL_NO_POSIX_FILEREAD)
50 #define WDL_POSIX_NATIVE_READ
51 #include <sys/fcntl.h>
52 #include <sys/file.h>
53 #include <sys/errno.h>
54 #include <sys/mman.h>
55 #ifdef __APPLE__
56 #include <sys/param.h>
57 #include <sys/mount.h>
58 #endif
59 #endif
61 #endif
65 #ifdef _MSC_VER
66 #define WDL_FILEREAD_POSTYPE __int64
67 #else
68 #define WDL_FILEREAD_POSTYPE long long
69 #endif
70 class WDL_FileRead
73 #ifdef WDL_WIN32_NATIVE_READ
76 class WDL_FileRead__ReadEnt
78 public:
79 WDL_FileRead__ReadEnt(int sz, char *buf)
81 m_size=0;
82 memset(&m_ol,0,sizeof(m_ol));
83 m_ol.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
84 m_buf=buf;
86 ~WDL_FileRead__ReadEnt()
88 CloseHandle(m_ol.hEvent);
91 OVERLAPPED m_ol;
92 DWORD m_size;
93 LPVOID m_buf;
96 #endif
98 #if defined(_WIN32) && !defined(WDL_NO_SUPPORT_UTF8)
99 BOOL HasUTF8(const char *_str)
101 const unsigned char *str = (const unsigned char *)_str;
102 if (!str) return FALSE;
103 while (*str)
105 unsigned char c = *str;
106 if (c >= 0xC2)
108 if (c <= 0xDF && str[1] >=0x80 && str[1] <= 0xBF) return TRUE;
109 else if (c <= 0xEF && str[1] >=0x80 && str[1] <= 0xBF && str[2] >=0x80 && str[2] <= 0xBF) return TRUE;
110 else if (c <= 0xF4 && str[1] >=0x80 && str[1] <= 0xBF && str[2] >=0x80 && str[2] <= 0xBF) return TRUE;
112 str++;
114 return FALSE;
116 #endif
118 public:
119 // allow_async=1 for unbuffered async, 2 for buffered async, =-1 for unbuffered sync
120 // async aspect is unused on OS X, but the buffered mode affects F_NOCACHE
121 WDL_FileRead(const char *filename, int allow_async=1, int bufsize=8192, int nbufs=4, unsigned int mmap_minsize=0, unsigned int mmap_maxsize=0) : m_bufspace(4096 WDL_HEAPBUF_TRACEPARM("WDL_FileRead"))
123 m_async_hashaderr=false;
124 m_sync_bufmode_used=m_sync_bufmode_pos=0;
125 m_async_readpos=m_file_position=0;
126 m_fsize=0;
127 m_fsize_maychange=false;
128 m_syncrd_firstbuf=true;
129 m_mmap_view=0;
130 m_mmap_totalbufmode=0;
132 #define WDL_UNBUF_ALIGN 8192
133 if (bufsize&(WDL_UNBUF_ALIGN-1)) bufsize=(bufsize&~(WDL_UNBUF_ALIGN-1))+WDL_UNBUF_ALIGN; // ensure bufsize is multiple of 4kb
135 #ifdef WDL_WIN32_NATIVE_READ
137 m_mmap_fmap=0;
138 bool isNT = GetVersion()<0x80000000;
139 m_async = isNT ? allow_async : 0;
141 int flags=FILE_ATTRIBUTE_NORMAL;
142 if (m_async>0)
144 flags|=FILE_FLAG_OVERLAPPED;
145 if (m_async==1) flags|=FILE_FLAG_NO_BUFFERING;
147 else if (nbufs*bufsize>=WDL_UNBUF_ALIGN && !mmap_maxsize && m_async==-1)
148 flags|=FILE_FLAG_NO_BUFFERING; // non-async mode unbuffered if we do our own buffering
150 #ifndef WDL_NO_SUPPORT_UTF8
151 m_fh = INVALID_HANDLE_VALUE;
152 if (isNT && HasUTF8(filename)) // only convert to wide if there are UTF-8 chars
154 int szreq=MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,filename,-1,NULL,0);
155 if (szreq > 1000)
157 WDL_TypedBuf<WCHAR> wfilename;
158 wfilename.Resize(szreq+10);
160 if (MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,filename,-1,wfilename.Get(),wfilename.GetSize()))
162 m_fh = CreateFileW(wfilename.Get(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,flags,NULL);
163 if (m_fh == INVALID_HANDLE_VALUE && GetLastError()==ERROR_SHARING_VIOLATION)
165 m_fh = CreateFileW(wfilename.Get(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,flags,NULL);
166 m_fsize_maychange=true;
170 else
172 WCHAR wfilename[1024];
174 if (MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,filename,-1,wfilename,1024))
176 m_fh = CreateFileW(wfilename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,flags,NULL);
177 if (m_fh == INVALID_HANDLE_VALUE && GetLastError()==ERROR_SHARING_VIOLATION)
179 m_fh = CreateFileW(wfilename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,flags,NULL);
180 m_fsize_maychange=true;
185 if (m_fh == INVALID_HANDLE_VALUE)
186 #endif
188 m_fh = CreateFileA(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,flags,NULL);
189 if (m_fh == INVALID_HANDLE_VALUE && GetLastError()==ERROR_SHARING_VIOLATION)
191 m_fh = CreateFileA(filename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,flags,NULL);
192 m_fsize_maychange=true;
196 if (m_fh != INVALID_HANDLE_VALUE)
198 DWORD h=0;
199 DWORD l=GetFileSize(m_fh,&h);
200 m_fsize=(((WDL_FILEREAD_POSTYPE)h)<<32)|l;
202 if (!h && l < mmap_maxsize && m_async<=0)
204 if (l >= mmap_minsize)
206 m_mmap_fmap=CreateFileMapping(m_fh,NULL,PAGE_READONLY,NULL,0,NULL);
207 if (m_mmap_fmap)
209 m_mmap_view=MapViewOfFile(m_mmap_fmap,FILE_MAP_READ,0,0,(int)m_fsize);
210 if (!m_mmap_view)
212 CloseHandle(m_mmap_fmap);
213 m_mmap_fmap=0;
215 else m_fsize_maychange=false;
218 else if (l>0)
220 m_mmap_totalbufmode = malloc(l);
221 DWORD sz;
222 ReadFile(m_fh,m_mmap_totalbufmode,l,&sz,NULL);
223 m_fsize_maychange=false;
227 if (m_async>0)
229 m_async_bufsize=bufsize;
230 int x;
231 char *bptr=(char *)m_bufspace.Resize(nbufs*bufsize + (WDL_UNBUF_ALIGN-1));
232 int a=((int)(INT_PTR)bptr)&(WDL_UNBUF_ALIGN-1);
233 if (a) bptr += WDL_UNBUF_ALIGN-a;
234 for (x = 0; x < nbufs; x ++)
236 WDL_FileRead__ReadEnt *t=new WDL_FileRead__ReadEnt(m_async_bufsize,bptr);
237 m_empties.Add(t);
238 bptr+=m_async_bufsize;
241 else if (!m_mmap_view && !m_mmap_totalbufmode && nbufs*bufsize>=WDL_UNBUF_ALIGN)
243 m_bufspace.Resize(nbufs*bufsize+(WDL_UNBUF_ALIGN-1));
247 #elif defined(WDL_POSIX_NATIVE_READ)
248 m_filedes_locked=false;
249 m_filedes_rdpos=0;
250 m_filedes=open(filename,O_RDONLY);
251 if (m_filedes>=0)
253 if (flock(m_filedes,LOCK_SH|LOCK_NB)>=0) // get shared lock
254 m_filedes_locked=true;
255 else
256 m_fsize_maychange=true; // if couldnt get shared lock, then it may change
258 #ifdef __APPLE__
259 if (allow_async==1 || allow_async==-1)
261 struct statfs sfs;
262 if (fstatfs(m_filedes,&sfs)||(sfs.f_flags&MNT_LOCAL)) // don't use F_NOCACHE on nfs/smb/afp mounts, we need caching there!
263 fcntl(m_filedes,F_NOCACHE,1);
265 #endif
266 m_fsize=lseek(m_filedes,0,SEEK_END);
267 lseek(m_filedes,0,SEEK_SET);
269 if (m_fsize < mmap_maxsize)
271 if (m_fsize >= mmap_minsize)
273 m_mmap_view = mmap(NULL,m_fsize,PROT_READ,MAP_SHARED,m_filedes,0);
274 if (m_mmap_view == MAP_FAILED) m_mmap_view = 0;
275 else m_fsize_maychange=false;
277 else
279 m_mmap_totalbufmode = malloc(m_fsize);
280 m_fsize = pread(m_filedes,m_mmap_totalbufmode,m_fsize,0);
281 m_fsize_maychange=false;
286 if (!m_mmap_view && !m_mmap_totalbufmode && m_filedes>=0 && nbufs*bufsize>=WDL_UNBUF_ALIGN)
287 m_bufspace.Resize(nbufs*bufsize+(WDL_UNBUF_ALIGN-1));
289 #else
290 m_fp=fopen(filename,"rb");
291 if(m_fp)
293 fseek(m_fp,0,SEEK_END);
294 m_fsize=ftell(m_fp);
295 fseek(m_fp,0,SEEK_SET);
297 if (m_fp && nbufs*bufsize>=WDL_UNBUF_ALIGN)
298 m_bufspace.Resize(nbufs*bufsize+(WDL_UNBUF_ALIGN-1));
299 #endif
302 ~WDL_FileRead()
304 free(m_mmap_totalbufmode);
305 m_mmap_totalbufmode=0;
307 #ifdef WDL_WIN32_NATIVE_READ
308 int x;
309 for (x = 0; x < m_empties.GetSize();x ++) delete m_empties.Get(x);
310 m_empties.Empty();
311 for (x = 0; x < m_full.GetSize();x ++) delete m_full.Get(x);
312 m_full.Empty();
313 for (x = 0; x < m_pending.GetSize();x ++)
315 WaitForSingleObject(m_pending.Get(x)->m_ol.hEvent,INFINITE);
316 delete m_pending.Get(x);
318 m_pending.Empty();
320 if (m_mmap_view) UnmapViewOfFile(m_mmap_view);
321 m_mmap_view=0;
323 if (m_mmap_fmap) CloseHandle(m_mmap_fmap);
324 m_mmap_fmap=0;
326 if (m_fh != INVALID_HANDLE_VALUE) CloseHandle(m_fh);
327 m_fh=INVALID_HANDLE_VALUE;
328 #elif defined(WDL_POSIX_NATIVE_READ)
329 if (m_mmap_view) munmap(m_mmap_view,m_fsize);
330 m_mmap_view=0;
331 if (m_filedes>=0)
333 if (m_filedes_locked) flock(m_filedes,LOCK_UN); // release shared lock
334 close(m_filedes);
336 m_filedes=-1;
337 #else
338 if (m_fp) fclose(m_fp);
339 m_fp=0;
340 #endif
344 bool IsOpen()
346 #ifdef WDL_WIN32_NATIVE_READ
347 return (m_fh != INVALID_HANDLE_VALUE);
348 #elif defined(WDL_POSIX_NATIVE_READ)
349 return m_filedes >= 0;
350 #else
351 return m_fp != NULL;
352 #endif
355 #ifdef WDL_WIN32_NATIVE_READ
357 int RunReads()
359 while (m_pending.GetSize())
361 WDL_FileRead__ReadEnt *ent=m_pending.Get(0);
362 DWORD s=0;
364 if (!ent->m_size && !GetOverlappedResult(m_fh,&ent->m_ol,&s,FALSE)) break;
365 m_pending.Delete(0);
366 if (!ent->m_size) ent->m_size=s;
367 m_full.Add(ent);
371 if (m_empties.GetSize()>0)
373 if (m_async_readpos < m_file_position) m_async_readpos = m_file_position;
375 if (m_async==1) m_async_readpos &= ~((WDL_FILEREAD_POSTYPE) WDL_UNBUF_ALIGN-1);
377 if (m_async_readpos >= m_fsize) return 0;
379 const int rdidx=m_empties.GetSize()-1;
380 WDL_FileRead__ReadEnt *t=m_empties.Get(rdidx);
382 ResetEvent(t->m_ol.hEvent);
384 *(WDL_FILEREAD_POSTYPE *)&t->m_ol.Offset = m_async_readpos;
386 m_async_readpos += m_async_bufsize;
387 DWORD dw;
388 if (ReadFile(m_fh,t->m_buf,m_async_bufsize,&dw,&t->m_ol))
390 if (!dw) return 1;
392 else
394 if (GetLastError() != ERROR_IO_PENDING) return 1;
395 dw=0;
397 t->m_size=dw;
398 m_empties.Delete(rdidx);
399 m_pending.Add(t);
401 return 0;
404 int AsyncRead(char *buf, int maxlen)
406 char *obuf=buf;
407 int lenout=0;
408 if (m_file_position+maxlen > m_fsize)
410 maxlen=(int) (m_fsize-m_file_position);
412 if (maxlen<1) return 0;
414 int errcnt=!!m_async_hashaderr;
417 while (m_full.GetSize() > 0)
419 WDL_FileRead__ReadEnt *ti=m_full.Get(0);
420 WDL_FILEREAD_POSTYPE tiofs=*(WDL_FILEREAD_POSTYPE *)&ti->m_ol.Offset;
421 if (m_file_position >= tiofs && m_file_position < tiofs + ti->m_size)
423 if (maxlen < 1) break;
425 int l=ti->m_size-(int) (m_file_position-tiofs);
426 if (l > maxlen) l=maxlen;
428 memcpy(buf,(char *)ti->m_buf+m_file_position - tiofs,l);
429 buf += l;
430 m_file_position += l;
431 maxlen -= l;
432 lenout += l;
434 else
436 m_empties.Add(ti);
437 m_full.Delete(0);
441 if (maxlen > 0 && m_async_readpos != m_file_position)
443 int x;
444 for (x = 0; x < m_pending.GetSize(); x ++)
446 WDL_FileRead__ReadEnt *ent=m_pending.Get(x);
447 WDL_FILEREAD_POSTYPE tiofs=*(WDL_FILEREAD_POSTYPE *)&ent->m_ol.Offset;
448 if (m_file_position >= tiofs && m_file_position < tiofs + m_async_bufsize) break;
450 if (x == m_pending.GetSize())
452 m_async_readpos=m_file_position;
456 errcnt+=RunReads();
458 if (maxlen > 0 && m_pending.GetSize() && !m_full.GetSize())
460 WDL_FileRead__ReadEnt *ent=m_pending.Get(0);
461 m_pending.Delete(0);
463 if (ent->m_size) m_full.Add(ent);
464 else
466 // WaitForSingleObject(ent->m_ol.hEvent,INFINITE);
468 DWORD s=0;
469 if (GetOverlappedResult(m_fh,&ent->m_ol,&s,TRUE) && s)
471 ent->m_size=s;
472 m_full.Add(ent);
474 else // failed read, set the error flag
476 errcnt++;
477 ent->m_size=0;
478 m_empties.Add(ent);
483 while (maxlen > 0 && (m_pending.GetSize()||m_full.GetSize()) && !errcnt);
484 if (!errcnt) RunReads();
485 else m_async_hashaderr=true;
487 return lenout;
490 #endif
492 void *GetMappedView(int offs, int *len)
494 if (!m_mmap_view && !m_mmap_totalbufmode) return 0;
496 int maxl=(int) (m_fsize-(WDL_FILEREAD_POSTYPE)offs);
497 if (*len > maxl) *len=maxl;
498 if (m_mmap_view)
499 return (char *)m_mmap_view + offs;
500 else
501 return (char *)m_mmap_totalbufmode + offs;
504 int Read(void *buf, int len)
506 if (m_mmap_view||m_mmap_totalbufmode)
508 int maxl=(int) (m_fsize-m_file_position);
509 if (maxl > len) maxl=len;
510 if (maxl < 0) maxl=0;
511 if (maxl>0)
513 if (m_mmap_view)
514 memcpy(buf,(char *)m_mmap_view + (int)m_file_position,maxl);
515 else
516 memcpy(buf,(char *)m_mmap_totalbufmode + (int)m_file_position,maxl);
519 m_file_position+=maxl;
520 return maxl;
523 if (m_fsize_maychange) GetSize(); // update m_fsize
525 #ifdef WDL_WIN32_NATIVE_READ
526 if (m_fh == INVALID_HANDLE_VALUE||len<1) return 0;
528 if (m_async>0)
530 return AsyncRead((char *)buf,len);
532 #elif defined(WDL_POSIX_NATIVE_READ)
533 if (m_filedes<0 || len<1) return 0;
535 #else
536 if (!m_fp || len<1) return 0;
538 #endif
540 if (m_bufspace.GetSize()>=WDL_UNBUF_ALIGN*2-1)
542 int rdout=0;
543 int sz=m_bufspace.GetSize()-(WDL_UNBUF_ALIGN-1);
544 char *srcbuf=(char *)m_bufspace.Get(); // read size
545 if (((int)(INT_PTR)srcbuf)&(WDL_UNBUF_ALIGN-1)) srcbuf += WDL_UNBUF_ALIGN-(((int)(INT_PTR)srcbuf)&(WDL_UNBUF_ALIGN-1));
546 while (len > rdout)
548 int a=m_sync_bufmode_used-m_sync_bufmode_pos;
549 if (a>(len-rdout)) a=(len-rdout);
550 if (a>0)
552 memcpy((char*)buf+rdout,srcbuf+m_sync_bufmode_pos,a);
553 rdout+=a;
554 m_sync_bufmode_pos+=a;
555 m_file_position+=a;
558 if (len > rdout)
560 m_sync_bufmode_used=0;
561 m_sync_bufmode_pos=0;
563 int thissz=sz;
564 if (m_syncrd_firstbuf) // this is a scheduling mechanism to avoid having reads on various files always happening at the same time -- not needed in async modes, only in sync with large buffers
566 m_syncrd_firstbuf=false;
567 int a= thissz/WDL_UNBUF_ALIGN;
568 if (a > 1)
570 static int rrs; // may not be ideal on multithread, but having it incorrect isnt a big deal.
571 if (a>7) thissz >>= (rrs++)&3;
572 else thissz>>= (rrs++)&1;
576 #ifdef WDL_WIN32_NATIVE_READ
577 DWORD o;
578 if (m_async==-1)
580 if (m_file_position&(WDL_UNBUF_ALIGN-1))
582 int offs = (int)(m_file_position&(WDL_UNBUF_ALIGN-1));
583 LONG high=(LONG) ((m_file_position-offs)>>32);
584 SetFilePointer(m_fh,(LONG)((m_file_position-offs)&((WDL_FILEREAD_POSTYPE)0xFFFFFFFF)),&high,FILE_BEGIN);
585 m_sync_bufmode_pos=offs;
588 if (!ReadFile(m_fh,srcbuf,thissz,&o,NULL) || o<1 || m_sync_bufmode_pos>=(int)o)
590 break;
592 #elif defined(WDL_POSIX_NATIVE_READ)
593 int o;
594 o=pread(m_filedes,srcbuf,thissz,m_filedes_rdpos);
595 if (o>0) m_filedes_rdpos+=o;
596 if (o<1 || m_sync_bufmode_pos>=o) break;
598 #else
599 int o;
600 o=fread(srcbuf,1,thissz,m_fp);
601 if (o<1 || m_sync_bufmode_pos>=o) break;
602 #endif
603 m_sync_bufmode_used=o;
607 return rdout;
609 else
611 #ifdef WDL_WIN32_NATIVE_READ
612 DWORD dw=0;
613 ReadFile(m_fh,buf,len,&dw,NULL);
614 m_file_position+=dw;
615 return dw;
616 #elif defined(WDL_POSIX_NATIVE_READ)
618 int ret=pread(m_filedes,buf,len,m_filedes_rdpos);
619 if (ret>0) m_filedes_rdpos+=ret;
620 m_file_position+=ret;
621 return ret;
622 #else
623 int ret=fread(buf,1,len,m_fp);
624 m_file_position+=ret;
625 return ret;
626 #endif
631 WDL_FILEREAD_POSTYPE GetSize()
633 #ifdef WDL_WIN32_NATIVE_READ
634 if (m_fh == INVALID_HANDLE_VALUE) return 0;
635 #elif defined(WDL_POSIX_NATIVE_READ)
636 if (m_filedes<0) return -1;
638 #else
639 if (!m_fp) return -1;
640 #endif
642 if (m_fsize_maychange)
644 #ifdef WDL_WIN32_NATIVE_READ
645 DWORD h=0;
646 DWORD l=GetFileSize(m_fh,&h);
647 m_fsize=(((WDL_FILEREAD_POSTYPE)h)<<32)|l;
648 #elif defined(WDL_POSIX_NATIVE_READ)
649 struct stat st;
650 if (!fstat(m_filedes,&st)) m_fsize = st.st_size;
652 #endif
655 return m_fsize;
658 WDL_FILEREAD_POSTYPE GetPosition()
660 #ifdef WDL_WIN32_NATIVE_READ
661 if (m_fh == INVALID_HANDLE_VALUE) return -1;
662 #elif defined(WDL_POSIX_NATIVE_READ)
663 if (m_filedes<0) return -1;
664 #else
665 if (!m_fp) return -1;
666 #endif
667 return m_file_position;
670 bool SetPosition(WDL_FILEREAD_POSTYPE pos) // returns 0 on success
672 m_async_hashaderr=false;
674 #ifdef WDL_WIN32_NATIVE_READ
675 if (m_fh == INVALID_HANDLE_VALUE) return true;
676 #elif defined(WDL_POSIX_NATIVE_READ)
677 if (m_filedes<0) return true;
678 #else
679 if (!m_fp) return true;
680 #endif
682 if (m_fsize_maychange) GetSize();
684 if (pos < 0) pos=0;
685 if (pos > m_fsize) pos=m_fsize;
686 WDL_FILEREAD_POSTYPE oldpos=m_file_position;
687 if (m_file_position!=pos) m_file_position=pos;
688 else return false;
690 if (m_mmap_view||m_mmap_totalbufmode) return false;
692 #ifdef WDL_WIN32_NATIVE_READ
693 if (m_async>0)
695 WDL_FileRead__ReadEnt *ent;
697 if (pos > m_async_readpos || !(ent=m_full.Get(0)) || pos < *(WDL_FILEREAD_POSTYPE *)&ent->m_ol.Offset)
699 m_async_readpos=pos;
702 return FALSE;
704 #endif
707 if (m_bufspace.GetSize()>=WDL_UNBUF_ALIGN*2-1)
709 if (pos >= oldpos-m_sync_bufmode_pos && pos < oldpos-m_sync_bufmode_pos + m_sync_bufmode_used)
711 int diff=(int) (pos-oldpos);
712 m_sync_bufmode_pos+=diff;
714 return 0;
716 m_sync_bufmode_pos=m_sync_bufmode_used=0;
719 m_syncrd_firstbuf=true;
720 #ifdef WDL_WIN32_NATIVE_READ
721 LONG high=(LONG) (m_file_position>>32);
722 return SetFilePointer(m_fh,(LONG)(m_file_position&((WDL_FILEREAD_POSTYPE)0xFFFFFFFF)),&high,FILE_BEGIN)==0xFFFFFFFF && GetLastError() != NO_ERROR;
723 #elif defined(WDL_POSIX_NATIVE_READ)
724 m_filedes_rdpos = m_file_position;
725 return false;
726 #else
727 return !!fseek(m_fp,m_file_position,SEEK_SET);
728 #endif
731 WDL_HeapBuf m_bufspace;
732 int m_sync_bufmode_used, m_sync_bufmode_pos;
734 WDL_FILEREAD_POSTYPE m_file_position,m_async_readpos;
735 WDL_FILEREAD_POSTYPE m_fsize;
737 void *m_mmap_view;
738 void *m_mmap_totalbufmode;
740 #ifdef WDL_WIN32_NATIVE_READ
741 HANDLE GetHandle() { return m_fh; }
742 HANDLE m_fh;
743 HANDLE m_mmap_fmap;
744 int m_mmap_size;
745 int m_async; // 1=nobuf, 2=buffered async, -1=unbuffered sync
747 int m_async_bufsize;
748 WDL_PtrList<WDL_FileRead__ReadEnt> m_empties;
749 WDL_PtrList<WDL_FileRead__ReadEnt> m_pending;
750 WDL_PtrList<WDL_FileRead__ReadEnt> m_full;
752 #elif defined(WDL_POSIX_NATIVE_READ)
753 WDL_FILEREAD_POSTYPE m_filedes_rdpos;
754 int m_filedes;
755 bool m_filedes_locked;
757 int GetHandle() { return m_filedes; }
758 #else
759 FILE *m_fp;
761 int GetHandle() { return fileno(m_fp); }
762 #endif
764 bool m_fsize_maychange;
765 bool m_syncrd_firstbuf;
766 bool m_async_hashaderr;
768 } WDL_FIXALIGN;
775 #endif