summaryrefslogtreecommitdiffstats
path: root/tests/arthur/data
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2011-05-10 13:08:29 (GMT)
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2011-06-06 14:02:33 (GMT)
commit86d88c5b719fd3d50336d9d8e7127b8045ee82ae (patch)
treecd6ad2732f7eec8d7b3530b0d35908c0f0c130ef /tests/arthur/data
parent71cd7dfea1b8f0e287eb1616c0cda5493508c3c4 (diff)
downloadQt-86d88c5b719fd3d50336d9d8e7127b8045ee82ae.zip
Qt-86d88c5b719fd3d50336d9d8e7127b8045ee82ae.tar.gz
Qt-86d88c5b719fd3d50336d9d8e7127b8045ee82ae.tar.bz2
Add function QGlyphRun::setRawData()
To provide an optimized way of constructing QGlyphRun objects with no copying or allocation, we add function setRawData() (naming inspired by QByteArray::setRawData()). Data retrieved from QRawFont can be passed directly into this. The logic is now that the data pointers in QGlyphRunPrivate should always point to the current valid data and is what will be used in comparisons and drawing calls. The vectors are optimizations to avoid unnecessary copying if the user wants to use the QVector based API (which makes it easier to manage the memory.) This reflected in the functions that return QVectors, which will return the stored vector if and only if it is identical to the current pointer. Otherwise we will have to copy the memory. The internal addition operators in QGlyphRun have been removed since they really provide no real optimization and have an unclear definition if the two glyph runs are based on different fonts. Reviewed-by: Jiang Jiang
Diffstat (limited to 'tests/arthur/data')
0 files changed, 0 insertions, 0 deletions
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
#! /usr/bin/env python3

# A simple gopher client.
#
# Usage: gopher [ [selector] host [port] ]

import sys
import os
import socket

# Default selector, host and port
DEF_SELECTOR = ''
DEF_HOST     = 'gopher.micro.umn.edu'
DEF_PORT     = 70

# Recognized file types
T_TEXTFILE  = '0'
T_MENU      = '1'
T_CSO       = '2'
T_ERROR     = '3'
T_BINHEX    = '4'
T_DOS       = '5'
T_UUENCODE  = '6'
T_SEARCH    = '7'
T_TELNET    = '8'
T_BINARY    = '9'
T_REDUNDANT = '+'
T_SOUND     = 's'

# Dictionary mapping types to strings
typename = {'0': '<TEXT>', '1': '<DIR>', '2': '<CSO>', '3': '<ERROR>', \
        '4': '<BINHEX>', '5': '<DOS>', '6': '<UUENCODE>', '7': '<SEARCH>', \
        '8': '<TELNET>', '9': '<BINARY>', '+': '<REDUNDANT>', 's': '<SOUND>'}

# Oft-used characters and strings
CRLF = '\r\n'
TAB = '\t'

# Open a TCP connection to a given host and port
def open_socket(host, port):
    if not port:
        port = DEF_PORT
    elif type(port) == type(''):
        port = int(port)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    return s

# Send a selector to a given host and port, return a file with the reply
def send_request(selector, host, port):
    s = open_socket(host, port)
    s.send(selector + CRLF)
    s.shutdown(1)
    return s.makefile('r')

# Get a menu in the form of a list of entries
def get_menu(selector, host, port):
    f = send_request(selector, host, port)
    list = []
    while 1:
        line = f.readline()
        if not line:
            print('(Unexpected EOF from server)')
            break
        if line[-2:] == CRLF:
            line = line[:-2]
        elif line[-1:] in CRLF:
            line = line[:-1]
        if line == '.':
            break
        if not line:
            print('(Empty line from server)')
            continue
        typechar = line[0]
        parts = line[1:].split(TAB)
        if len(parts) < 4:
            print('(Bad line from server: %r)' % (line,))
            continue
        if len(parts) > 4:
            print('(Extra info from server: %r)' % (parts[4:],))
        parts.insert(0, typechar)
        list.append(parts)
    f.close()
    return list

# Get a text file as a list of lines, with trailing CRLF stripped
def get_textfile(selector, host, port):
    list = []
    get_alt_textfile(selector, host, port, list.append)
    return list

# Get a text file and pass each line to a function, with trailing CRLF stripped
def get_alt_textfile(selector, host, port, func):
    f = send_request(selector, host, port)
    while 1:
        line = f.readline()
        if not line:
            print('(Unexpected EOF from server)')
            break
        if line[-2:] == CRLF:
            line = line[:-2]
        elif line[-1:] in CRLF:
            line = line[:-1]
        if line == '.':
            break
        if line[:2] == '..':
            line = line[1:]
        func(line)
    f.close()

# Get a binary file as one solid data block
def get_binary(selector, host, port):
    f = send_request(selector, host, port)
    data = f.read()
    f.close()
    return data

# Get a binary file and pass each block to a function
def get_alt_binary(selector, host, port, func, blocksize):
    f = send_request(selector, host, port)
    while 1:
        data = f.read(blocksize)
        if not data:
            break
        func(data)

# A *very* simple interactive browser

# Browser main command, has default arguments
def browser(*args):
    selector = DEF_SELECTOR
    host = DEF_HOST
    port = DEF_PORT
    n = len(args)
    if n > 0 and args[0]:
        selector = args[0]
    if n > 1 and args[1]:
        host = args[1]
    if n > 2 and args[2]:
        port = args[2]
    if n > 3:
        raise RuntimeError('too many args')
    try:
        browse_menu(selector, host, port)
    except socket.error as msg:
        print('Socket error:', msg)
        sys.exit(1)
    except KeyboardInterrupt:
        print('\n[Goodbye]')

# Browse a menu
def browse_menu(selector, host, port):
    list = get_menu(selector, host, port)
    while 1:
        print('----- MENU -----')
        print('Selector:', repr(selector))
        print('Host:', host, ' Port:', port)
        print()
        for i in range(len(list)):
            item = list[i]
            typechar, description = item[0], item[1]
            print(repr(i+1).rjust(3) + ':', description, end=' ')
            if typechar in typename:
                print(typename[typechar])
            else:
                print('<TYPE=' + repr(typechar) + '>')
        print()
        while 1:
            try:
                str = input('Choice [CR == up a level]: ')
            except EOFError:
                print()
                return
            if not str:
                return
            try:
                choice = int(str)
            except ValueError:
                print('Choice must be a number; try again:')
                continue
            if not 0 < choice <= len(list):
                print('Choice out of range; try again:')
                continue
            break
        item = list[choice-1]
        typechar = item[0]
        [i_selector, i_host, i_port] = item[2:5]
        if typechar in typebrowser:
            browserfunc = typebrowser[typechar]
            try:
                browserfunc(i_selector, i_host, i_port)
            except (IOError, socket.error):
                t, v, tb = sys.exc_info()
                print('***', t, ':', v)
        else:
            print('Unsupported object type')

# Browse a text file
def browse_textfile(selector, host, port):
    x = None
    try:
        p = os.popen('${PAGER-more}', 'w')
        x = SaveLines(p)
        get_alt_textfile(selector, host, port, x.writeln)
    except IOError as msg:
        print('IOError:', msg)
    if x:
        x.close()
    f = open_savefile()
    if not f:
        return
    x = SaveLines(f)
    try:
        get_alt_textfile(selector, host, port, x.writeln)
        print('Done.')
    except IOError as msg:
        print('IOError:', msg)
    x.close()

def raw_input(prompt):
    sys.stdout.write(prompt)
    sys.stdout.flush()
    return sys.stdin.readline()

# Browse a search index
def browse_search(selector, host, port):
    while 1:
        print('----- SEARCH -----')
        print('Selector:', repr(selector))
        print('Host:', host, ' Port:', port)
        print()
        try:
            query = input('Query [CR == up a level]: ')
        except EOFError:
            print()
            break
        query = query.strip()
        if not query:
            break
        if '\t' in query:
            print('Sorry, queries cannot contain tabs')
            continue
        browse_menu(selector + TAB + query, host, port)

# "Browse" telnet-based information, i.e. open a telnet session
def browse_telnet(selector, host, port):
    if selector:
        print('Log in as', repr(selector))
    if type(port) != type(''):
        port = repr(port)
    sts = os.system('set -x; exec telnet ' + host + ' ' + port)
    if sts:
        print('Exit status:', sts)

# "Browse" a binary file, i.e. save it to a file
def browse_binary(selector, host, port):
    f = open_savefile()
    if not f:
        return
    x = SaveWithProgress(f)
    get_alt_binary(selector, host, port, x.write, 8*1024)
    x.close()

# "Browse" a sound file, i.e. play it or save it
def browse_sound(selector, host, port):
    browse_binary(selector, host, port)

# Dictionary mapping types to browser functions
typebrowser = {'0': browse_textfile, '1': browse_menu, \
        '4': browse_binary, '5': browse_binary, '6': browse_textfile, \
        '7': browse_search, \
        '8': browse_telnet, '9': browse_binary, 's': browse_sound}

# Class used to save lines, appending a newline to each line
class SaveLines:
    def __init__(self, f):
        self.f = f
    def writeln(self, line):
        self.f.write(line + '\n')
    def close(self):
        sts = self.f.close()
        if sts:
            print('Exit status:', sts)

# Class used to save data while showing progress
class SaveWithProgress:
    def __init__(self, f):
        self.f = f
    def write(self, data):
        sys.stdout.write('#')
        sys.stdout.flush()
        self.f.write(data)
    def close(self):
        print()
        sts = self.f.close()
        if sts:
            print('Exit status:', sts)

# Ask for and open a save file, or return None if not to save
def open_savefile():
    try:
        savefile = input( \
    'Save as file [CR == don\'t save; |pipeline or ~user/... OK]: ')
    except EOFError:
        print()
        return None
    savefile = savefile.strip()
    if not savefile:
        return None
    if savefile[0] == '|':
        cmd = savefile[1:].strip()
        try:
            p = os.popen(cmd, 'w')
        except IOError as msg:
            print(repr(cmd), ':', msg)
            return None
        print('Piping through', repr(cmd), '...')
        return p
    if savefile[0] == '~':
        savefile = os.path.expanduser(savefile)
    try:
        f = open(savefile, 'w')
    except IOError as msg:
        print(repr(savefile), ':', msg)
        return None
    print('Saving to', repr(savefile), '...')
    return f

# Test program
def test():
    if sys.argv[4:]:
        print('usage: gopher [ [selector] host [port] ]')
        sys.exit(2)
    elif sys.argv[3:]:
        browser(sys.argv[1], sys.argv[2], sys.argv[3])
    elif sys.argv[2:]:
        try:
            port = int(sys.argv[2])
            selector = ''
            host = sys.argv[1]
        except ValueError:
            selector = sys.argv[1]
            host = sys.argv[2]
            port = ''
        browser(selector, host, port)
    elif sys.argv[1:]:
        browser('', sys.argv[1])
    else:
        browser()

# Call the test program as a main program
test()