edt_os_nt.c

00001 
00002 
00003 #include "edt_utils.h"
00004 
00005 #include  <sys/stat.h>
00006 
00007 #include <direct.h>
00008 
00009 static LARGE_INTEGER frequency = {0,0};
00010 
00022 double
00023 edt_dtime()
00024 {
00025     double  dtime;
00026 
00027     static LARGE_INTEGER starttime;
00028 
00029     LARGE_INTEGER endtime;
00030 
00031     QueryPerformanceCounter(&endtime);
00032     if (frequency.QuadPart == 0)
00033         QueryPerformanceFrequency(&frequency);
00034 
00035     dtime = ((double) endtime.QuadPart - (double) starttime.QuadPart)
00036         / (double) frequency.QuadPart;
00037 
00038     starttime = endtime;
00039     return (dtime);
00040 }
00041 
00042 
00049 double
00050 edt_timestamp()
00051 {
00052     double  dtime;
00053 
00054     LARGE_INTEGER endtime;
00055 
00056     QueryPerformanceCounter(&endtime);
00057     if (frequency.QuadPart == 0)
00058         QueryPerformanceFrequency(&frequency);
00059 
00060     dtime = ((double) endtime.QuadPart)
00061         / (double) frequency.QuadPart
00062         ;
00063 
00064     return (dtime);
00065 }
00066 
00067 
00068 
00077 void
00078 edt_msleep(int msecs)
00079 {
00080 
00081     Sleep(msecs);
00082 
00083 }
00084 
00085 
00093 void
00094 edt_usleep(int usecs)
00095 {
00096     Sleep(usecs/1000);
00097 }
00098 
00099 void
00100 edt_usec_busywait(u_int usec)
00101 {
00102     double t = 0.0, f_usec = usec / 1000000;
00103 
00104     edt_dtime();           /* edt_dtime() returns a double containing the delta time since last called. */
00105     while (t < f_usec)
00106         t += edt_dtime();  /* spin until the sum of the deltas equals the input. */
00107 }
00108 
00109 
00110 HANDLE edt_open_datafile(const char *path,
00111                          const char *szFileName,
00112                          u_char writing,
00113                          u_char direct,
00114                          u_char truncate)
00115 {
00116 
00117     int ok = 0;
00118 
00119     HANDLE pFile;
00120 
00121     int rwmode;
00122 
00123     int createmode;
00124 
00125     int flags = 0;
00126 
00127     char name[256];
00128 
00129     if (path)
00130         sprintf(name, "%s/%s", path, szFileName);
00131     else
00132         sprintf(name, "%s",szFileName);
00133 
00134     createmode = CREATE_ALWAYS;
00135 
00136     if (direct) 
00137         flags = FILE_FLAG_NO_BUFFERING;
00138 
00139     if (writing)
00140     {
00141         rwmode = GENERIC_WRITE | GENERIC_READ;
00142         createmode = CREATE_ALWAYS;
00143         if (truncate)
00144         {
00145             if (access(name, 0) == 0)
00146                 createmode = TRUNCATE_EXISTING;
00147         }
00148     }
00149     else
00150     {
00151         rwmode = GENERIC_READ;
00152         createmode = OPEN_EXISTING;
00153     }
00154 
00155     pFile = CreateFile(name,
00156         rwmode,
00157         FILE_SHARE_READ,
00158         NULL,
00159         createmode,
00160         flags,
00161         NULL
00162         );
00163 
00164 
00165     ok = (pFile != NULL_HANDLE);
00166 
00167     if (pFile == NULL_HANDLE)
00168     {
00169         int error = GetLastError();
00170 
00171         printf("Error opening %s = %08x\n", name, error);
00172     }
00173     return pFile;
00174 }
00175 
00176 void edt_close_datafile(HANDLE f)
00177 
00178 {
00179     CloseHandle(f);
00180 }
00181 
00182 int edt_write_datafile(HANDLE f, void *p, int bufsize)
00183 
00184 {
00185 
00186     unsigned long rc;
00187 
00188     WriteFile(f,
00189         p,
00190         bufsize, 
00191         &rc,
00192         NULL);
00193 
00194     return rc;
00195 
00196 }
00197 
00198 int edt_read_datafile(HANDLE f, void *p, int bufsize)
00199 
00200 {
00201 
00202     unsigned long rc;
00203 
00204     ReadFile(f,
00205         p,
00206         bufsize, 
00207         &rc,
00208         NULL);
00209 
00210     if (rc == 0)
00211     {
00212         int error = GetLastError();
00213         printf("read_data_file: error = %d\n",error);
00214     }
00215     return rc;
00216 
00217 }
00218 
00219 void *edt_alloc_aligned(int size)
00220 
00221 {
00222     return (unsigned char *)
00223         VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
00224 }
00225 
00226 void edt_free_aligned(void *p)
00227 {
00228     VirtualFree(p,0,MEM_RELEASE);
00229 }
00230 
00231 
00232 #include <conio.h>
00233 
00234 u_char edt_wait_for_console_input(char *line,
00235                                   const int timeout,  
00236                                   const int maxlen)
00237 
00238 {
00239 
00240     int t = 0;
00241 
00242     while (t < timeout && !kbhit())
00243     {
00244         edt_msleep(10);
00245         t += 10;
00246     }
00247 
00248     if (t < timeout)
00249     {
00250         fgets(line, maxlen, stdin);
00251         if (strlen(line))
00252             if (line[strlen(line)-1] == '\n')
00253                 line[strlen(line)-1] = 0;
00254 
00255         return TRUE;
00256     }
00257     else
00258         return FALSE;
00259 
00260 }
00261 #include <sys/timeb.h>
00262 
00263 #include <time.h>
00264 
00265 int edt_get_datestr(char *s, int maxlen)
00266 
00267 {
00268     time_t tm;
00269 
00270     time(&tm);
00271 
00272     strncpy(s,ctime(&tm),maxlen);
00273 
00274     if (s[strlen(s)-1] == '\n')
00275         s[strlen(s)-1] = 0;
00276 
00277     return 0;
00278 }
00279 
00280 uint64_t
00281 edt_get_file_position(HANDLE f)
00282 
00283 {
00284     uint64_t rc;
00285 
00286     LARGE_INTEGER set;
00287 
00288     set.QuadPart = 0;
00289 
00290     set.LowPart = SetFilePointer(f, set.LowPart, &set.HighPart, FILE_CURRENT);
00291 
00292     rc = set.QuadPart;
00293 
00294     return rc;
00295 }
00296 
00297 uint64_t
00298 edt_get_file_size(HANDLE f)
00299 
00300 {
00301     LARGE_INTEGER pos;
00302 
00303     pos.LowPart = GetFileSize(f, (LPDWORD)&pos.HighPart);
00304 
00305     return pos.QuadPart;
00306 
00307 }
00308 
00309 uint64_t edt_file_seek(HANDLE f, uint64_t to)
00310 
00311 {
00312     uint64_t rc;
00313 
00314     LARGE_INTEGER set;
00315 
00316     set.QuadPart = to;
00317 
00318     set.LowPart = SetFilePointer(f, set.LowPart, &set.HighPart, FILE_BEGIN);
00319 
00320     rc = set.QuadPart;
00321 
00322     return rc;
00323 }
00324 
00336 unsigned char *
00337 edt_alloc(int size)
00338 {
00339     unsigned char *ret;
00340 
00341     ret = (unsigned char *)
00342         VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
00343 
00344     return (ret);
00345 }
00346 
00355 void
00356 edt_free(uchar_t *ptr)
00357 {
00358 
00359     VirtualFree(ptr, 0, MEM_RELEASE);
00360 
00361 }
00362 
00363 static double elapsed_start = 0.0;
00364 
00370 double 
00371 edt_elapsed(u_char reset)
00372 {
00373 
00374     if (reset || elapsed_start == 0.0)
00375     {
00376         elapsed_start = edt_timestamp();
00377     }
00378 
00379     return edt_timestamp() - elapsed_start;
00380 }
00381 
00382 /*
00383 * below are utility routines, intended primarily for EDT library
00384 * internal use but available for application use if desired
00385 */
00386 
00390 void
00391 edt_fwd_to_back(char *str)
00392 {
00393     char   *p = str;
00394 
00395     while (*p != '\0')
00396     {
00397         if (*p == '/')
00398             *p = '\\';
00399         ++p;
00400     }
00401 }
00402 
00403 
00407 void
00408 edt_back_to_fwd(char *str)
00409 {
00410     char   *p = str;
00411 
00412     while (*p != '\0')
00413     {
00414         if (*p == '\\')
00415             *p = '/';
00416         ++p;
00417     }
00418 }
00419 
00420 
00421 
00426 void
00427 edt_correct_slashes(char *str)
00428 {
00429     edt_fwd_to_back(str);
00430 }
00431 
00451 int
00452 edt_access(char *fname, int perm)
00453 {
00454     int     ret;
00455 
00456     edt_correct_slashes(fname);
00457     ret = _access(fname, perm);
00458 
00459     return ret;
00460 }
00461 
00462 
00463 /* emulate opendir/readdir */
00490 char    Next_name[256];
00491 DWORD   Err = 0;
00492 
00493 HANDLE
00494 edt_opendir(const char *dirname)
00495 {
00496     WIN32_FIND_DATA fd;
00497     HANDLE  h;
00498     char    searchname[256];
00499 
00500     /*
00501     * This fixes a bug where second time through  this function fails
00502     */
00503     SetLastError(0);
00504 
00505     sprintf(searchname, "%s\\*", dirname);
00506     if ((h = FindFirstFile(searchname, &fd)) == INVALID_HANDLE_VALUE)
00507         return NULL;
00508     if ((Err = GetLastError()) != ERROR_NO_MORE_FILES)
00509         strcpy(Next_name, fd.cFileName);
00510 
00511     return h;
00512 }
00513 
00514 int
00515 edt_readdir(HANDLE h, char *name)
00516 {
00517 
00518     WIN32_FIND_DATA fd;
00519     int rc;
00520 
00521     /* get file and check error from previous FindFirstFile or FindNextFile */
00522     if (Err == ERROR_NO_MORE_FILES)
00523         return 0;
00524 
00525     strcpy(name, Next_name);
00526 
00527     /* get the next one */
00528     rc = FindNextFile(h, &fd);
00529     Err = GetLastError();
00530     if (rc)
00531     {
00532         strcpy(Next_name, fd.cFileName);
00533     }
00534     else
00535         Next_name[0] = 0;
00536 
00537     return 1;
00538 }
00539 
00540 void
00541 edt_closedir(HANDLE h)
00542 {
00543     FindClose(h);
00544 }
00545 
00551 int64_t edt_disk_free(const char *name)
00552 
00553 {
00554     ULARGE_INTEGER freebytes;
00555     ULARGE_INTEGER totalbytes;
00556     ULARGE_INTEGER totalfree;
00557 
00558     int rc;
00559 
00560     rc = GetDiskFreeSpaceEx(name, &freebytes, &totalbytes, &totalfree);
00561 
00562     if (rc)
00563         return freebytes.QuadPart;
00564     else
00565         return -1;
00566 
00567 }
00568 
00569 int64_t edt_disk_size(const char *name)
00570 
00571 {
00572     ULARGE_INTEGER freebytes;
00573     ULARGE_INTEGER totalbytes;
00574     ULARGE_INTEGER totalfree;
00575 
00576     int rc;
00577 
00578     rc = GetDiskFreeSpaceEx(name, &freebytes, &totalbytes, &totalfree);
00579 
00580     if (rc)
00581         return totalbytes.QuadPart;
00582     else
00583         return -1;
00584 
00585 }
00586 
00587 int64_t edt_disk_used(const char *name)
00588 
00589 {
00590     ULARGE_INTEGER freebytes;
00591     ULARGE_INTEGER totalbytes;
00592     ULARGE_INTEGER totalfree;
00593 
00594     int rc;
00595 
00596     rc = GetDiskFreeSpaceEx(name, &freebytes, &totalbytes, &totalfree);
00597 
00598     if (rc)
00599         return totalbytes.QuadPart - freebytes.QuadPart;
00600     else
00601         return -1;
00602 
00603 }
00604 
00605 int edt_file_exists(const char *name, int rdonly)
00606 
00607 {
00608     int rc = access(name, (rdonly) ? 4:6);
00609     return (rc == 0);
00610 }
00611 
00612 int edt_mkdir(const char *name, int access)
00613 
00614 {
00615     return _mkdir(name);
00616 }
00617 
00618 
00619 int edt_mkdir_p(const char *name, int access)
00620 
00621 {
00622 
00623     if (edt_file_exists(name, access))
00624     {
00625         return 0;
00626     }
00627     else
00628         return _mkdir(name);
00629 
00630 #if 0
00631     stat(snapshotDir.c_str(), &st);
00632         if (!S_ISDIR(st.st_mode))
00633             mkdir(snapshotDir.c_str(), S_IRWXU);
00634 #endif
00635 
00636     
00637 }

Generated on 19 Jun 2015 by  doxygen 1.4.7