Fossil

Check-in [9b3a11e1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added push/pop as aliases for start/end in the ob API. Fixed a horrible size calculation bug which triggered an assert() for ob nesting levels deeper than 2.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1: 9b3a11e16095e2ce9ab1db03d0ada3a02f5046a1
User & Date: stephan 2012-07-15 09:23:59
Context
2012-07-15
10:59
Added ability to tie client data/finalizer to th1, allowing a refactoring of OB manager to use per-interpreter-instance state instead of global state. check-in: 147c602d user: stephan tags: th1-query-api
09:23
Added push/pop as aliases for start/end in the ob API. Fixed a horrible size calculation bug which triggered an assert() for ob nesting levels deeper than 2. check-in: 9b3a11e1 user: stephan tags: th1-query-api
09:20
Added incr as built-in function. check-in: dc3c8ac3 user: stephan tags: th1-query-api
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/th.c.

2796
2797
2798
2799
2800
2801
2802
2803
2804

2805



2806
2807
2808
2809
2810
2811
2812
....
2875
2876
2877
2878
2879
2880
2881
2882

2883

2884
2885
2886
2887
2888
2889
2890
....
2899
2900
2901
2902
2903
2904
2905
2906

2907

2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920


2921
2922
2923
2924
2925
2926

2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937













2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
....
2967
2968
2969
2970
2971
2972
2973
2974


2975
2976
2977
2978
2979
2980
2981
....
3039
3040
3041
3042
3043
3044
3045


3046
3047
3048
3049
3050
3051
3052
int Th_ob_push( Th_Ob_Man * pMan, Blob ** pOut ){
  Blob * pBlob;
  int x, i;
  assert( NULL != pMan->interp );
  pBlob = (Blob *)Th_Malloc(pMan->interp, sizeof(Blob));
  *pBlob = empty_blob;

  if( pMan->cursor <= pMan->nBuf ){
    /* expand if needed */

    x = (pMan->cursor>0 ? pMan->cursor : 1) * 2;



    /*fprintf(stderr,"OB EXPAND x=%d\n",x);*/
    void * re = Th_Realloc( pMan->interp, pMan->aBuf, x * sizeof(Blob*) );
    if(NULL==re){
      goto error;
    }
    pMan->aBuf = (Blob **)re;
    re = Th_Realloc( pMan->interp, pMan->aVtab, x * sizeof(Th_Vtab*) );
................................................................................
  assert( pMan && (interp == pMan->interp) );
  b = pMan ? Th_ob_current(pMan) : NULL;
  if(!b){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }else{
    blob_reset(b);
  }

  return TH_OK;

}

/*
** TH Syntax:
**
** ob end
**
................................................................................
  b = Th_ob_pop(pMan);
  if(!b){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }else{
    blob_reset(b);
    Th_Free( interp, b );
  }

  return TH_OK;

}

/*
** TH Syntax:
**
** ob flush
**
** UNTESTED! Maybe not needed.
**
** Briefly reverts the output layer to the next-lower
** level, flushes the current buffer to that output layer,
** and clears out the current buffer. Does not change the
** buffering level.


*/
static int ob_flush_command( Th_Interp *interp, void *ctx,
                             int argc,  const char **argv, int *argl ){
  Th_Ob_Man * pMan = (Th_Ob_Man *)ctx;
  Blob * b = NULL;
  Th_Vtab * oldVtab;

  assert( pMan && (interp == pMan->interp) );
  b = Th_ob_current(pMan);
  if( NULL == b ){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }
  oldVtab = interp->pVtab;
  interp->pVtab = pMan->aVtab[pMan->cursor];
  Th_output( interp, blob_str(b), b->nUsed );
  interp->pVtab = oldVtab;
  blob_reset(b);













  return TH_OK;
}

/*
** TH Syntax:
**
** ob get ?clean|end?
**
** Fetches the contents of the current buffer level.  If either
** 'clean' or 'end' are specified then the effect is as if "ob clean"
** or "ob end", respectively, are called after fetching the
** value. Calling "ob get end" is functionality equivalent to "ob get"
** followed by "ob end".
*/
................................................................................
    if(argc>argPos){
      sub = argv[argPos];
      subL = argl[argPos];
      /* "ob get clean" */
      if(!rc && th_strlen(sub)==5 && 0==memcmp("clean", sub, subL)){
        rc |= ob_clean_command(interp, ctx, argc-1, argv+1, argl+1);
      }/* "ob get end" */
      else if(!rc && th_strlen(sub)==3 && 0==memcmp("end", sub, subL)){


        rc |= ob_end_command(interp, ctx, argc-1, argv+1, argl+1);
      }
    }
    return rc;
  }
}

................................................................................
  static Th_Ob_Man * pMan = &Th_Ob_Man_instance;
  Th_SubCommand aSub[] = {
    { "clean",     ob_clean_command },
    { "end",       ob_end_command },
    { "flush",     ob_flush_command },
    { "get",       ob_get_command },
    { "level",     ob_level_command },


    { "start",     ob_start_command },
    { 0, 0 }
  };
  if(NULL == pMan->interp){
    pMan->interp = interp;
    /*
      FIXME: add rudamentary at-finalization GC to Th_Interp and clean







|

>
|
>
>
>







 







<
>
|
>







 







<
>
|
>





|
<
<




|
>
>






>











>
>
>
>
>
>
>
>
>
>
>
>
>
|





|







 







|
>
>







 







>
>







2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
....
2879
2880
2881
2882
2883
2884
2885

2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
....
2904
2905
2906
2907
2908
2909
2910

2911
2912
2913
2914
2915
2916
2917
2918
2919


2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
....
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
....
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
int Th_ob_push( Th_Ob_Man * pMan, Blob ** pOut ){
  Blob * pBlob;
  int x, i;
  assert( NULL != pMan->interp );
  pBlob = (Blob *)Th_Malloc(pMan->interp, sizeof(Blob));
  *pBlob = empty_blob;

  if( pMan->cursor >= pMan->nBuf-2 ){
    /* expand if needed */
    x = pMan->nBuf + 5;
    if( pMan->cursor >= x ) {
      assert( 0 && "This really should not happen." );
      x = pMan->cursor + 5;
    }
    /*fprintf(stderr,"OB EXPAND x=%d\n",x);*/
    void * re = Th_Realloc( pMan->interp, pMan->aBuf, x * sizeof(Blob*) );
    if(NULL==re){
      goto error;
    }
    pMan->aBuf = (Blob **)re;
    re = Th_Realloc( pMan->interp, pMan->aVtab, x * sizeof(Th_Vtab*) );
................................................................................
  assert( pMan && (interp == pMan->interp) );
  b = pMan ? Th_ob_current(pMan) : NULL;
  if(!b){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }else{
    blob_reset(b);

    Th_SetResultInt( interp, 0 );
    return TH_OK;
  }
}

/*
** TH Syntax:
**
** ob end
**
................................................................................
  b = Th_ob_pop(pMan);
  if(!b){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }else{
    blob_reset(b);
    Th_Free( interp, b );

    Th_SetResultInt( interp, 0 );
    return TH_OK;
  }
}

/*
** TH Syntax:
**
** ob flush ?pop|end?


**
** Briefly reverts the output layer to the next-lower
** level, flushes the current buffer to that output layer,
** and clears out the current buffer. Does not change the
** buffering level unless "end" is specified, in which case
** it behaves as if "ob end" had been called (after flushing
** the buffer).
*/
static int ob_flush_command( Th_Interp *interp, void *ctx,
                             int argc,  const char **argv, int *argl ){
  Th_Ob_Man * pMan = (Th_Ob_Man *)ctx;
  Blob * b = NULL;
  Th_Vtab * oldVtab;
  int rc = TH_OK;
  assert( pMan && (interp == pMan->interp) );
  b = Th_ob_current(pMan);
  if( NULL == b ){
    Th_ErrorMessage( interp, "Not currently buffering.", NULL, 0 );
    return TH_ERROR;
  }
  oldVtab = interp->pVtab;
  interp->pVtab = pMan->aVtab[pMan->cursor];
  Th_output( interp, blob_str(b), b->nUsed );
  interp->pVtab = oldVtab;
  blob_reset(b);

  if(!rc && argc>2){
    int argPos = 2;
    char const * sub = argv[argPos];
    int subL = argl[argPos];
    /* "flush end" */
    if(th_strlen(sub)==3 &&
       ((0==memcmp("end", sub, subL)
         || (0==memcmp("pop", sub, subL))))){
      rc |= ob_end_command(interp, ctx, argc-1, argv+1, argl+1);
    }
  }
  Th_SetResultInt( interp, 0 );
  return rc;
}

/*
** TH Syntax:
**
** ob get ?clean|end|pop?
**
** Fetches the contents of the current buffer level.  If either
** 'clean' or 'end' are specified then the effect is as if "ob clean"
** or "ob end", respectively, are called after fetching the
** value. Calling "ob get end" is functionality equivalent to "ob get"
** followed by "ob end".
*/
................................................................................
    if(argc>argPos){
      sub = argv[argPos];
      subL = argl[argPos];
      /* "ob get clean" */
      if(!rc && th_strlen(sub)==5 && 0==memcmp("clean", sub, subL)){
        rc |= ob_clean_command(interp, ctx, argc-1, argv+1, argl+1);
      }/* "ob get end" */
      else if(!rc && th_strlen(sub)==3 &&
              ((0==memcmp("end", sub, subL))
               || (0==memcmp("pop", sub, subL)))){
        rc |= ob_end_command(interp, ctx, argc-1, argv+1, argl+1);
      }
    }
    return rc;
  }
}

................................................................................
  static Th_Ob_Man * pMan = &Th_Ob_Man_instance;
  Th_SubCommand aSub[] = {
    { "clean",     ob_clean_command },
    { "end",       ob_end_command },
    { "flush",     ob_flush_command },
    { "get",       ob_get_command },
    { "level",     ob_level_command },
    { "pop",       ob_end_command },
    { "push",      ob_start_command },
    { "start",     ob_start_command },
    { 0, 0 }
  };
  if(NULL == pMan->interp){
    pMan->interp = interp;
    /*
      FIXME: add rudamentary at-finalization GC to Th_Interp and clean