Browse Source

Makefile.am Added cmTextTemplate.c/h cmText.c/h Added CmTextReplaceFirst()

master
kpl 12 years ago
parent
commit
dd8ef2dbe6
6 changed files with 61 additions and 39 deletions
  1. 5
    2
      Makefile.am
  2. 19
    19
      cmCtx.h
  3. 11
    14
      cmLex.c
  4. 9
    1
      cmMem.c
  5. 14
    3
      cmText.c
  6. 3
    0
      cmText.h

+ 5
- 2
Makefile.am View File

9
 cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h src/libcm/cmMem.h src/libcm/cmTime.h src/libcm/cmPgmOpts.h
9
 cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h src/libcm/cmMem.h src/libcm/cmTime.h src/libcm/cmPgmOpts.h
10
 cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c src/libcm/cmMem.c src/libcm/cmTime.c src/libcm/cmPgmOpts.c
10
 cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c src/libcm/cmMem.c src/libcm/cmTime.c src/libcm/cmPgmOpts.c
11
 
11
 
12
-cmHDR += src/libcm/cmLib.h src/libcm/cmText.h src/libcm/cmMath.h src/libcm/cmOp.h src/libcm/cmGnuPlot.h src/libcm/cmKeyboard.h
13
-cmSRC += src/libcm/cmLib.c src/libcm/cmText.c src/libcm/cmMath.c src/libcm/cmOp.c src/libcm/cmGnuPlot.c src/libcm/cmKeyboard.c
12
+cmHDR += src/libcm/cmLib.h src/libcm/cmText.h src/libcm/cmTextTemplate.h
13
+cmSRC += src/libcm/cmLib.c src/libcm/cmText.c src/libcm/cmTextTemplate.c
14
+
15
+cmHDR += src/libcm/cmMath.h src/libcm/cmOp.h src/libcm/cmGnuPlot.h src/libcm/cmKeyboard.h
16
+cmSRC += src/libcm/cmMath.c src/libcm/cmOp.c src/libcm/cmGnuPlot.c src/libcm/cmKeyboard.c
14
 
17
 
15
 cmHDR += src/libcm/cmLinkedHeap.h src/libcm/cmMallocDebug.h src/libcm/cmLex.h src/libcm/cmJson.h src/libcm/cmPrefs.h src/libcm/cmStack.h
18
 cmHDR += src/libcm/cmLinkedHeap.h src/libcm/cmMallocDebug.h src/libcm/cmLex.h src/libcm/cmJson.h src/libcm/cmPrefs.h src/libcm/cmStack.h
16
 cmSRC += src/libcm/cmLinkedHeap.c src/libcm/cmMallocDebug.c src/libcm/cmLex.c src/libcm/cmJson.c src/libcm/cmPrefs.c src/libcm/cmStack.c
19
 cmSRC += src/libcm/cmLinkedHeap.c src/libcm/cmMallocDebug.c src/libcm/cmLex.c src/libcm/cmJson.c src/libcm/cmPrefs.c src/libcm/cmStack.c

+ 19
- 19
cmCtx.h View File

3
 // cmCtx_t is used to hold application supplied cmRpt_t, cmErr_t and
3
 // cmCtx_t is used to hold application supplied cmRpt_t, cmErr_t and
4
 // other global values for easy distribution throughtout a cm based application.
4
 // other global values for easy distribution throughtout a cm based application.
5
 //
5
 //
6
-//  Most the cm components need at least an application supplied cmRpt_t function
6
+//  Most the libcm components need at least an application supplied cmRpt_t function
7
 //  to initialize their own internal cmErr_t error class.  Likewise classes which
7
 //  to initialize their own internal cmErr_t error class.  Likewise classes which
8
 //  use a cmLHeapH_t based internal heap manager require application wide memory 
8
 //  use a cmLHeapH_t based internal heap manager require application wide memory 
9
 // manager configuration information.  The cmCtx_t packages this information and
9
 // manager configuration information.  The cmCtx_t packages this information and
10
 // allows it to be easily distributed.  The applicaton and its constituent objects
10
 // allows it to be easily distributed.  The applicaton and its constituent objects
11
 // then need only maintain and pass pointers to a single cmCtx_t object to have access to 
11
 // then need only maintain and pass pointers to a single cmCtx_t object to have access to 
12
-// this all the global program information.
12
+// all the global program information.
13
 //)
13
 //)
14
 
14
 
15
 #ifndef cmCtx_h
15
 #ifndef cmCtx_h
22
   //(
22
   //(
23
 
23
 
24
   // cmCtx_t data type.
24
   // cmCtx_t data type.
25
-typedef struct
26
-{
27
-  cmRpt_t  rpt;           //< Application supplied global reporter. This reporter is also use by \ref err.
28
-  cmErr_t  err;           //< Application error reporter which can be used to report errors prior to the client object being initialized to the point where it can use it's own cmErr_t.
29
-  unsigned guardByteCnt;  //< Guard byte count in use by \ref cmMallocDebug.h .
30
-  unsigned alignByteCnt;  //< Align byte count used by the \ref cmMallocDebug.h
31
-  unsigned mmFlags;       //< Initialization flags used by \ref cmMallocDebug.h.
32
-  void*    userDefPtr;    //< Application defined pointer.
33
-} cmCtx_t;
25
+  typedef struct
26
+  {
27
+    cmRpt_t  rpt;           // Application supplied global reporter. This reporter is also use by \ref err.
28
+    cmErr_t  err;           // Application error reporter which can be used to report errors prior to the client object being initialized to the point where it can use it's own cmErr_t.
29
+    unsigned guardByteCnt;  // Guard byte count in use by \ref cmMallocDebug.h .
30
+    unsigned alignByteCnt;  // Align byte count used by the \ref cmMallocDebug.h
31
+    unsigned mmFlags;       // Initialization flags used by \ref cmMallocDebug.h.
32
+    void*    userDefPtr;    // Application defined pointer.
33
+  } cmCtx_t;
34
 
34
 
35
   // cmCtx_t initialization function.
35
   // cmCtx_t initialization function.
36
   void cmCtxSetup( 
36
   void cmCtxSetup( 
37
-    cmCtx_t*         ctx,         //< The cmCtx_t to initialize.
38
-    const cmChar_t*  title,       //<  The cmCtx_t error label. See cmErrSetup().
39
-    cmRptPrintFunc_t prtFunc,     //<  The printFunc() to assign to the cmCtx_t.rpt.
40
-    cmRptPrintFunc_t errFunc,     //<  The errFunc() to assign to cmCtx_t.rpt.
41
-    void*            cbPtr,       //<  Callback data to use with prtFunc() and errFunc().
42
-    unsigned         guardByteCnt,//<  Guard byte count used to configure \ref cmMallocDebug.h
43
-    unsigned         alignByteCnt,//<  Align byte count used to configure \ref cmMallocDebug.h
44
-    unsigned         mmFlags      //<  Initialization flags used to configure \ref cmMallocDebug.h
37
+    cmCtx_t*         ctx,         // The cmCtx_t to initialize.
38
+    const cmChar_t*  title,       //  The cmCtx_t error label. See cmErrSetup().
39
+    cmRptPrintFunc_t prtFunc,     //  The printFunc() to assign to the cmCtx_t.rpt.
40
+    cmRptPrintFunc_t errFunc,     //  The errFunc() to assign to cmCtx_t.rpt.
41
+    void*            cbPtr,       //  Callback data to use with prtFunc() and errFunc().
42
+    unsigned         guardByteCnt,//  Guard byte count used to configure \ref cmMallocDebug.h
43
+    unsigned         alignByteCnt,//  Align byte count used to configure \ref cmMallocDebug.h
44
+    unsigned         mmFlags      //  Initialization flags used to configure \ref cmMallocDebug.h
45
                    );
45
                    );
46
   //)
46
   //)
47
   //}
47
   //}

+ 11
- 14
cmLex.c View File

431
 
431
 
432
 cmRC_t cmLexFinal( cmLexH* hp )
432
 cmRC_t cmLexFinal( cmLexH* hp )
433
 {
433
 {
434
-  if( hp == NULL )
434
+  if( hp == NULL || cmLexIsValid(*hp)==false )
435
     return cmOkRC;
435
     return cmOkRC;
436
 
436
 
437
   cmLex* p = _cmLexHandleToPtr(*hp);
437
   cmLex* p = _cmLexHandleToPtr(*hp);
835
 // cmLexTest() gives a simple cmLex example.
835
 // cmLexTest() gives a simple cmLex example.
836
 //)
836
 //)
837
 
837
 
838
-//[
838
+//(
839
 void cmLexTest( cmRpt_t* rpt)
839
 void cmLexTest( cmRpt_t* rpt)
840
 {
840
 {
841
   cmChar_t buf[] =
841
   cmChar_t buf[] =
842
-"123ident0\n\
843
- 123.456\n\
844
-ident0\n\
845
-0xa12+.2\n\
846
-// comment \n\
847
-/* block \n\
848
-comment */\
849
-\"quoted string\"\
850
-ident1\
851
-// last line comment\
852
-";
842
+"123ident0\n 123.456\nident0\n"
843
+"0xa12+.2\n"
844
+"// comment \n"
845
+"/* block \n"
846
+"comment */"
847
+"\"quoted string\""
848
+"ident1"
849
+"// last line comment";
853
 
850
 
854
   // initialize a lexer with a buffer of text
851
   // initialize a lexer with a buffer of text
855
   cmLexH h = cmLexInit(buf,strlen(buf),
852
   cmLexH h = cmLexInit(buf,strlen(buf),
906
 
903
 
907
 }
904
 }
908
 
905
 
909
-//]
906
+//)
910
 //}
907
 //}

+ 9
- 1
cmMem.c View File

166
     // if no tracking recd was found
166
     // if no tracking recd was found
167
     if( rp == NULL )
167
     if( rp == NULL )
168
       return cmErrMsg(&p->err,kMissingRecdMmRC,"Unable to locate a tracking record associated with released data area pointer:%p.",dataPtr);
168
       return cmErrMsg(&p->err,kMissingRecdMmRC,"Unable to locate a tracking record associated with released data area pointer:%p.",dataPtr);
169
+    /*
170
+    if( rp->uniqueId == 176690 )
171
+    {
172
+      cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory free id:%i.",rp->uniqueId);
173
+    }
174
+    */
169
 
175
 
170
     // if this block was already freed then this is a double free
176
     // if this block was already freed then this is a double free
171
     if( cmIsFlag(rp->flags,kFreedMmFl) )
177
     if( cmIsFlag(rp->flags,kFreedMmFl) )
437
    cmMm_t*  p          = _cmMmHandleToPtr(h); 
443
    cmMm_t*  p          = _cmMmHandleToPtr(h); 
438
    unsigned newByteCnt = newEleCnt * newEleByteCnt;
444
    unsigned newByteCnt = newEleCnt * newEleByteCnt;
439
    void*    ndp        = _cmMmAllocate(p,orgDataPtr,newByteCnt,flags);
445
    void*    ndp        = _cmMmAllocate(p,orgDataPtr,newByteCnt,flags);
446
+   
440
    /*
447
    /*
441
-   if( p->nextId == 1575 )
448
+   if( p->nextId == 189114 )
442
    {
449
    {
443
      cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
450
      cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
444
    }
451
    }
445
    */
452
    */
453
+
446
    // if we are tracking changes
454
    // if we are tracking changes
447
    if( cmIsFlag(p->flags,kTrackMmFl) )
455
    if( cmIsFlag(p->flags,kTrackMmFl) )
448
    {
456
    {

+ 14
- 3
cmText.c View File

522
 cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u )
522
 cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u )
523
 { return cmTextReplaceSN(s,t,tn,u,u==NULL ? 0 : strlen(u)); }
523
 { return cmTextReplaceSN(s,t,tn,u,u==NULL ? 0 : strlen(u)); }
524
 
524
 
525
-cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
525
+cmChar_t* _cmTextReplace( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned n )
526
 {
526
 {
527
-  // we will go into an endless loop if 't' is contained in 'u'.
528
-  assert( s!= NULL && t!=NULL && u!=NULL && strstr(u,t) == NULL );
527
+  // we will go into an endless loop if 't' is contained in 'u' and n > 1.
528
+  assert( s!= NULL && t!=NULL && u!=NULL && (n==1 || strstr(u,t) == NULL) );
529
 
529
 
530
   int       tn = strlen(t);
530
   int       tn = strlen(t);
531
   cmChar_t* c  = NULL;
531
   cmChar_t* c  = NULL;
532
+  unsigned  i  = 0;
532
 
533
 
533
   while( (c = strstr(s,t)) != NULL ) 
534
   while( (c = strstr(s,t)) != NULL ) 
534
   {
535
   {
536
 
537
 
537
     assert(s!=NULL);
538
     assert(s!=NULL);
538
 
539
 
540
+    ++i;
541
+    if( n!=cmInvalidCnt && i>=n)
542
+      break;
543
+
539
   };
544
   };
540
 
545
 
541
   return s;
546
   return s;
542
 }
547
 }
543
 
548
 
549
+cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
550
+{ return _cmTextReplace(s,t,u,cmInvalidCnt); }
551
+
552
+cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
553
+{ return _cmTextReplace(s,t,u,1); }
554
+
544
 cmChar_t* cmTextInsertSN(  cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un )
555
 cmChar_t* cmTextInsertSN(  cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un )
545
 {
556
 {
546
   unsigned n = strlen(s)+1;
557
   unsigned n = strlen(s)+1;

+ 3
- 0
cmText.h View File

169
   
169
   
170
   // Replace all occurrences of t[] with u[] in s[].
170
   // Replace all occurrences of t[] with u[] in s[].
171
   cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
171
   cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
172
+  
173
+  // Replace the first occurence of t[] in s[] with u[].
174
+  cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
172
 
175
 
173
   // Insert u[un] at before t in s[].
176
   // Insert u[un] at before t in s[].
174
   cmChar_t* cmTextInsertSN(  cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un );
177
   cmChar_t* cmTextInsertSN(  cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un );

Loading…
Cancel
Save