ソースを参照

cmDspSys.h/c:Initial add of cmDspRsrcWriteString() and check of cmJsonIsModified() in cmDspSysUnload().

master
kevin 11年前
コミット
f1dc68364a
2個のファイルの変更133行の追加23行の削除
  1. 129
    21
      dsp/cmDspSys.c
  2. 4
    2
      dsp/cmDspSys.h

+ 129
- 21
dsp/cmDspSys.c ファイルの表示

@@ -480,13 +480,13 @@ cmDspRC_t cmDspSysUnload( cmDspSysH_t h )
480 480
   // finalize the pgm specific JSON tree
481 481
   if( cmJsonIsValid(p->ctx.rsrcJsH))
482 482
   {
483
-    //if( cmJsonWrite(p->ctx.rsrcJsH, cmJsonRoot(p->ctx.rsrcJsH), p->rsrcFn ) != kOkJsRC )
484
-    //  rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON file write failed on '%s'.",cmStringNullGuard(p->rsrcFn));
485
-    //else
486
-    //{
487
-      if( cmJsonFinalize(&p->ctx.rsrcJsH) != kOkJsRC )
488
-        rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource JSON finalization failed.");
489
-      //}
483
+    // if the JSON tree has been modified.
484
+    if( cmJsonIsModified(p->ctx.rsrcJsH) )
485
+      if( cmJsonWrite(p->ctx.rsrcJsH, cmJsonRoot(p->ctx.rsrcJsH), p->rsrcFn ) != kOkJsRC )
486
+        rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON resource file write failed on '%s'.",cmStringNullGuard(p->rsrcFn));
487
+    
488
+    if( cmJsonFinalize(&p->ctx.rsrcJsH) != kOkJsRC )
489
+      rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource JSON finalization failed.");
490 490
   }
491 491
   
492 492
   // release the JSON file name
@@ -1850,6 +1850,51 @@ cmDspRC_t _cmDspRsrcPath( cmDspSysH_t h, cmChar_t** pathPtr, va_list vl )
1850 1850
   return rc;
1851 1851
 }
1852 1852
 
1853
+cmDspRC_t _cmDspRsrcWritePath( cmDspSysH_t h, cmChar_t** pathStrRef, cmChar_t** varStrRef, va_list vl )
1854
+{
1855
+  unsigned  i;
1856
+  cmDspRC_t rc        = kOkDspRC;;
1857
+  cmDsp_t*  p         = _cmDspHandleToPtr(h);
1858
+  cmChar_t* path      = NULL;
1859
+  unsigned  maxArgCnt = 10;
1860
+  cmChar_t* prevStr   = NULL;
1861
+
1862
+  *pathStrRef = NULL;
1863
+  *varStrRef  = NULL;
1864
+ 
1865
+  for(i=0; i<maxArgCnt; ++i )
1866
+  {
1867
+    cmChar_t* str;
1868
+
1869
+    if((str = va_arg(vl,cmChar_t* )) == NULL )
1870
+      break;
1871
+    
1872
+    if( prevStr != NULL )
1873
+      path = cmTextAppendSS(path,"/");
1874
+    path = cmTextAppendSS(path,prevStr);    
1875
+
1876
+    prevStr = str;    
1877
+  }
1878
+
1879
+  if( i >= maxArgCnt )
1880
+  {
1881
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
1882
+    goto errLabel;
1883
+  }
1884
+
1885
+  // duplicate the string onto the program linked heap
1886
+  if( path != NULL )
1887
+    *pathStrRef = cmLhAllocStr( cmDspSysLHeap(h), path);
1888
+
1889
+  *varStrRef  = prevStr;
1890
+
1891
+ errLabel:
1892
+  cmMemFree(path);
1893
+
1894
+  return rc;
1895
+
1896
+}
1897
+
1853 1898
 cmDspRC_t cmDspRsrcBoolV(    cmDspSysH_t h, bool* vp, va_list vl )
1854 1899
 {
1855 1900
   assert(vp != NULL);
@@ -1861,7 +1906,9 @@ cmDspRC_t cmDspRsrcBoolV(    cmDspSysH_t h, bool* vp, va_list vl )
1861 1906
     return rc;
1862 1907
 
1863 1908
   if((rc = cmJsonPathToBool( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
1864
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'bool' resource not found at path:'%s'.",cmStringNullGuard(path));
1909
+    rc =  cmErrMsg(&p->err,kJsonFailDspRC,"'bool' resource not found at path:'%s'.",cmStringNullGuard(path));
1910
+
1911
+  cmLhFree(cmDspSysLHeap(h),path);
1865 1912
 
1866 1913
   return rc;
1867 1914
 }
@@ -1877,7 +1924,9 @@ cmDspRC_t cmDspRsrcIntV(    cmDspSysH_t h, int* vp, va_list vl )
1877 1924
     return rc;
1878 1925
 
1879 1926
   if((rc = cmJsonPathToInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
1880
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'int' resource not found at path:'%s'.",cmStringNullGuard(path));
1927
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'int' resource not found at path:'%s'.",cmStringNullGuard(path));
1928
+
1929
+  cmLhFree(cmDspSysLHeap(h),path);
1881 1930
 
1882 1931
   return rc;  
1883 1932
 }
@@ -1893,7 +1942,9 @@ cmDspRC_t cmDspRsrcUIntV(   cmDspSysH_t h, unsigned* vp, va_list vl )
1893 1942
     return rc;
1894 1943
 
1895 1944
   if((rc = cmJsonPathToUInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
1896
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'uint' resource not found at path:'%s'.",cmStringNullGuard(path));
1945
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'uint' resource not found at path:'%s'.",cmStringNullGuard(path));
1946
+
1947
+  cmLhFree(cmDspSysLHeap(h),path);
1897 1948
 
1898 1949
   return rc;  
1899 1950
 }
@@ -1909,7 +1960,9 @@ cmDspRC_t cmDspRsrcDblV(   cmDspSysH_t h, double* vp, va_list vl )
1909 1960
     return rc;
1910 1961
 
1911 1962
   if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
1912
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
1963
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
1964
+
1965
+  cmLhFree(cmDspSysLHeap(h),path);
1913 1966
 
1914 1967
   return rc;  
1915 1968
 }
@@ -1926,9 +1979,11 @@ cmDspRC_t cmDspRsrcRealV(   cmDspSysH_t h, cmReal_t* vp, va_list vl )
1926 1979
     return rc;
1927 1980
 
1928 1981
   if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &dval )) != kOkJsRC )
1929
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
1982
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
1983
+  else
1984
+    *vp = dval;
1930 1985
 
1931
-  *vp = dval;
1986
+  cmLhFree(cmDspSysLHeap(h),path);
1932 1987
 
1933 1988
   return rc;  
1934 1989
 }
@@ -1944,7 +1999,9 @@ cmDspRC_t cmDspRsrcStringV( cmDspSysH_t h, const cmChar_t** vp, va_list vl )
1944 1999
     return rc;
1945 2000
 
1946 2001
   if((rc = cmJsonPathToString( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
1947
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'string' resource not found at path:'%s'.",cmStringNullGuard(path));
2002
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'string' resource not found at path:'%s'.",cmStringNullGuard(path));
2003
+
2004
+  cmLhFree(cmDspSysLHeap(h),path);
1948 2005
 
1949 2006
   return rc;  
1950 2007
 }
@@ -1961,9 +2018,12 @@ cmDspRC_t cmDspRsrcArrayCountV( cmDspSysH_t h, unsigned *n, va_list vl )
1961 2018
     return rc;
1962 2019
 
1963 2020
   if((rc = cmJsonPathToArray( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &np )) != kOkJsRC )
1964
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
2021
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
2022
+  else
2023
+    *n = cmJsonChildCount(np);
2024
+
2025
+  cmLhFree(cmDspSysLHeap(h),path);
1965 2026
 
1966
-  *n = cmJsonChildCount(np);
1967 2027
   return rc;  
1968 2028
 
1969 2029
 }
@@ -1979,12 +2039,15 @@ cmDspRC_t _cmDspRsrcArrayV( cmDspSysH_t h, unsigned* np, cmJsonNode_t** npp, cmC
1979 2039
     return rc;
1980 2040
 
1981 2041
   if((rc = cmJsonPathToArray( jsH, NULL, NULL, path, npp )) != kOkJsRC )
1982
-    return cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
2042
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
2043
+  else
2044
+  {
2045
+    if((*np = cmJsonChildCount(*npp)) != 0 )
2046
+      *pathPtrPtr = path;
1983 2047
 
1984
-  if((*np = cmJsonChildCount(*npp)) == 0 )
1985
-    return rc;
2048
+  }
1986 2049
 
1987
-  *pathPtrPtr = path;
2050
+  cmLhFree(cmDspSysLHeap(h),path);
1988 2051
 
1989 2052
   return rc;
1990 2053
 }
@@ -2012,6 +2075,8 @@ cmDspRC_t cmDspRsrcBoolArrayV(   cmDspSysH_t h, unsigned* np, bool** vpp, va_lis
2012 2075
   *vpp = vp;
2013 2076
   *np  = n;
2014 2077
 
2078
+  cmLhFree(cmDspSysLHeap(h),path);
2079
+
2015 2080
   return rc;
2016 2081
 }
2017 2082
 
@@ -2038,6 +2103,8 @@ cmDspRC_t cmDspRsrcIntArrayV(   cmDspSysH_t h, unsigned* np, int** vpp, va_list
2038 2103
   *vpp = vp;
2039 2104
   *np  = n;
2040 2105
 
2106
+  cmLhFree(cmDspSysLHeap(h),path);
2107
+
2041 2108
   return rc;
2042 2109
 }
2043 2110
 
@@ -2064,6 +2131,8 @@ cmDspRC_t cmDspRsrcUIntArrayV(  cmDspSysH_t h, unsigned* np, unsigned** vpp, va_
2064 2131
   *vpp = vp;
2065 2132
   *np  = n;
2066 2133
 
2134
+  cmLhFree(cmDspSysLHeap(h),path);
2135
+
2067 2136
   return rc;
2068 2137
 }
2069 2138
 
@@ -2090,8 +2159,9 @@ cmDspRC_t cmDspRsrcDblArrayV(  cmDspSysH_t h, unsigned* np, double** vpp, va_lis
2090 2159
   *vpp = vp;
2091 2160
   *np  = n;
2092 2161
 
2093
-  return rc;
2162
+  cmLhFree(cmDspSysLHeap(h),path);
2094 2163
 
2164
+  return rc;
2095 2165
 }
2096 2166
 
2097 2167
 cmDspRC_t cmDspRsrcRealArrayV(  cmDspSysH_t h, unsigned* np, cmReal_t** vpp, va_list vl )
@@ -2122,6 +2192,8 @@ cmDspRC_t cmDspRsrcRealArrayV(  cmDspSysH_t h, unsigned* np, cmReal_t** vpp, va_
2122 2192
   *vpp = vp;
2123 2193
   *np  = n;
2124 2194
 
2195
+  cmLhFree(cmDspSysLHeap(h),path);
2196
+
2125 2197
   return rc;
2126 2198
 }
2127 2199
 
@@ -2148,6 +2220,8 @@ cmDspRC_t cmDspRsrcStringArrayV(cmDspSysH_t h, unsigned* np, const cmChar_t*** v
2148 2220
   *vpp = vp;
2149 2221
   *np  = n;
2150 2222
 
2223
+  cmLhFree(cmDspSysLHeap(h),path);
2224
+
2151 2225
   return rc;
2152 2226
 }
2153 2227
 
@@ -2258,3 +2332,37 @@ cmDspRC_t cmDspRsrcStringArray( cmDspSysH_t h, unsigned* np, const cmChar_t*** v
2258 2332
   va_end(vl);
2259 2333
   return rc;
2260 2334
 }
2335
+
2336
+cmDspRC_t cmDspRsrcWriteStringV( cmDspSysH_t h, const cmChar_t*  v, va_list vl )
2337
+{
2338
+  cmDsp_t*      p                = _cmDspHandleToPtr(h);
2339
+  cmChar_t*     pathStr          = NULL;
2340
+  cmChar_t*     varStr           = NULL;
2341
+  cmJsonH_t     jsH              = cmDspSysPgmRsrcHandle(h);
2342
+  cmJsonNode_t* parentObjNodePtr = NULL;
2343
+  cmDspRC_t     rc;
2344
+
2345
+  if((rc = _cmDspRsrcWritePath(h,&pathStr,&varStr,vl)) != kOkDspRC )
2346
+    goto errLabel;
2347
+
2348
+  if((parentObjNodePtr = cmJsonFindPathValue( jsH, pathStr, cmJsonRoot(jsH), kPairTId )) == NULL )
2349
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"The parent object '%s' for variable '%s' could not be found.",cmStringNullGuard(pathStr),cmStringNullGuard(varStr)); 
2350
+
2351
+  if( cmJsonInsertOrReplacePairString( jsH, parentObjNodePtr, varStr, kStringTId, v ) != kOkJsRC )
2352
+    rc = cmErrMsg(&p->err,kJsonFailDspRC,"Write 'string' resource value failed for path '%s' and variable '%s'",cmStringNullGuard(pathStr),cmStringNullGuard(varStr)); 
2353
+
2354
+ errLabel:
2355
+  if( pathStr != NULL )
2356
+    cmLhFree(cmDspSysLHeap(h),pathStr);
2357
+
2358
+  return rc;    
2359
+}
2360
+
2361
+cmDspRC_t cmDspRsrcWriteString( cmDspSysH_t h, const cmChar_t* v, ... )
2362
+{
2363
+  va_list vl;
2364
+  va_start(vl,v);
2365
+  cmDspRC_t rc = cmDspRsrcWriteStringV(h,v,vl);
2366
+  va_end(vl);
2367
+  return rc;
2368
+}

+ 4
- 2
dsp/cmDspSys.h ファイルの表示

@@ -57,7 +57,7 @@ extern "C" {
57 57
   const cmChar_t* cmDspSysPresetGroupLabel(    cmDspSysH_t h, unsigned groupIdx );
58 58
   cmDspRC_t       cmDspSysPresetGroupJsonList( cmDspSysH_t h, cmJsonH_t* jsHPtr );
59 59
   
60
-  // A 'preset' is a collection of stored DSP instances and there variables.  A preset belongs
60
+  // A 'preset' is a collection of stored DSP instances and their variables.  A preset belongs
61 61
   // to a group.  A given group may have multiple presets. Each preset represents a saved
62 62
   // instance/var state.
63 63
   unsigned        cmDspSysPresetPresetCount(   cmDspSysH_t h, unsigned groupIdx );
@@ -266,7 +266,9 @@ extern "C" {
266 266
   cmDspRC_t cmDspRsrcRealArray(   cmDspSysH_t h, unsigned* np, cmReal_t**  vpp, ... );
267 267
   cmDspRC_t cmDspRsrcStringArray( cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, ... );
268 268
  
269
-  
269
+
270
+  cmDspRC_t cmDspRsrcWriteStringV( cmDspSysH_t h, const cmChar_t* v, va_list vl );
271
+  cmDspRC_t cmDspRsrcWriteString(  cmDspSysH_t h, const cmChar_t* v, ... );
270 272
 
271 273
 
272 274
 #ifdef __cplusplus

読み込み中…
キャンセル
保存