~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit_c.pyx

  • Committer: John Arbash Meinel
  • Date: 2007-05-09 20:04:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2614.
  • Revision ID: john@arbash-meinel.com-20070509200435-buae2sjnyn2olunc
Revert previous change since it doesn't help

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Pyrex extensions to knit parsing."""
18
18
 
19
 
from bzrlib import errors
 
19
import sys
20
20
 
21
21
cdef extern from "stdlib.h":
22
22
    long int strtol(char *nptr, char **endptr, int base)
269
269
 
270
270
        return self.process_one_record(start, last)
271
271
 
272
 
    cdef int check_header(self) except -1:
273
 
        """Check the header string is valid."""
274
 
        cdef char *end
275
 
        cdef char *header_str
276
 
        cdef int header_len
277
 
        cdef int disk_header_len
278
 
        cdef int invalid
279
 
 
280
 
        header = self.kndx.HEADER
281
 
        header_str = PyString_AsString(header)
282
 
        header_len = PyString_Size(header)
283
 
 
284
 
        invalid = 1
285
 
        end = strchr(self.cur_str, c'\n')
286
 
        if end != NULL:
287
 
            disk_header_len = end - self.cur_str + 1
288
 
            if (disk_header_len == header_len
289
 
                and strncmp(self.cur_str, header_str, header_len) == 0):
290
 
                invalid = 0
291
 
        if invalid == 1:
292
 
            line = PyString_FromStringAndSize(self.cur_str, disk_header_len)
293
 
            raise errors.KnitHeaderError(badline=line,
294
 
                              filename=self.kndx._transport.abspath(
295
 
                                                    self.kndx._filename))
296
 
        # If the header is correct, we move past it.
297
 
        self.cur_str = end
298
 
        return 0
299
 
 
300
272
    def read(self):
301
273
        cdef int text_size
302
274
 
303
275
        self.validate()
304
276
 
305
 
        text = self.fp.read()
306
 
        if PyString_Size(text) == 0:
307
 
            # An empty file can actually be treated as though the file doesn't
308
 
            # exist yet.
309
 
            raise errors.NoSuchFile(self.kndx._full_path())
 
277
        self.kndx.check_header(self.fp)
310
278
 
311
279
        # We read the whole thing at once
312
280
        # TODO: jam 2007-05-09 Consider reading incrementally rather than
316
284
        #       The other possibility is to avoid a Python String here
317
285
        #       completely. However self.fp may be a 'file-like' object
318
286
        #       it is not guaranteed to be a real file.
 
287
        text = self.fp.read()
319
288
        text_size = PyString_Size(text)
320
289
        self.cur_str = PyString_AsString(text)
321
290
        # This points to the last character in the string
322
291
        self.end_str = self.cur_str + text_size
323
 
        self.check_header()
324
292
 
325
293
        while self.cur_str < self.end_str:
326
294
            self.process_next_record()