1# Copyright (c) 2010, Tomohiro Kusumi
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# 1. Redistributions of source code must retain the above copyright notice, this
8#    list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright notice,
10#    this list of conditions and the following disclaimer in the documentation
11#    and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24from .. import extension
25from .. import filebytes
26from .. import kernel
27from .. import screen
28from .. import setting
29from .. import util
30
31def get_text(co, fo, args):
32    tot = fo.get_size()
33    if not tot:
34        return "Empty buffer"
35
36    beg = args.pop()
37    pos = beg
38    rem = tot - pos
39    if extension.test_dryrun():
40        if rem > 1024:
41            rem = 1024
42
43    siz = kernel.get_buffer_size()
44    l = []
45
46    while True:
47        b = fo.read(pos, siz)
48        if b:
49            n = 0
50            for i, c in enumerate(filebytes.iter_ords(b)):
51                if util.isprint(c):
52                    n += 1
53                else:
54                    if n >= setting.ext_strings_thresh:
55                        s = filebytes.str(b[i - n:i])
56                        l.append((pos + i - n, s))
57                    n = 0
58            if len(b) == n:
59                l = [(pos, b)]
60        pos += len(b)
61        rem -= len(b)
62        if rem <= 0:
63            break
64        if screen.test_signal():
65            co.flash("Interrupted ({0})".format(pos))
66            break
67
68    sl = ["Range {0}-{1}".format(util.get_size_repr(beg),
69        util.get_size_repr(pos - 1))]
70    sl.append("Found {0} strings".format(len(l)))
71
72    if l:
73        sl.append('')
74        n = max([len(str(x[0])) for x in l])
75        f = "{{0:{0}}} {{1}}".format(n)
76        for i, x in enumerate(l):
77            sl.append(f.format(x[0], x[1]))
78    return sl
79
80def init():
81    setting.ext_add_gt_zero("strings_thresh", 3,
82        "Set number of minimum string length for :strings. "
83        "Defaults to 3 if undefined.")
84
85def cleanup():
86    setting.ext_delete("strings_thresh")
87
88init()
89