Pulsar  3d3a057
types.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Base types
4  */
5 #pragma once
6 
7 #include <switch.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <malloc.h>
12 #include <unistd.h>
13 
14 /// Shortcut to get a result type enum from its name
15 #define PLSR_RC_MAKE_RESULT_TYPE(RESULT_TYPE) (PLSR_ResultType_ ## RESULT_TYPE)
16 /// Shortcut to get a category enum from its name
17 #define PLSR_RC_MAKE_CATEGORY_TYPE(ARCHIVE_TYPE, CATEGORY_TYPE) (PLSR_ ## ARCHIVE_TYPE ## CategoryType_ ## CATEGORY_TYPE)
18 /// Shortcut to get an archive enum from its name
19 #define PLSR_RC_MAKE_ARCHIVE_TYPE(ARCHIVE_TYPE) (PLSR_ArchiveType_ ## ARCHIVE_TYPE)
20 
21 /// Make a result code from raw integers
22 #define PLSR_RC_MAKE_RAW(ARCHIVE, CATEGORY, RESULT) (RESULT + (CATEGORY << 8) + (ARCHIVE << 16))
23 
24 /// Make a result code from archive, category and result type names
25 #define PLSR_RC_MAKE(ARCHIVE_TYPE, CATEGORY_TYPE, RESULT_TYPE) PLSR_RC_MAKE_RAW( \
26  PLSR_RC_MAKE_ARCHIVE_TYPE(ARCHIVE_TYPE), \
27  PLSR_RC_MAKE_CATEGORY_TYPE(ARCHIVE_TYPE, CATEGORY_TYPE), \
28  PLSR_RC_MAKE_RESULT_TYPE(RESULT_TYPE) \
29 )
30 
31 /// Result code returned on success
32 #define PLSR_RC_OK 0
33 /// Test that the result code means failure
34 #define PLSR_RC_FAILED(RESULT) (RESULT != PLSR_RC_OK)
35 /// Test that the result code means success
36 #define PLSR_RC_SUCCEEDED(RESULT) (RESULT == PLSR_RC_OK)
37 
38 /// Extract the archive part of a result code
39 #define PLSR_RC_ARCHIVE(RESULT) ((RESULT >> 16) & 0xFF)
40 /// Extract the category part of a result code
41 #define PLSR_RC_CATEGORY(RESULT) ((RESULT >> 8) & 0xFF)
42 /// Extract the result type part of a result code
43 #define PLSR_RC_RESULT(RESULT) (RESULT & 0xFF)
44 
45 /// Convert a result code to another archive and category, keeping the result type
46 /** @note This is mainly meant to convert errors from generic archive read calls into the callee archive and category type */
47 #define PLSR_RC_CONVERT(RESULT, NEW_ARCHIVE_TYPE, NEW_CATEGORY_TYPE) PLSR_RC_MAKE_RAW( \
48  PLSR_RC_MAKE_ARCHIVE_TYPE(NEW_ARCHIVE_TYPE), \
49  PLSR_RC_MAKE_CATEGORY_TYPE(NEW_ARCHIVE_TYPE, NEW_CATEGORY_TYPE), \
50  PLSR_RC_RESULT(RESULT) \
51 )
52 
53 /// Return result code if failed
54 #define PLSR_RC_TRY(X) do { \
55  const PLSR_RC _rc = (X); \
56  if(PLSR_RC_FAILED(_rc)) { return _rc; } \
57 } while(false)
58 
59 /// Return result code if failed, also converts result to a new archive and category
60 /** @note This is mainly meant to convert errors from generic archive read calls into the callee archive and category type */
61 #define PLSR_RC_LTRY(ARCHIVE_TYPE, CATEGORY_TYPE, X) do { \
62  const PLSR_RC _rc = (X); \
63  if(PLSR_RC_FAILED(_rc)) { return PLSR_RC_CONVERT(_rc, ARCHIVE_TYPE, CATEGORY_TYPE); } \
64 } while(false)
65 
66 /// Return a System type result code if the libnx result failed
67 /** @note This is mainly used in the player when calling the audio renderer */
68 #define PLSR_RC_NX_LTRY(ARCHIVE_TYPE, CATEGORY_TYPE, NX_RESULT) do { \
69  if(R_FAILED(NX_RESULT)) { return PLSR_RC_MAKE(ARCHIVE_TYPE, CATEGORY_TYPE, System); } \
70 } while(false)
71 
72 /// Result code returned by Pulsar functions
73 typedef u32 PLSR_RC;
74 
75 /// Result code types
76 typedef enum {
77  PLSR_ResultType_OK = 0,
78 
79  PLSR_ResultType_FileRead, ///< File could not be opened or read
80  PLSR_ResultType_BadMagic, ///< Magic mismatch
81  PLSR_ResultType_BadEndianness, ///< Archive endianness did not match host endianness
82  PLSR_ResultType_BadInput, ///< Function was called with improper arguments
83  PLSR_ResultType_NotFound, ///< Requested data could not be retrieved
84  PLSR_ResultType_NotReady, ///< Function was called before required initialization
85  PLSR_ResultType_Unsupported, ///< Execution encountered an unexpected case
86  PLSR_ResultType_Memory, ///< Memory allocation failed
87 
88  PLSR_ResultType_System = 0xFF ///< Can indicate an underlying libnx fn call failure
90 
91 /// Supported archive types
92 typedef enum {
93  PLSR_ArchiveType_Unknown = 0,
94 
95  PLSR_ArchiveType_BFSAR, ///< Sound archive
96  PLSR_ArchiveType_BFGRP, ///< Sound group
97  PLSR_ArchiveType_BFWSD, ///< Wave sound data
98  PLSR_ArchiveType_BFWAR, ///< Wave archive
99  PLSR_ArchiveType_BFWAV, ///< Wave file
100  PLSR_ArchiveType_BFSTM, ///< Stream file
101 
102  PLSR_ArchiveType_Player = 0xFF, ///< Not an archive, type used by Player functions
Function was called before required initialization.
Definition: types.h:84
PLSR_ArchiveType
Supported archive types.
Definition: types.h:92
Wave file.
Definition: types.h:99
Sound group.
Definition: types.h:96
Function was called with improper arguments.
Definition: types.h:82
Magic mismatch.
Definition: types.h:80
Archive endianness did not match host endianness.
Definition: types.h:81
Wave archive.
Definition: types.h:98
Memory allocation failed.
Definition: types.h:86
PLSR_ResultType
Result code types.
Definition: types.h:76
File could not be opened or read.
Definition: types.h:79
Not an archive, type used by Player functions.
Definition: types.h:102
Requested data could not be retrieved.
Definition: types.h:83
Stream file.
Definition: types.h:100
Can indicate an underlying libnx fn call failure.
Definition: types.h:88
Execution encountered an unexpected case.
Definition: types.h:85
u32 PLSR_RC
Result code returned by Pulsar functions.
Definition: types.h:73
Wave sound data.
Definition: types.h:97
Sound archive.
Definition: types.h:95