rhino/demo/c/dr_libs/tests/common/dr_common.c
Go to the documentation of this file.
1 
2 #ifdef _WIN32
3 #include <windows.h>
4 #endif
5 
6 #if defined(_MSC_VER) || defined(__DMC__)
7 #else
8 #include <strings.h>
9 #endif
10 
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <time.h> /* So we can seed the random number generator based on time. */
14 #include <errno.h>
15 
16 #if !defined(_WIN32)
17 #include <dirent.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <unistd.h>
21 #include <dlfcn.h>
22 #endif
23 
24 #include <stddef.h> /* For size_t. */
25 
26 /* Sized types. Prefer built-in types. Fall back to stdint. */
27 #ifdef _MSC_VER
28  #if defined(__clang__)
29  #pragma GCC diagnostic push
30  #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
31  #pragma GCC diagnostic ignored "-Wlong-long"
32  #pragma GCC diagnostic ignored "-Wc++11-long-long"
33  #endif
34  typedef signed __int8 dr_int8;
35  typedef unsigned __int8 dr_uint8;
36  typedef signed __int16 dr_int16;
37  typedef unsigned __int16 dr_uint16;
38  typedef signed __int32 dr_int32;
39  typedef unsigned __int32 dr_uint32;
40  typedef signed __int64 dr_int64;
41  typedef unsigned __int64 dr_uint64;
42  #if defined(__clang__)
43  #pragma GCC diagnostic pop
44  #endif
45 #else
46  #define MA_HAS_STDINT
47  #include <stdint.h>
48  typedef int8_t dr_int8;
49  typedef uint8_t dr_uint8;
50  typedef int16_t dr_int16;
51  typedef uint16_t dr_uint16;
52  typedef int32_t dr_int32;
53  typedef uint32_t dr_uint32;
54  typedef int64_t dr_int64;
55  typedef uint64_t dr_uint64;
56 #endif
57 
58 #ifdef MA_HAS_STDINT
59  typedef uintptr_t dr_uintptr;
60 #else
61  #if defined(_WIN32)
62  #if defined(_WIN64)
63  typedef dr_uint64 dr_uintptr;
64  #else
65  typedef dr_uint32 dr_uintptr;
66  #endif
67  #elif defined(__GNUC__)
68  #if defined(__LP64__)
69  typedef dr_uint64 dr_uintptr;
70  #else
71  typedef dr_uint32 dr_uintptr;
72  #endif
73  #else
74  typedef dr_uint64 dr_uintptr; /* Fallback. */
75  #endif
76 #endif
77 
80 #define DR_TRUE 1
81 #define DR_FALSE 0
82 
83 typedef void* dr_handle;
84 typedef void* dr_ptr;
85 typedef void (* dr_proc)(void);
86 
87 #if defined(SIZE_MAX)
88  #define DR_SIZE_MAX SIZE_MAX
89 #else
90  #define DR_SIZE_MAX 0xFFFFFFFF
91 #endif
92 
93 /*
94 Return Values:
95  0: Success
96  22: EINVAL
97  34: ERANGE
98 
99 Not using symbolic constants for errors because I want to avoid #including errno.h
100 */
101 int dr_strcpy_s(char* dst, size_t dstSizeInBytes, const char* src)
102 {
103  size_t i;
104 
105  if (dst == 0) {
106  return 22;
107  }
108  if (dstSizeInBytes == 0) {
109  return 34;
110  }
111  if (src == 0) {
112  dst[0] = '\0';
113  return 22;
114  }
115 
116  for (i = 0; i < dstSizeInBytes && src[i] != '\0'; ++i) {
117  dst[i] = src[i];
118  }
119 
120  if (i < dstSizeInBytes) {
121  dst[i] = '\0';
122  return 0;
123  }
124 
125  dst[0] = '\0';
126  return 34;
127 }
128 
129 int dr_strncpy_s(char* dst, size_t dstSizeInBytes, const char* src, size_t count)
130 {
131  size_t maxcount;
132  size_t i;
133 
134  if (dst == 0) {
135  return 22;
136  }
137  if (dstSizeInBytes == 0) {
138  return 34;
139  }
140  if (src == 0) {
141  dst[0] = '\0';
142  return 22;
143  }
144 
145  maxcount = count;
146  if (count == ((size_t)-1) || count >= dstSizeInBytes) { /* -1 = _TRUNCATE */
147  maxcount = dstSizeInBytes - 1;
148  }
149 
150  for (i = 0; i < maxcount && src[i] != '\0'; ++i) {
151  dst[i] = src[i];
152  }
153 
154  if (src[i] == '\0' || i == count || count == ((size_t)-1)) {
155  dst[i] = '\0';
156  return 0;
157  }
158 
159  dst[0] = '\0';
160  return 34;
161 }
162 
163 int dr_strcat_s(char* dst, size_t dstSizeInBytes, const char* src)
164 {
165  char* dstorig;
166 
167  if (dst == 0) {
168  return 22;
169  }
170  if (dstSizeInBytes == 0) {
171  return 34;
172  }
173  if (src == 0) {
174  dst[0] = '\0';
175  return 22;
176  }
177 
178  dstorig = dst;
179 
180  while (dstSizeInBytes > 0 && dst[0] != '\0') {
181  dst += 1;
182  dstSizeInBytes -= 1;
183  }
184 
185  if (dstSizeInBytes == 0) {
186  return 22; /* Unterminated. */
187  }
188 
189 
190  while (dstSizeInBytes > 0 && src[0] != '\0') {
191  *dst++ = *src++;
192  dstSizeInBytes -= 1;
193  }
194 
195  if (dstSizeInBytes > 0) {
196  dst[0] = '\0';
197  } else {
198  dstorig[0] = '\0';
199  return 34;
200  }
201 
202  return 0;
203 }
204 
205 int dr_strncat_s(char* dst, size_t dstSizeInBytes, const char* src, size_t count)
206 {
207  char* dstorig;
208 
209  if (dst == 0) {
210  return 22;
211  }
212  if (dstSizeInBytes == 0) {
213  return 34;
214  }
215  if (src == 0) {
216  return 22;
217  }
218 
219  dstorig = dst;
220 
221  while (dstSizeInBytes > 0 && dst[0] != '\0') {
222  dst += 1;
223  dstSizeInBytes -= 1;
224  }
225 
226  if (dstSizeInBytes == 0) {
227  return 22; /* Unterminated. */
228  }
229 
230 
231  if (count == ((size_t)-1)) { /* _TRUNCATE */
232  count = dstSizeInBytes - 1;
233  }
234 
235  while (dstSizeInBytes > 0 && src[0] != '\0' && count > 0) {
236  *dst++ = *src++;
237  dstSizeInBytes -= 1;
238  count -= 1;
239  }
240 
241  if (dstSizeInBytes > 0) {
242  dst[0] = '\0';
243  } else {
244  dstorig[0] = '\0';
245  return 34;
246  }
247 
248  return 0;
249 }
250 
251 /*
252 String Helpers
253 */
254 int dr_append_path(char* dst, size_t dstSize, const char* base, const char* other)
255 {
256  int err;
257  size_t len;
258 
259  /* TODO: Return the correct error codes here. */
260  if (dst == NULL) {
261  return -1;
262  }
263  if (base == NULL || other == NULL) {
264  return -1;
265  }
266 
267  err = dr_strcpy_s(dst, dstSize, base);
268  if (err != 0) {
269  return err;
270  }
271 
272  len = strlen(dst);
273  if (len > 0) {
274  /* Append the slash if required. */
275  if (dst[len-1] != '/' && dst[len-1] != '\\') {
276  err = dr_strcat_s(dst, dstSize, "/");
277  if (err != 0) {
278  dst[0] = '\0';
279  return err;
280  }
281 
282  len += 1; /* +1 to the length to account for the slash. */
283  }
284  }
285 
286  err = dr_strcat_s(dst, dstSize, other);
287  if (err != 0) {
288  dst[0] = '\0';
289  return err;
290  }
291 
292  /* Success. */
293  return 0;
294 }
295 
296 const char* dr_path_file_name(const char* path)
297 {
298  const char* fileName = path;
299 
300  if (path == NULL) {
301  return NULL;
302  }
303 
304  /* We just loop through the path until we find the last slash. */
305  while (path[0] != '\0') {
306  if (path[0] == '/' || path[0] == '\\') {
307  fileName = path;
308  }
309 
310  path += 1;
311  }
312 
313  /* At this point the file name is sitting on a slash, so just move forward. */
314  while (fileName[0] != '\0' && (fileName[0] == '/' || fileName[0] == '\\')) {
315  fileName += 1;
316  }
317 
318  return fileName;
319 }
320 
321 const char* dr_extension(const char* path)
322 {
323  const char* extension = path;
324  const char* lastoccurance = NULL;
325 
326  if (path == NULL) {
327  return NULL;
328  }
329 
330  /* Just find the last '.' and return. */
331  while (extension[0] != '\0') {
332  if (extension[0] == '.') {
333  extension += 1;
334  lastoccurance = extension;
335  }
336 
337  extension += 1;
338  }
339 
340  return (lastoccurance != 0) ? lastoccurance : extension;
341 }
342 
343 dr_bool32 dr_extension_equal(const char* path, const char* extension)
344 {
345  const char* ext1;
346  const char* ext2;
347 
348  if (path == NULL || extension == NULL) {
349  return 0;
350  }
351 
352  ext1 = extension;
353  ext2 = dr_extension(path);
354 
355 #if defined(_MSC_VER) || defined(__DMC__)
356  return _stricmp(ext1, ext2) == 0;
357 #else
358  return strcasecmp(ext1, ext2) == 0;
359 #endif
360 }
361 
362 
363 
364 
365 /*
366 File Iterator
367 
368 dr_file_iterator state;
369 dr_file_iterator* pFile = dr_file_iterator_begin("the/folder/path", &state);
370 while (pFile != NULL) {
371  // Do something with pFile.
372  pFile = dr_file_iterator_next(pFile);
373 }
374 
375 Limitations:
376  - Only supports file paths up to 256 characters.
377 */
378 typedef struct
379 {
380  char folderPath[256];
381  char relativePath[256]; /* Relative to the original folder path. */
382  char absolutePath[256]; /* Concatenation of folderPath and relativePath. */
383  dr_bool32 isDirectory;
384 #ifdef _WIN32
385  HANDLE hFind;
386 #else
387  DIR* dir;
388 #endif
390 
391 dr_file_iterator* dr_file_iterator_begin(const char* pFolderPath, dr_file_iterator* pState)
392 {
393 #ifdef _WIN32
394  char searchQuery[MAX_PATH];
395  unsigned int searchQueryLength;
396  WIN32_FIND_DATAA ffd;
397  HANDLE hFind;
398 #else
399  DIR* dir;
400 #endif
401 
402  if (pState == NULL) {
403  return NULL;
404  }
405 
406  memset(pState, 0, sizeof(*pState));
407 
408  if (pFolderPath == NULL) {
409  return NULL;
410  }
411 
412 #ifdef _WIN32
413  dr_strcpy_s(searchQuery, sizeof(searchQuery), pFolderPath);
414 
415  searchQueryLength = (unsigned int)strlen(searchQuery);
416  if (searchQueryLength >= MAX_PATH - 3) {
417  return NULL; /* Path is too long. */
418  }
419 
420  searchQuery[searchQueryLength + 0] = '\\';
421  searchQuery[searchQueryLength + 1] = '*';
422  searchQuery[searchQueryLength + 2] = '\0';
423 
424  hFind = FindFirstFileA(searchQuery, &ffd);
425  if (hFind == INVALID_HANDLE_VALUE) {
426  return NULL; /* Failed to begin search. */
427  }
428 
429  /* Skip past "." and ".." directories. */
430  while (strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, "..") == 0) {
431  if (!FindNextFileA(hFind, &ffd)) {
432  FindClose(hFind);
433  return NULL; /* Couldn't find anything. */
434  }
435  }
436 
437  pState->hFind = hFind;
438 
439 
440  if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
441  pState->isDirectory = 1;
442  } else {
443  pState->isDirectory = 0;
444  }
445 
446  dr_strcpy_s(pState->relativePath, sizeof(pState->relativePath), ffd.cFileName);
447 #else
448  dir = opendir(pFolderPath);
449  if (dir == NULL) {
450  return NULL; /* Couldn't find anything. */
451  }
452 
453  /* Select the first file. */
454  for (;;) {
455  struct dirent* info;
456  struct stat fileinfo;
457  char filePath[4096];
458 
459  info = readdir(dir);
460  if (info == NULL) {
461  closedir(dir);
462  return NULL;
463  }
464 
465  if (strcmp(info->d_name, ".") == 0 || strcmp(info->d_name, "..") == 0) {
466  continue; /* Skip past "." and ".." directories. */
467  }
468 
469  dr_strcpy_s(pState->relativePath, sizeof(pState->relativePath), info->d_name);
470  dr_append_path(filePath, sizeof(filePath), pFolderPath, pState->relativePath);
471 
472  if (stat(filePath, &fileinfo) != 0) {
473  continue;
474  }
475 
476  if (S_ISDIR(fileinfo.st_mode)) {
477  pState->isDirectory = 1;
478  } else {
479  pState->isDirectory = 0;
480  }
481 
482  break;
483  }
484 
485  pState->dir = dir;
486 #endif
487 
488  /* Getting here means everything was successful. We can now set some state before returning. */
489  dr_strcpy_s(pState->folderPath, sizeof(pState->folderPath), pFolderPath);
490  dr_append_path(pState->absolutePath, sizeof(pState->absolutePath), pState->folderPath, pState->relativePath);
491 
492  return pState;
493 }
494 
496 {
497 #ifdef _WIN32
498  WIN32_FIND_DATAA ffd;
499 #endif
500 
501  if (pState == NULL) {
502  return NULL;
503  }
504 
505 #ifdef _WIN32
506  if (!FindNextFileA(pState->hFind, &ffd)) {
507  /* Couldn't find anything else. */
508  FindClose(pState->hFind);
509  pState->hFind = NULL;
510  return NULL;
511  }
512 
513  /* Found something. */
514  if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
515  pState->isDirectory = 1;
516  } else {
517  pState->isDirectory = 0;
518  }
519 
520  dr_strcpy_s(pState->relativePath, sizeof(pState->relativePath), ffd.cFileName);
521 #else
522  /* Enter a loop here so we can skip past "." and ".." directories. */
523  for (;;) {
524  struct dirent* info;
525  struct stat fileinfo;
526  char filePath[4096];
527 
528  info = readdir(pState->dir);
529  if (info == NULL) {
530  closedir(pState->dir);
531  pState->dir = NULL;
532  return NULL;
533  }
534 
535  if (strcmp(info->d_name, ".") == 0 || strcmp(info->d_name, "..") == 0) {
536  continue; /* Skip past "." and ".." directories. */
537  }
538 
539  dr_strcpy_s(pState->relativePath, sizeof(pState->relativePath), info->d_name);
540  dr_append_path(filePath, sizeof(filePath), pState->folderPath, pState->relativePath);
541 
542  /*printf("Done: %s\n", pState->relativePath);*/
543 
544  if (stat(filePath, &fileinfo) != 0) {
545  continue;
546  }
547 
548  if (S_ISDIR(fileinfo.st_mode)) {
549  pState->isDirectory = 1;
550  } else {
551  pState->isDirectory = 0;
552  }
553 
554  break;
555  }
556 #endif
557 
558  /* Success */
559  dr_append_path(pState->absolutePath, sizeof(pState->absolutePath), pState->folderPath, pState->relativePath);
560  return pState;
561 }
562 
564 {
565  if (pState == NULL) {
566  return;
567  }
568 
569 #ifdef _WIN32
570  FindClose(pState->hFind);
571  pState->hFind = NULL;
572 #else
573  closedir(pState->dir);
574  pState->dir = NULL;
575 #endif
576 }
577 
578 
579 /*
580 File Management
581 
582 Free file data with free().
583 */
584 static int dr_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
585 {
586 #if defined(_MSC_VER) && _MSC_VER >= 1400
587  errno_t err;
588 #endif
589 
590  if (ppFile != NULL) {
591  *ppFile = NULL; /* Safety. */
592  }
593 
594  if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
595  return -1; /* Invalid args. */
596  }
597 
598 #if defined(_MSC_VER) && _MSC_VER >= 1400
599  err = fopen_s(ppFile, pFilePath, pOpenMode);
600  if (err != 0) {
601  return err;
602  }
603 #else
604 #if defined(_WIN32) || defined(__APPLE__)
605  *ppFile = fopen(pFilePath, pOpenMode);
606 #else
607  #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
608  *ppFile = fopen64(pFilePath, pOpenMode);
609  #else
610  *ppFile = fopen(pFilePath, pOpenMode);
611  #endif
612 #endif
613  if (*ppFile == NULL) {
614  return errno;
615  }
616 #endif
617 
618  return 0;
619 }
620 
621 void* dr_open_and_read_file_with_extra_data(const char* pFilePath, size_t* pFileSizeOut, size_t extraBytes)
622 {
623  FILE* pFile;
624  size_t fileSize;
625  size_t bytesRead;
626  void* pFileData;
627 
628  /* Safety. */
629  if (pFileSizeOut) {
630  *pFileSizeOut = 0;
631  }
632 
633  if (pFilePath == NULL) {
634  return NULL;
635  }
636 
637  /* TODO: Use 64-bit versions of the FILE APIs. */
638 
639  if (dr_fopen(&pFile, pFilePath, "rb") != 0) {
640  return NULL;
641  }
642 
643  fseek(pFile, 0, SEEK_END);
644  fileSize = ftell(pFile);
645  fseek(pFile, 0, SEEK_SET);
646 
647  /* Need to make sure we have enough room for the extra bytes, if any. */
648  if (fileSize == DR_SIZE_MAX && extraBytes > 0) {
649  fclose(pFile);
650  return NULL; /* File is too big. */
651  }
652 
653  pFileData = malloc((size_t)fileSize + extraBytes); /* <-- Safe cast due to the check above. */
654  if (pFileData == NULL) {
655  fclose(pFile);
656  return NULL; /* Failed to allocate memory for the file. Good chance the file is too big. */
657  }
658 
659  bytesRead = fread(pFileData, 1, (size_t)fileSize, pFile);
660  if (bytesRead != fileSize) {
661  free(pFileData);
662  fclose(pFile);
663  return NULL; /* Failed to read every byte from the file. */
664  }
665 
666  fclose(pFile);
667 
668  if (pFileSizeOut) {
669  *pFileSizeOut = (size_t)fileSize;
670  }
671 
672  return pFileData;
673 }
674 
675 void* dr_open_and_read_file(const char* pFilePath, size_t* pFileSizeOut)
676 {
677  return dr_open_and_read_file_with_extra_data(pFilePath, pFileSizeOut, 0);
678 }
679 
680 
681 dr_bool32 dr_argv_is_set(int argc, char** argv, const char* value)
682 {
683  int iarg;
684  for (iarg = 0; iarg < argc; ++iarg) {
685  if (strcmp(argv[iarg], value) == 0) {
686  return DR_TRUE;
687  }
688  }
689 
690  return DR_FALSE;
691 }
692 
693 
694 int dr_vprintf_fixed(int width, const char* const format, va_list args)
695 {
696  int i;
697  int len;
698  char buffer[4096];
699 
700  if (width <= 0) {
701  return -1; /* Width cannot be negative or 0. */
702  }
703 
704  if ((unsigned int)width > sizeof(buffer)) {
705  return -1; /* Width is too big. */
706  }
707 
708  /* We need to print this into a string (truncated). */
709 #if (defined(_MSC_VER) && _MSC_VER > 1200) || ((defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || defined(_ISOC99_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L))
710  len = vsnprintf(buffer, width+1, format, args);
711 #else
712  len = vsprintf(buffer, format, args);
713  if (len > width) {
714  len = width;
715  }
716 
717  buffer[len] = '\0';
718 #endif
719 
720  printf("%s", buffer);
721  for (i = len; i < width; ++i) {
722  printf(" ");
723  }
724 
725 
726  return len;
727 }
728 
729 int dr_printf_fixed(int width, const char* const format, ...)
730 {
731  int result;
732 
733  va_list args;
734  va_start(args, format);
735  {
736  result = dr_vprintf_fixed(width, format, args);
737  }
738  va_end(args);
739 
740  return result;
741 }
742 
743 int dr_vprintf_fixed_with_margin(int width, int margin, const char* const format, va_list args)
744 {
745  int i;
746 
747  /* Margin. */
748  for (i = 0; i < margin; ++i) {
749  printf(" ");
750  }
751 
752  return dr_vprintf_fixed(width - margin, format, args);
753 }
754 
755 int dr_printf_fixed_with_margin(int width, int margin, const char* const format, ...)
756 {
757  int result;
758 
759  va_list args;
760  va_start(args, format);
761  {
762  result = dr_vprintf_fixed_with_margin(width, margin, format, args);
763  }
764  va_end(args);
765 
766  return result;
767 }
768 
769 
770 
771 #ifdef _WIN32
772 static LARGE_INTEGER g_DRTimerFrequency = {{0}};
773 double dr_timer_now()
774 {
775  LARGE_INTEGER counter;
776 
777  if (g_DRTimerFrequency.QuadPart == 0) {
778  QueryPerformanceFrequency(&g_DRTimerFrequency);
779  }
780 
781  QueryPerformanceCounter(&counter);
782 
783  return counter.QuadPart / (double)g_DRTimerFrequency.QuadPart;
784 }
785 #else
786 #if _POSIX_C_SOURCE >= 199309L
787  #if defined(CLOCK_MONOTONIC)
788  #define MA_CLOCK_ID CLOCK_MONOTONIC
789  #else
790  #define MA_CLOCK_ID CLOCK_REALTIME
791  #endif
792  double dr_timer_now()
793  {
794  struct timespec newTime;
795  clock_gettime(CLOCK_MONOTONIC, &newTime);
796 
797  return ((newTime.tv_sec * 1000000000LL) + newTime.tv_nsec) / 1000000000.0;
798  }
799 #else
800  double dr_timer_now()
801  {
802  struct timeval newTime;
803  gettimeofday(&newTime, NULL);
804 
805  return ((newTime.tv_sec * 1000000) + newTime.tv_usec) / 1000000.0;
806  }
807 #endif
808 #endif
809 
810 
811 float dr_scale_to_range_f32(float x, float lo, float hi)
812 {
813  return lo + x*(hi-lo);
814 }
815 
816 
817 #define DR_LCG_M 2147483647
818 #define DR_LCG_A 48271
819 #define DR_LCG_C 0
820 static int g_drLCG;
821 
822 void dr_seed(int seed)
823 {
824  g_drLCG = seed;
825 }
826 
828 {
829  int lcg = g_drLCG;
830  int r = (DR_LCG_A * lcg + DR_LCG_C) % DR_LCG_M;
831  g_drLCG = r;
832  return r;
833 }
834 
835 unsigned int dr_rand_u32()
836 {
837  return (unsigned int)dr_rand_s32();
838 }
839 
841 {
842  return ((dr_uint64)dr_rand_u32() << 32) | dr_rand_u32();
843 }
844 
845 double dr_rand_f64()
846 {
847  return dr_rand_s32() / (double)0x7FFFFFFF;
848 }
849 
850 float dr_rand_f32()
851 {
852  return (float)dr_rand_f64();
853 }
854 
855 float dr_rand_range_f32(float lo, float hi)
856 {
857  return dr_scale_to_range_f32(dr_rand_f32(), lo, hi);
858 }
859 
860 int dr_rand_range_s32(int lo, int hi)
861 {
862  if (lo == hi) {
863  return lo;
864  }
865 
866  return lo + dr_rand_u32() / (0xFFFFFFFF / (hi - lo + 1) + 1);
867 }
868 
870 {
871  if (lo == hi) {
872  return lo;
873  }
874 
875  return lo + dr_rand_u64() / ((~(dr_uint64)0) / (hi - lo + 1) + 1);
876 }
877 
878 
879 
880 void dr_pcm_s32_to_f32(void* dst, const void* src, dr_uint64 count)
881 {
882  float* dst_f32 = (float*)dst;
883  const dr_int32* src_s32 = (const dr_int32*)src;
884 
885  dr_uint64 i;
886  for (i = 0; i < count; i += 1) {
887  double x = src_s32[i];
888 
889 #if 0
890  x = x + 2147483648.0;
891  x = x * 0.0000000004656612873077392578125;
892  x = x - 1;
893 #else
894  x = x / 2147483648.0;
895 #endif
896 
897  dst_f32[i] = (float)x;
898  }
899 }
900 
901 void dr_pcm_s32_to_s16(void* dst, const void* src, dr_uint64 count)
902 {
903  dr_int16* dst_s16 = (dr_int16*)dst;
904  const dr_int32* src_s32 = (const dr_int32*)src;
905 
906  dr_uint64 i;
907  for (i = 0; i < count; i += 1) {
908  dr_int32 x = src_s32[i];
909  x = x >> 16;
910  dst_s16[i] = (dr_int16)x;
911  }
912 }
913 
914 
915 
916 
917 dr_handle dr_dlopen(const char* filename)
918 {
919  dr_handle handle;
920 
921 #ifdef _WIN32
922  handle = (dr_handle)LoadLibraryA(filename);
923 #else
924  handle = (dr_handle)dlopen(filename, RTLD_NOW);
925 #endif
926 
927  return handle;
928 }
929 
930 void dr_dlclose(dr_handle handle)
931 {
932 #ifdef _WIN32
933  FreeLibrary((HMODULE)handle);
934 #else
935  dlclose((void*)handle);
936 #endif
937 }
938 
939 dr_proc dr_dlsym(dr_handle handle, const char* symbol)
940 {
941  dr_proc proc;
942 
943 #ifdef _WIN32
944  proc = (dr_proc)GetProcAddress((HMODULE)handle, symbol);
945 #else
946 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
947  #pragma GCC diagnostic push
948  #pragma GCC diagnostic ignored "-Wpedantic"
949 #endif
950  proc = (dr_proc)dlsym((void*)handle, symbol);
951 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
952  #pragma GCC diagnostic pop
953 #endif
954 #endif
955 
956  return proc;
957 }
dr_file_iterator_begin
dr_file_iterator * dr_file_iterator_begin(const char *pFolderPath, dr_file_iterator *pState)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:391
dr_uint8
uint8_t dr_uint8
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:49
dr_rand_f64
double dr_rand_f64()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:845
dr_uint32
uint32_t dr_uint32
Definition: porcupine/demo/c/dr_libs/old/dr.h:65
dr_rand_s32
int dr_rand_s32()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:827
DR_TRUE
#define DR_TRUE
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:80
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
dr_int32
int32_t dr_int32
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:52
DR_SIZE_MAX
#define DR_SIZE_MAX
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:90
errno
int errno
dr_handle
void * dr_handle
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:83
_stricmp
DR_INLINE int _stricmp(const char *string1, const char *string2)
Definition: porcupine/demo/c/dr_libs/old/dr.h:178
time.h
dr_scale_to_range_f32
float dr_scale_to_range_f32(float x, float lo, float hi)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:811
dr_int16
int16_t dr_int16
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:50
dr_printf_fixed
int dr_printf_fixed(int width, const char *const format,...)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:729
dr_int16
int16_t dr_int16
Definition: porcupine/demo/c/dr_libs/old/dr.h:62
dr_rand_range_f32
float dr_rand_range_f32(float lo, float hi)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:855
dr_uint64
uint64_t dr_uint64
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:55
dr_file_iterator::isDirectory
dr_bool32 isDirectory
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:383
dr_path_file_name
const char * dr_path_file_name(const char *path)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:296
dr_rand_u64
dr_uint64 dr_rand_u64()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:840
DR_LCG_M
#define DR_LCG_M
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:817
dr_dlsym
dr_proc dr_dlsym(dr_handle handle, const char *symbol)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:939
dr_seed
void dr_seed(int seed)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:822
dr_file_iterator
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:378
dr_rand_range_u64
dr_uint64 dr_rand_range_u64(dr_uint64 lo, dr_uint64 hi)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:869
dr_bool8
dr_uint8 dr_bool8
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:78
dr_file_iterator_next
dr_file_iterator * dr_file_iterator_next(dr_file_iterator *pState)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:495
DR_LCG_C
#define DR_LCG_C
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:819
dr_rand_u32
unsigned int dr_rand_u32()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:835
dr_uintptr
uintptr_t dr_uintptr
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:59
dr_open_and_read_file
void * dr_open_and_read_file(const char *pFilePath, size_t *pFileSizeOut)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:675
dr_file_iterator::relativePath
char relativePath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:381
dr_printf_fixed_with_margin
int dr_printf_fixed_with_margin(int width, int margin, const char *const format,...)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:755
dr_append_path
int dr_append_path(char *dst, size_t dstSize, const char *base, const char *other)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:254
dr_file_iterator_end
void dr_file_iterator_end(dr_file_iterator *pState)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:563
dr_bool32
dr_uint32 dr_bool32
Definition: porcupine/demo/c/dr_libs/old/dr.h:70
dr_int8
int8_t dr_int8
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:48
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
dr_uint64
uint64_t dr_uint64
Definition: porcupine/demo/c/dr_libs/old/dr.h:67
dr_uint8
uint8_t dr_uint8
Definition: porcupine/demo/c/dr_libs/old/dr.h:61
dr_extension
const char * dr_extension(const char *path)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:321
dr_pcm_s32_to_s16
void dr_pcm_s32_to_s16(void *dst, const void *src, dr_uint64 count)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:901
dr_uint32
uint32_t dr_uint32
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:53
dr_dlclose
void dr_dlclose(dr_handle handle)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:930
dr_bool32
dr_uint32 dr_bool32
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:79
dr_file_iterator::folderPath
char folderPath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:380
dr_timer_now
double dr_timer_now()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:800
dr_proc
void(* dr_proc)(void)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:85
g_drLCG
static int g_drLCG
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:820
dr_rand_f32
float dr_rand_f32()
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:850
dr_pcm_s32_to_f32
void dr_pcm_s32_to_f32(void *dst, const void *src, dr_uint64 count)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:880
dr_fopen
static int dr_fopen(FILE **ppFile, const char *pFilePath, const char *pOpenMode)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:584
dr_strcat_s
int dr_strcat_s(char *dst, size_t dstSizeInBytes, const char *src)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:163
dr_uint16
uint16_t dr_uint16
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:51
dr_file_iterator::absolutePath
char absolutePath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:382
dr_handle
void * dr_handle
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:83
dr_int32
int32_t dr_int32
Definition: porcupine/demo/c/dr_libs/old/dr.h:64
dr_strncpy_s
int dr_strncpy_s(char *dst, size_t dstSizeInBytes, const char *src, size_t count)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:129
dr_dlopen
dr_handle dr_dlopen(const char *filename)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:917
dr_vprintf_fixed_with_margin
int dr_vprintf_fixed_with_margin(int width, int margin, const char *const format, va_list args)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:743
dr_argv_is_set
dr_bool32 dr_argv_is_set(int argc, char **argv, const char *value)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:681
DR_LCG_A
#define DR_LCG_A
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:818
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
dr_strcpy_s
int dr_strcpy_s(char *dst, size_t dstSizeInBytes, const char *src)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:101
dr_int64
int64_t dr_int64
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:54
dr_open_and_read_file_with_extra_data
void * dr_open_and_read_file_with_extra_data(const char *pFilePath, size_t *pFileSizeOut, size_t extraBytes)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:621
dr_file_iterator::dir
DIR * dir
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:387
DR_FALSE
#define DR_FALSE
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:81
args
dr_strncat_s
int dr_strncat_s(char *dst, size_t dstSizeInBytes, const char *src, size_t count)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:205
dr_ptr
void * dr_ptr
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:84
dr_vprintf_fixed
int dr_vprintf_fixed(int width, const char *const format, va_list args)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:694
dr_extension_equal
dr_bool32 dr_extension_equal(const char *path, const char *extension)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:343
dr_rand_range_s32
int dr_rand_range_s32(int lo, int hi)
Definition: rhino/demo/c/dr_libs/tests/common/dr_common.c:860


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:49