- Main Page
- Related Pages
- Data Structures
- Files
- Examples

- File List

# /home/v/Kate/WORK_TREES/kate-sorceress-backup/v/src/kate/src/kate_encode_state.c

    00001 /* Copyright (C) 2008, 2009 Vincent Penquerc'h.
    00002    This file is part of the Kate codec library.
    00003    Written by Vincent Penquerc'h.
    00004
    00005    Use, distribution and reproduction of this library is governed
    00006    by a BSD style source license included with this source in the
    00007    file 'COPYING'. Please read these terms before distributing. */
    00008
    00009
    00010 #define KATE_INTERNAL
    00011 #include "kate_internal.h"
    00012
    00013 #ifdef HAVE_STDLIB_H
    00014 #include <stdlib.h>
    00015 #endif
    00016 #include <string.h>
    00017 #include "kate/kate.h"
    00018 #include "kate_encode_state.h"
    00019
    00020 static void kate_encode_state_init_helper(kate_encode_state *kes)
    00021 {
    00022   kes->motions=NULL;
    00023   kes->destroy_motions=NULL;
    00024   kes->motion_indices=NULL;
    00025   kes->nmotions=0;
    00026
    00027   kes->bitmaps=NULL;
    00028   kes->bitmap_indices=NULL;
    00029   kes->nbitmaps=0;
    00030
    00031   kes->overrides.region_index=-1;
    00032   kes->overrides.region=NULL;
    00033   kes->overrides.style_index=-1;
    00034   kes->overrides.style=NULL;
    00035   kes->overrides.secondary_style_index=-1;
    00036   kes->overrides.secondary_style=NULL;
    00037
    00038   kes->overrides.palette_index=-1;
    00039   kes->overrides.palette=NULL;
    00040   kes->overrides.bitmap_index=-1;
    00041   kes->overrides.bitmap=NULL;
    00042
    00043   kes->overrides.language=NULL;
    00044
    00045   kes->overrides.font_mapping_index=-1;
    00046 }
    00047
    00048 kate_encode_state *kate_encode_state_create(const kate_info *ki)
    00049 {
    00050   kate_encode_state *kes=(kate_encode_state*)kate_malloc(sizeof(kate_encode_state));
    00051   if (kes) {
    00052     kate_pack_writeinit(&kes->kpb);
    00053
    00054     kes->ki=ki;
    00055
    00056     kes->id=0;
    00057
    00058     kes->granulepos=0;
    00059     kes->packetno=-1;
    00060
    00061     kes->furthest_granule=0;
    00062
    00063     kes->eos=0;
    00064
    00065     kes->ntimings=0;
    00066     kes->timings=NULL;
    00067
    00068     kate_encode_state_init_helper(kes);
    00069   }
    00070   return kes;
    00071 }
    00072
    00073 int kate_encode_state_clear_overrides(kate_encode_state *kes)
    00074 {
    00075   if (!kes) return KATE_E_INVALID_PARAMETER;
    00076   if (!kes->ki) return KATE_E_INIT;
    00077
    00078   if (kes->motions) {
    00079     kate_motion_destroy(kes->ki,kes->motions,kes->destroy_motions,kes->nmotions,0);
    00080   }
    00081   if (kes->destroy_motions) {
    00082     kate_free(kes->destroy_motions);
    00083   }
    00084   if (kes->motion_indices) {
    00085     kate_free(kes->motion_indices);
    00086   }
    00087
    00088   if (kes->bitmaps) {
    00089     kate_free(kes->bitmaps);
    00090   }
    00091   if (kes->bitmap_indices) {
    00092     kate_free(kes->bitmap_indices);
    00093   }
    00094
    00095   if (kes->overrides.language) {
    00096     kate_free(kes->overrides.language);
    00097   }
    00098
    00099   kes->overrides.text_encoding=kes->ki->text_encoding;
    00100   kes->overrides.text_directionality=kes->ki->text_directionality;
    00101   kes->overrides.text_markup_type=kes->ki->text_markup_type;
    00102
    00103   kate_encode_state_init_helper(kes);
    00104
    00105   return 0;
    00106 }
    00107
    00108 static int kate_encode_state_add_motion_or_index(kate_encode_state *kes,kate_motion *km,size_t motion,int destroy)
    00109 {
    00110   kate_motion **motions;
    00111   int *destroy_motions;
    00112   size_t *motion_indices;
    00113   kate_motion_semantics semantics;
    00114   size_t n;
    00115   int ret;
    00116
    00117   if (!kes) return KATE_E_INVALID_PARAMETER;
    00118   if (!kes->ki) return KATE_E_INIT;
    00119   if (!km && motion>=kes->ki->nmotions) return KATE_E_INVALID_PARAMETER;
    00120
    00121   ret=kate_check_add_overflow(kes->nmotions,1,NULL);
    00122   if (ret<0) return ret;
    00123
    00124   /* check if we have that semantic already */
    00125   semantics=km?km->semantics:kes->ki->motions[motion]->semantics;
    00126   for (n=0;n<kes->nmotions;++n) {
    00127     const kate_motion *km2=kes->motions[n];
    00128     if (!km2) km2=kes->ki->motions[kes->motion_indices[n]];
    00129     if (km2->semantics==semantics) return KATE_E_LIMIT;
    00130   }
    00131
    00132   motions=(kate_motion**)kate_checked_realloc(kes->motions,(kes->nmotions+1),sizeof(kate_motion*));
    00133   if (!motions) return KATE_E_OUT_OF_MEMORY;
    00134   kes->motions=motions;
    00135
    00136   destroy_motions=(int*)kate_checked_realloc(kes->destroy_motions,(kes->nmotions+1),sizeof(int));
    00137   if (!destroy_motions) return KATE_E_OUT_OF_MEMORY;
    00138   kes->destroy_motions=destroy_motions;
    00139
    00140   motion_indices=(size_t*)kate_checked_realloc(kes->motion_indices,(kes->nmotions+1),sizeof(size_t));
    00141   if (!motion_indices) return KATE_E_OUT_OF_MEMORY;
    00142   kes->motion_indices=motion_indices;
    00143
    00144   kes->motions[kes->nmotions]=km;
    00145   kes->destroy_motions[kes->nmotions]=destroy;
    00146   kes->motion_indices[kes->nmotions]=motion;
    00147   ++kes->nmotions;
    00148
    00149   return 0;
    00150 }
    00151
    00152 int kate_encode_state_add_motion(kate_encode_state *kes,kate_motion *km,int destroy)
    00153 {
    00154   if (!kes || !km) return KATE_E_INVALID_PARAMETER;
    00155   return kate_encode_state_add_motion_or_index(kes,km,0,destroy);
    00156 }
    00157
    00158 int kate_encode_state_add_motion_index(kate_encode_state *kes,size_t motion)
    00159 {
    00160   if (!kes) return KATE_E_INVALID_PARAMETER;
    00161   return kate_encode_state_add_motion_or_index(kes,NULL,motion,0);
    00162 }
    00163
    00164 static int kate_encode_state_add_bitmap_or_index(kate_encode_state *kes,const kate_bitmap *kb,size_t bitmap)
    00165 {
    00166   const kate_bitmap **bitmaps;
    00167   size_t *bitmap_indices;
    00168   int ret;
    00169
    00170   if (!kes) return KATE_E_INVALID_PARAMETER;
    00171   if (!kes->ki) return KATE_E_INIT;
    00172   if (!kb && bitmap>=kes->ki->nbitmaps) return KATE_E_INVALID_PARAMETER;
    00173
    00174   ret=kate_check_add_overflow(kes->nbitmaps,1,NULL);
    00175   if (ret<0) return ret;
    00176
    00177   bitmaps=(const kate_bitmap**)kate_checked_realloc(kes->bitmaps,(kes->nbitmaps+1),sizeof(const kate_bitmap*));
    00178   if (!bitmaps) return KATE_E_OUT_OF_MEMORY;
    00179   kes->bitmaps=bitmaps;
    00180
    00181   bitmap_indices=(size_t*)kate_checked_realloc(kes->bitmap_indices,(kes->nbitmaps+1),sizeof(size_t));
    00182   if (!bitmap_indices) return KATE_E_OUT_OF_MEMORY;
    00183   kes->bitmap_indices=bitmap_indices;
    00184
    00185   kes->bitmaps[kes->nbitmaps]=kb;
    00186   kes->bitmap_indices[kes->nbitmaps]=bitmap;
    00187   ++kes->nbitmaps;
    00188
    00189   return 0;
    00190 }
    00191
    00192 int kate_encode_state_add_bitmap(kate_encode_state *kes,const kate_bitmap *kb)
    00193 {
    00194   if (!kes || !kb) return KATE_E_INVALID_PARAMETER;
    00195   return kate_encode_state_add_bitmap_or_index(kes,kb,0);
    00196 }
    00197
    00198 int kate_encode_state_add_bitmap_index(kate_encode_state *kes,size_t bitmap)
    00199 {
    00200   if (!kes) return KATE_E_INVALID_PARAMETER;
    00201   return kate_encode_state_add_bitmap_or_index(kes,NULL,bitmap);
    00202 }
    00203
    00204 int kate_encode_state_destroy(kate_encode_state *kes)
    00205 {
    00206   size_t n;
    00207
    00208   if (!kes) return KATE_E_INVALID_PARAMETER;
    00209
    00210   kate_pack_writeclear(&kes->kpb);
    00211   if (kes->timings) {
    00212     for (n=0;n<kes->ntimings;++n) {
    00213       if (kes->timings[n].original_data) kate_free(kes->timings[n].original_data);
    00214       if (kes->timings[n].repeat_data) kate_free(kes->timings[n].repeat_data);
    00215     }
    00216     kate_free(kes->timings);
    00217   }
    00218   if (kes->motions) kate_free(kes->motions);
    00219   if (kes->destroy_motions) kate_free(kes->destroy_motions);
    00220   if (kes->motion_indices) kate_free(kes->motion_indices);
    00221   if (kes->bitmaps) kate_free(kes->bitmaps);
    00222   if (kes->bitmap_indices) kate_free(kes->bitmap_indices);
    00223   if (kes->overrides.language) kate_free(kes->overrides.language);
    00224   kate_free(kes);
    00225
    00226   return 0;
    00227 }
    00228
    00229 int kate_encode_state_add_event(kate_encode_state *kes,kate_int64_t start,kate_int64_t end)
    00230 {
    00231   kate_event_timing *new_timings;
    00232   int ret;
    00233
    00234   if (!kes) return KATE_E_INVALID_PARAMETER;
    00235   if (start<0 || end<0 || end<start) return KATE_E_INVALID_PARAMETER;
    00236
    00237   ret=kate_check_add_overflow(kes->ntimings,1,NULL);
    00238   if (ret<0) return ret;
    00239
    00240   new_timings=(kate_event_timing*)kate_checked_realloc(kes->timings,(kes->ntimings+1),sizeof(kate_event_timing));
    00241   if (!new_timings) return KATE_E_OUT_OF_MEMORY;
    00242   kes->timings=new_timings;
    00243
    00244   kes->timings[kes->ntimings].start=start;
    00245   kes->timings[kes->ntimings].end=end;
    00246   kes->timings[kes->ntimings].id=kes->id;
    00247   kes->timings[kes->ntimings].repeat=start;
    00248   kes->timings[kes->ntimings].original_size=0;
    00249   kes->timings[kes->ntimings].original_data=NULL;
    00250   kes->timings[kes->ntimings].repeat_size=0;
    00251   kes->timings[kes->ntimings].repeat_data=NULL;
    00252   ++kes->ntimings;
    00253
    00254   return 0;
    00255 }
    00256
    00257 int kate_encode_state_get_earliest_event(kate_encode_state *kes,kate_int64_t *start,kate_int64_t *end)
    00258 {
    00259   size_t n;
    00260
    00261   if (!kes || !start) return KATE_E_INVALID_PARAMETER; /* end may be NULL */
    00262   if (!kes->ntimings) return KATE_E_NOT_FOUND;
    00263
    00264   for (n=0;n<kes->ntimings;++n) {
    00265     /* repeat will be either start, or later if a repeat has been emitted */
    00266     if (n==0 || kes->timings[n].repeat<*start) {
    00267       *start=kes->timings[n].repeat;
    00268       if (end) *end=kes->timings[n].end;
    00269     }
    00270   }
    00271
    00272   return 0;
    00273 }
    00274
    00275 int kate_encode_state_get_latest_event(kate_encode_state *kes,kate_int64_t *start,kate_int64_t *end)
    00276 {
    00277   size_t n;
    00278
    00279   if (!kes || !end) return KATE_E_INVALID_PARAMETER; /* start may be NULL */
    00280   if (!kes->ntimings) return KATE_E_NOT_FOUND;
    00281
    00282   for (n=0;n<kes->ntimings;++n) {
    00283     if (n==0 || kes->timings[n].end>*end) {
    00284       if (start) *start=kes->timings[n].start;
    00285       *end=kes->timings[n].end;
    00286     }
    00287   }
    00288
    00289   return 0;
    00290 }
    00291
    00292 int kate_encode_state_trim_events(kate_encode_state *kes,kate_int64_t t)
    00293 {
    00294   size_t n;
    00295
    00296   if (!kes) return KATE_E_INVALID_PARAMETER;
    00297
    00298   for (n=0;n<kes->ntimings;++n) {
    00299     if (kes->timings[n].end<=t) {
    00300       if (kes->timings[n].original_data) kate_free(kes->timings[n].original_data);
    00301       if (kes->timings[n].repeat_data) kate_free(kes->timings[n].repeat_data);
    00302       kes->timings[n--]=kes->timings[--kes->ntimings];
    00303     }
    00304   }
    00305
    00306   return 0;
    00307 }
    00308
    00309 int kate_encode_state_save_event_buffer(kate_encode_state *kes,size_t size,const void *data)
    00310 {
    00311   kate_event_timing *ket;
    00312
    00313   if (!kes) return KATE_E_INVALID_PARAMETER;
    00314   if (!data || size==0) return KATE_E_INVALID_PARAMETER;
    00315   if (kes->ntimings==0) return KATE_E_INIT;
    00316
    00317   /* store in the last added slot */
    00318   ket=&kes->timings[kes->ntimings-1];
    00319
    00320   /* initialized already ? */
    00321   if (ket->original_data || ket->original_size) return KATE_E_INIT;
    00322   if (ket->repeat_data || ket->repeat_size) return KATE_E_INIT;
    00323
    00324   /* increment id */
    00325   ++kes->id;
    00326   if (kes->id<0) return KATE_E_LIMIT; /* 2 billion or so events limit */
    00327
    00328   /* store packet data */
    00329   ket->original_data=kate_malloc(size);
    00330   if (!ket->original_data) return KATE_E_OUT_OF_MEMORY;
    00331   memcpy(ket->original_data,data,size);
    00332   ket->original_size=size;
    00333
    00334   return 0;
    00335 }
    00336
    00337 int kate_encode_state_get_repeat(kate_encode_state *kes,kate_int64_t t,kate_int64_t threshold,kate_packet *kp)
    00338 {
    00339   size_t n;
    00340   kate_event_timing *ket;
    00341   unsigned char packet_type;
    00342
    00343   if (!kes) return KATE_E_INVALID_PARAMETER; /* kp may be NULL */
    00344
    00345   for (n=0;n<kes->ntimings;++n) {
    00346     ket=kes->timings+n;
    00347     /* old enough ? */
    00348     if (threshold==0) {
    00349       if (ket->repeat>=t-threshold) continue;
    00350     }
    00351     else {
    00352       if (ket->repeat>t-threshold) continue;
    00353     }
    00354     /* has data ? */
    00355     if (!ket->original_data) continue;
    00356     /* is a text packet ? */
    00357     if (ket->original_size<1+8*3) continue;
    00358     packet_type=((const unsigned char*)ket->original_data)[0];
    00359     if (packet_type!=0x00) continue;
    00360
    00361     /* we found an older one */
    00362     ket->repeat = t;
    00363
    00364     /* create a repeat packet if there is none yet */
    00365     if (!ket->repeat_data) {
    00366       /* we're in luck, it's the same size */
    00367       ket->repeat_data=kate_malloc(ket->original_size);
    00368       if (!ket->repeat_data) return KATE_E_OUT_OF_MEMORY;
    00369       memcpy(ket->repeat_data,ket->original_data,ket->original_size);
    00370       ket->repeat_size=ket->original_size;
    00371       /* set packet type to repeat */
    00372       ((unsigned char*)ket->repeat_data)[0]=0x02;
    00373     }
    00374
    00375     kate_packet_init(kp,ket->repeat_size,ket->repeat_data);
    00376
    00377     return 1;
    00378   }
    00379
    00380   return 0;
    00381 }
    00382