mercurial/cffi/mpatch.py
changeset 43076 2372284d9457
parent 33572 857876ebaed4
child 43077 687b865b95ad
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    12 from . import _mpatch
    12 from . import _mpatch
    13 
    13 
    14 ffi = _mpatch.ffi
    14 ffi = _mpatch.ffi
    15 lib = _mpatch.lib
    15 lib = _mpatch.lib
    16 
    16 
       
    17 
    17 @ffi.def_extern()
    18 @ffi.def_extern()
    18 def cffi_get_next_item(arg, pos):
    19 def cffi_get_next_item(arg, pos):
    19     all, bins = ffi.from_handle(arg)
    20     all, bins = ffi.from_handle(arg)
    20     container = ffi.new("struct mpatch_flist*[1]")
    21     container = ffi.new("struct mpatch_flist*[1]")
    21     to_pass = ffi.new("char[]", str(bins[pos]))
    22     to_pass = ffi.new("char[]", str(bins[pos]))
    23     r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container)
    24     r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container)
    24     if r < 0:
    25     if r < 0:
    25         return ffi.NULL
    26         return ffi.NULL
    26     return container[0]
    27     return container[0]
    27 
    28 
       
    29 
    28 def patches(text, bins):
    30 def patches(text, bins):
    29     lgt = len(bins)
    31     lgt = len(bins)
    30     all = []
    32     all = []
    31     if not lgt:
    33     if not lgt:
    32         return text
    34         return text
    33     arg = (all, bins)
    35     arg = (all, bins)
    34     patch = lib.mpatch_fold(ffi.new_handle(arg),
    36     patch = lib.mpatch_fold(ffi.new_handle(arg), lib.cffi_get_next_item, 0, lgt)
    35                             lib.cffi_get_next_item, 0, lgt)
       
    36     if not patch:
    37     if not patch:
    37         raise mpatchError("cannot decode chunk")
    38         raise mpatchError("cannot decode chunk")
    38     outlen = lib.mpatch_calcsize(len(text), patch)
    39     outlen = lib.mpatch_calcsize(len(text), patch)
    39     if outlen < 0:
    40     if outlen < 0:
    40         lib.mpatch_lfree(patch)
    41         lib.mpatch_lfree(patch)