libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmPrefs_h
  4. #define cmPrefs_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"Manage persistent application preferences." kw:[base] }
  9. typedef unsigned cmPrRC_t;
  10. typedef cmHandle_t cmPrH_t;
  11. enum
  12. {
  13. kOkPrRC = cmOkRC,
  14. kJsonFailPrRC,
  15. kLHeapFailPrRC,
  16. kCbNotFoundPrRC,
  17. kVarNotFoundPrRC,
  18. kBufTooSmallPrRC,
  19. kCvtErrPrRC,
  20. kInvalidIdPrRC,
  21. kInvalidIndexPrRC,
  22. kWriteFileFailPrRC,
  23. kNodeCreateFailPrRC,
  24. kDuplicateIdPrRC,
  25. kFileSysFailPrRC
  26. };
  27. enum
  28. {
  29. kMaxVarPrId = 0x7fffffff // User assigned variable id's given to cmPrefsCreateXXX()
  30. // must be less than kMaxVarPrId
  31. };
  32. typedef void (*cmPrefsOnChangeFunc_t)( cmPrH_t h, void* cbDataPtr, unsigned id );
  33. extern cmPrH_t cmPrNullHandle;
  34. // cmPrefsInit() creates the preference directory if it does not exist
  35. // according to cmFsPrefsDir(). It then forms the prefs file name as
  36. // 'cmFsPrefsDir()/fnName.fnExt' and call cmPrefsInitialize().
  37. // Set 'fnName' to NULL to use cmFsAppName() as the pref file name.
  38. // Set 'fnExt' to NULL to use '.js' as the pref file extension.
  39. // 'cbFunc' and 'cbDataPtr' are optional in both versions.
  40. // 'pathPrefixStr' (optional) provides a prefix to be added to
  41. // the path of each preference variable. This can be used to separate
  42. // preference data from other data in the preference file.
  43. cmPrRC_t cmPrefsInit( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fnName, const cmChar_t* fnExt, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr );
  44. cmPrRC_t cmPrefsInitialize( cmCtx_t* ctx, cmPrH_t* hp, const cmChar_t* fn, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr, const cmChar_t* pathPrefixStr);
  45. cmPrRC_t cmPrefsFinalize( cmPrH_t* hp );
  46. bool cmPrefsIsValid( cmPrH_t h );
  47. const cmChar_t* cmPrefsFileName( cmPrH_t h );
  48. // Return last RC.
  49. cmPrRC_t cmPrefsRC( cmPrH_t h);
  50. // Return last RC and set new RC.
  51. cmPrRC_t cmPrefsSetRC( cmPrH_t h, cmPrRC_t rc );
  52. cmPrRC_t cmPrefsInstallCallback( cmPrH_t h, cmPrefsOnChangeFunc_t cbFunc, void* cbDataPtr );
  53. cmPrRC_t cmPrefsRemoveCallback( cmPrH_t h, cmPrefsOnChangeFunc_t cbFunc );
  54. // Return cmInvalidId if the no variable is found with the requested path.
  55. unsigned cmPrefsId( cmPrH_t h, const cmChar_t* pathStr, bool reportNotFoundFl );
  56. // Returns -1 (and generates an error msg) if no pref. variable
  57. // is associated with id. Returns 1 if the variable is a scalar.
  58. unsigned cmPrefsEleCount( cmPrH_t h, unsigned id );
  59. // On input *'eleCntPtr' must contain the number of elements in the buffer
  60. // pointed to by 'vp' or NULL if '*vp' is a scalar.
  61. // On return *'eleCntPtr' contains the actual number of elements
  62. // returned by the function.
  63. // Set eleCntPtr to NULL for scalar a values.
  64. cmPrRC_t cmPrefsGetBool( cmPrH_t h, unsigned id, bool* vp, unsigned* eleCntPtr );
  65. cmPrRC_t cmPrefsGetUInt( cmPrH_t h, unsigned id, unsigned* vp, unsigned* eleCntPtr );
  66. cmPrRC_t cmPrefsGetInt( cmPrH_t h, unsigned id, int* vp, unsigned* eleCntPtr );
  67. cmPrRC_t cmPrefsGetReal( cmPrH_t h, unsigned id, double* vp, unsigned* eleCntPtr );
  68. cmPrRC_t cmPrefsGetString( cmPrH_t h, unsigned id, const cmChar_t** vp, unsigned* eleCntPtr );
  69. // Simplified scalar interface - check cmPrefsRC() for errors.
  70. bool cmPrefsBool( cmPrH_t h, unsigned id );
  71. unsigned cmPrefsUInt( cmPrH_t h, unsigned id );
  72. int cmPrefsInt( cmPrH_t h, unsigned id );
  73. float cmPrefsFloat( cmPrH_t h, unsigned id );
  74. double cmPrefsReal( cmPrH_t h, unsigned id );
  75. const cmChar_t* cmPrefsString( cmPrH_t h, unsigned id );
  76. // Simplified scalar interface w/ default values - check cmPrefsRC() for errors.
  77. // Returns the stored preference variable unless 'pathStr' does not identify a variable
  78. // or if 'h' is not valid. In either of these cases 'dfltVal' is returned.
  79. bool cmPrefsBoolDef( cmPrH_t h, const cmChar_t* pathStr, bool dfltVal );
  80. unsigned cmPrefsUIntDef( cmPrH_t h, const cmChar_t* pathStr, unsigned dfltVal );
  81. int cmPrefsIntDef( cmPrH_t h, const cmChar_t* pathStr, int dfltVal );
  82. float cmPrefsFloatDef( cmPrH_t h, const cmChar_t* pathStr, float dfltVal );
  83. double cmPrefsRealDef( cmPrH_t h, const cmChar_t* pathStr, double dfltVal );
  84. const cmChar_t* cmPrefsStringDef( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* dfltVal );
  85. // Get a scalar value.
  86. cmPrRC_t cmPrefsBoolRc( cmPrH_t h, const cmChar_t* pathStr, bool* retValPtr );
  87. cmPrRC_t cmPrefsUIntRc( cmPrH_t h, const cmChar_t* pathStr, unsigned* retValPtr );
  88. cmPrRC_t cmPrefsIntRc( cmPrH_t h, const cmChar_t* pathStr, int* retValPtr );
  89. cmPrRC_t cmPrefsFloatRc( cmPrH_t h, const cmChar_t* pathStr, float* retValPtr );
  90. cmPrRC_t cmPrefsRealRc( cmPrH_t h, const cmChar_t* pathStr, double* retValPtr );
  91. cmPrRC_t cmPrefsStringRc( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t** retValPtr );
  92. // Simplified array interface - check cmPrefsRC() for errors
  93. // Returns cmInvalidCnt if 'id' is invalid or 0 if the identified var. is a scalar.
  94. unsigned cmPrefsArrayElementCount( cmPrH_t h, unsigned id );
  95. bool cmPrefsBoolEle( cmPrH_t h, unsigned id, unsigned idx );
  96. unsigned cmPrefsUIntEle( cmPrH_t h, unsigned id, unsigned idx );
  97. int cmPrefsIntEle( cmPrH_t h, unsigned id, unsigned idx );
  98. float cmPrefsFloatEle( cmPrH_t h, unsigned id, unsigned idx );
  99. double cmPrefsRealEle( cmPrH_t h, unsigned id, unsigned idx );
  100. const cmChar_t* cmPrefsStringEle( cmPrH_t h, unsigned id, unsigned idx );
  101. // Set a preference value.
  102. // The size of array variables is allowed to shrink or grow but the type (bool/int/real/string)
  103. // must match.
  104. //
  105. // Note that the following limitations apply:
  106. // 1) This interface allows setting the value of an existing preference variable.
  107. // New variables may not be added.
  108. // 2) The type (bool/int/real/string) of the variable must match the value.
  109. // (e.g. 'int' type variables can only be set via cmPrefsSetInt()).
  110. // 3) For scalar (non-array) variables *eleCntPtr must be set to 1.
  111. //
  112. cmPrRC_t cmPrefsSetBoolArray( cmPrH_t h, unsigned id, const bool* vp, const unsigned* eleCntPtr );
  113. cmPrRC_t cmPrefsSetIntArray( cmPrH_t h, unsigned id, const int* vp, const unsigned* eleCntPtr );
  114. cmPrRC_t cmPrefsSetRealArray( cmPrH_t h, unsigned id, const double* vp, const unsigned* eleCntPtr );
  115. cmPrRC_t cmPrefsSetStringArray( cmPrH_t h, unsigned id, const cmChar_t** vp, const unsigned* eleCntPtr );
  116. cmPrRC_t cmPrefsSetBool( cmPrH_t h, unsigned id, bool val );
  117. cmPrRC_t cmPrefsSetUInt( cmPrH_t h, unsigned id, unsigned val );
  118. cmPrRC_t cmPrefsSetInt( cmPrH_t h, unsigned id, int val );
  119. cmPrRC_t cmPrefsSetFloat( cmPrH_t h, unsigned id, float val );
  120. cmPrRC_t cmPrefsSetReal( cmPrH_t h, unsigned id, double val );
  121. cmPrRC_t cmPrefsSetString( cmPrH_t h, unsigned id, const cmChar_t* val );
  122. cmPrRC_t cmPrefsPathSetBool( cmPrH_t h, const cmChar_t* pathStr, bool val );
  123. cmPrRC_t cmPrefsPathSetUInt( cmPrH_t h, const cmChar_t* pathStr, unsigned val );
  124. cmPrRC_t cmPrefsPathSetInt( cmPrH_t h, const cmChar_t* pathStr, int val );
  125. cmPrRC_t cmPrefsPathSetFloat( cmPrH_t h, const cmChar_t* pathStr, float val );
  126. cmPrRC_t cmPrefsPathSetReal( cmPrH_t h, const cmChar_t* pathStr, double val );
  127. cmPrRC_t cmPrefsPathSetString( cmPrH_t h, const cmChar_t* pathStr, const cmChar_t* val );
  128. // Create a new preference variable and set it's value to 'val'.
  129. // If a variable with the same path and type already exists and kForceValuePrFl
  130. // is set then update it's value to 'val'. Note that in this case if
  131. // kForceValuePrFl is not set then the function returns quietly.
  132. //
  133. // If a variable with the same path but a different type exists then an error is returned.
  134. //
  135. // The 'id' argument is optional. If 'id' is set to cmInvalidId then the
  136. // variable will be automatically assigned an id. The value of the
  137. // automatically assigned id can be found from the path string
  138. // via cmPrefsId(). If 'id' is not set to cmInvalidId then it must be less than
  139. // kMaxVarId.
  140. // Set kForceValuePrFl
  141. enum { kForceValuePrFl=0x01 };
  142. cmPrRC_t cmPrefsCreateBool( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, bool val );
  143. cmPrRC_t cmPrefsCreateUInt( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, unsigned val );
  144. cmPrRC_t cmPrefsCreateInt( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, int val );
  145. cmPrRC_t cmPrefsCreateFloat( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, float val );
  146. cmPrRC_t cmPrefsCreateReal( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, double val );
  147. cmPrRC_t cmPrefsCreateString( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const cmChar_t* val );
  148. cmPrRC_t cmPrefsCreateBoolArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const bool* val, unsigned eleCnt );
  149. cmPrRC_t cmPrefsCreateUIntArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const unsigned* val, unsigned eleCnt );
  150. cmPrRC_t cmPrefsCreateIntArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const int* val, unsigned eleCnt );
  151. cmPrRC_t cmPrefsCreateFloatArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const float* val, unsigned eleCnt );
  152. cmPrRC_t cmPrefsCreateRealArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const double* val, unsigned eleCnt );
  153. cmPrRC_t cmPrefsCreateStringArray( cmPrH_t h, unsigned id, const cmChar_t* pathStr, unsigned flags, const cmChar_t** val, unsigned eleCnt );
  154. bool cmPrefsIsDirty( cmPrH_t h );
  155. // If 'fn' is NULL then the filename passed in cmPrefsInitialize() is used.
  156. cmPrRC_t cmPrefsWrite( cmPrH_t h, const cmChar_t* fn );
  157. void cmPrefsReport( cmPrH_t h );
  158. void cmPrefsTest( cmCtx_t* ctx, const char* ifn, const char* ofn );
  159. //)
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif