~ubuntu-branches/ubuntu/utopic/geany/utopic

« back to all changes in this revision

Viewing changes to tagmanager/read.c

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-12-10 07:43:26 UTC
  • mfrom: (3.3.7 sid)
  • Revision ID: package-import@ubuntu.com-20111210074326-s8yqbew5i20h33tf
Tags: 0.21-1ubuntu1
* Merge from Debian Unstable, remaining changes:
  - debian/patches/20_use_evince_viewer.patch:
     + use evince as viewer for pdf and dvi files
  - debian/patches/20_use_x_terminal_emulator.patch:
     + use x-terminal-emulator as terminal
  - debian/control
     + Add breaks on geany-plugins-common << 0.20
* Also fixes bugs:
  - Filter for MATLAB/Octave files filters everythign (LP: 885505)

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
*   DATA DEFINITIONS
29
29
*/
30
30
inputFile File;                 /* globally read through macros */
31
 
static fpos_t StartOfLine;      /* holds deferred position of start of line */
32
 
static int bufferStartOfLine;   /* the same as StartOfLine but for buffer */
33
 
 
34
 
 
35
 
static int readNextChar (void);
36
 
static int pushBackChar (int c);
 
31
static MIOPos StartOfLine;      /* holds deferred position of start of line */
 
32
 
 
33
 
 
34
 
 
35
/* Read a character choosing automatically between file or buffer, depending
 
36
 * on which mode we are.
 
37
 */
 
38
#define readNextChar() (mio_getc (File.mio))
 
39
 
 
40
/* Replaces ungetc() for file. In case of buffer we'll perform the same action:
 
41
 * fpBufferPosition-- and write of the param char into the buf.
 
42
 */
 
43
#define pushBackChar(c) (mio_ungetc (File.mio, c))
37
44
 
38
45
/*
39
46
*   FUNCTION DEFINITIONS
253
260
 
254
261
    /*  If another file was already open, then close it.
255
262
     */
256
 
    if (File.fp != NULL)
 
263
    if (File.mio != NULL)
257
264
    {
258
 
        fclose (File.fp);               /* close any open source file */
259
 
        File.fp = NULL;
 
265
        mio_free (File.mio);            /* close any open source file */
 
266
        File.mio = NULL;
260
267
    }
261
268
 
262
 
    File.fp = g_fopen (fileName, openMode);
263
 
    if (File.fp == NULL)
 
269
    File.mio = mio_new_file_full (fileName, openMode, g_fopen, fclose);
 
270
    if (File.mio == NULL)
264
271
        error (WARNING | PERROR, "cannot open \"%s\"", fileName);
265
272
    else
266
273
    {
267
274
        opened = TRUE;
268
275
 
269
276
        setInputFileName (fileName);
270
 
        fgetpos (File.fp, &StartOfLine);
271
 
        fgetpos (File.fp, &File.filePosition);
 
277
        mio_getpos (File.mio, &StartOfLine);
 
278
        mio_getpos (File.mio, &File.filePosition);
272
279
        File.currentLine  = NULL;
273
280
        File.language     = language;
274
281
        File.lineNumber   = 0L;
297
304
{
298
305
    boolean opened = FALSE;
299
306
        
300
 
    /*  Check whether a file of a buffer were already open, then close them.
 
307
    /* Check whether a file of a buffer were already open, then close them.
301
308
     */
302
 
    if (File.fp != NULL) {
303
 
        fclose (File.fp);               /* close any open source file */
304
 
        File.fp = NULL;
 
309
    if (File.mio != NULL) {
 
310
        mio_free (File.mio);            /* close any open source file */
 
311
        File.mio = NULL;
305
312
    }
306
313
 
307
 
    if (File.fpBuffer != NULL) {
308
 
        error(PERROR, "An unallocated buffer was found. Please check you called \
309
 
        correctly bufferClose ()\n");
310
 
        File.fpBuffer = NULL;
311
 
    }
312
 
    
313
314
    /* check if we got a good buffer */
314
315
    if (buffer == NULL || buffer_size == 0) {
315
316
        opened = FALSE;
318
319
        
319
320
    opened = TRUE;
320
321
            
321
 
    File.fpBuffer = buffer;             
 
322
    File.mio = mio_new_memory (buffer, buffer_size, NULL, NULL);
322
323
    setInputFileName (fileName);
323
 
    bufferStartOfLine = 0;
324
 
    File.fpBufferPosition = 0;
325
 
    File.fpBufferSize = buffer_size;
 
324
    mio_getpos (File.mio, &StartOfLine);
 
325
    mio_getpos (File.mio, &File.filePosition);
326
326
    File.currentLine  = NULL;
327
327
    File.language     = language;
328
328
    File.lineNumber   = 0L;
339
339
            getLanguageName (language),
340
340
            File.source.isHeader ? "include " : "");
341
341
 
342
 
    return opened;      
 
342
    return opened;
343
343
}
344
344
 
345
345
extern void fileClose (void)
346
346
{
347
 
    if (File.fp != NULL)
 
347
    if (File.mio != NULL)
348
348
    {
349
349
        /*  The line count of the file is 1 too big, since it is one-based
350
350
         *  and is incremented upon each newline.
353
353
            addTotals (0, File.lineNumber - 1L,
354
354
                      getFileSize (vStringValue (File.name)));
355
355
 
356
 
        fclose (File.fp);
357
 
        File.fp = NULL;
358
 
    }
359
 
}
360
 
 
361
 
/* user should take care of freeing the buffer */
362
 
extern void bufferClose (void)
363
 
{
364
 
    if (File.fpBuffer != NULL) {
365
 
        File.fpBuffer = NULL;
 
356
        mio_free (File.mio);
 
357
        File.mio = NULL;
366
358
    }
367
359
}
368
360
 
403
395
                goto readnext;
404
396
            else
405
397
            {
406
 
                /* FIXME: find out a better way to do this check */
407
 
                if (File.fp != NULL)
408
 
                    fsetpos (File.fp, &StartOfLine);
409
 
                else
410
 
                    File.fpBufferPosition = bufferStartOfLine;
 
398
                mio_setpos (File.mio, &StartOfLine);
411
399
 
412
400
                c = readNextChar ();
413
401
            }
419
407
    else if (c == NEWLINE)
420
408
    {
421
409
        File.newLine = TRUE;
422
 
        if (File.fp != NULL)            /* we have a file */
423
 
            fgetpos (File.fp, &StartOfLine);
424
 
        else                                            /* it's a buffer */
425
 
            bufferStartOfLine = File.fpBufferPosition;
 
410
        mio_getpos (File.mio, &StartOfLine);
426
411
    }
427
412
    else if (c == CRETURN)
428
413
    {
437
422
 
438
423
        c = NEWLINE;                            /* convert CR into newline */
439
424
        File.newLine = TRUE;
440
 
        if (File.fp != NULL)
441
 
            fgetpos (File.fp, &StartOfLine);
442
 
        else
443
 
            bufferStartOfLine = File.fpBufferPosition;
 
425
        mio_getpos (File.mio, &StartOfLine);
444
426
    }
445
427
    DebugStatement ( debugPutc (DEBUG_RAW, c); )
446
428
    return c;
535
517
    return result;
536
518
}
537
519
 
538
 
/* Read a character choosing automatically between file or buffer, depending
539
 
 * on which mode we are.
540
 
 */
541
 
static int readNextChar(void) 
542
 
{
543
 
    if (File.fp != NULL) {
544
 
        return getc(File.fp);
545
 
    }
546
 
    else {
547
 
        int c;
548
 
        if (File.fpBufferPosition >= File.fpBufferSize)
549
 
            return EOF;
550
 
 
551
 
        c = File.fpBuffer[File.fpBufferPosition];
552
 
        File.fpBufferPosition++;
553
 
        
554
 
        return c;
555
 
    }
556
 
}
557
 
 
558
 
/* Replaces ungetc() for file. In case of buffer we'll perform the same action:
559
 
 * fpBufferPosition-- and write of the param char into the buf.
560
 
 */
561
 
static int pushBackChar (int c) 
562
 
{
563
 
    if (File.fp != NULL) {
564
 
        return ungetc (c, File.fp);
565
 
    }
566
 
    else {
567
 
        File.fpBufferPosition--;
568
 
        if (File.fpBufferPosition < 0)
569
 
            return EOF;
570
 
        File.fpBuffer[File.fpBufferPosition] = c;
571
 
        return File.fpBuffer[File.fpBufferPosition];
572
 
    }
573
 
}
574
 
 
575
 
 
576
 
/* replacement for fsetpos, applied to a buffer */
577
 
extern void setBufPos (int new_position) 
578
 
{
579
 
    File.fpBufferPosition = new_position;
580
 
}
581
 
 
582
 
/* replacement for fgetpos, applied to a buffer */
583
 
extern int getBufPos (void) 
584
 
{
585
 
    return File.fpBufferPosition;
586
 
}
587
 
 
588
 
extern boolean useFile (void)
589
 
{
590
 
    if (File.fp != NULL)
591
 
        return TRUE;
592
 
    else
593
 
        return FALSE;
594
 
}
595
520
 
596
521
/*
597
522
 *   Source file line reading with automatic buffer sizing
598
 
 *   Does not perform file/buffer checks. Only file is supported.
599
523
 */
600
 
extern char *readLine (vString *const vLine, FILE *const fp)
 
524
extern char *readLine (vString *const vLine, MIO *const mio)
601
525
{
602
526
    char *result = NULL;
603
527
 
604
528
    vStringClear (vLine);
605
 
    if (fp == NULL)             /* to free memory allocated to buffer */
606
 
        error (FATAL, "NULL file pointer");
 
529
    if (mio == NULL)            /* to free memory allocated to buffer */
 
530
        error (FATAL, "NULL MIO pointer");
607
531
    else
608
532
    {
609
533
        boolean reReadLine;
616
540
        do
617
541
        {
618
542
            char *const pLastChar = vStringValue (vLine) + vStringSize (vLine) -2;
619
 
            fpos_t startOfLine;
 
543
            MIOPos startOfLine;
620
544
 
621
 
            fgetpos (fp, &startOfLine);
 
545
            mio_getpos (mio, &startOfLine);
622
546
            reReadLine = FALSE;
623
547
            *pLastChar = '\0';
624
 
            result = fgets (vStringValue (vLine), (int) vStringSize (vLine), fp);
 
548
            result = mio_gets (mio, vStringValue (vLine), (int) vStringSize (vLine));
625
549
            if (result == NULL)
626
550
            {
627
 
                if (! feof (fp))
 
551
                if (! mio_eof (mio))
628
552
                    error (FATAL | PERROR, "Failure on attempt to read file");
629
553
            }
630
554
            else if (*pLastChar != '\0'  &&
633
557
                /*  buffer overflow */
634
558
                reReadLine = vStringAutoResize (vLine);
635
559
                if (reReadLine)
636
 
                    fsetpos (fp, &startOfLine);
 
560
                    mio_setpos (mio, &startOfLine);
637
561
                else
638
562
                    error (FATAL | PERROR, "input line too big; out of memory");
639
563
            }
660
584
/*  Places into the line buffer the contents of the line referenced by
661
585
 *  "location".
662
586
 */
663
 
extern char *readSourceLine (vString *const vLine, fpos_t location,
 
587
extern char *readSourceLine (vString *const vLine, MIOPos location,
664
588
                             long *const pSeekValue)
665
589
{
666
 
    fpos_t orignalPosition;
 
590
    MIOPos orignalPosition;
667
591
    char *result;
668
592
 
669
 
    fgetpos (File.fp, &orignalPosition);
670
 
    fsetpos (File.fp, &location);
 
593
    mio_getpos (File.mio, &orignalPosition);
 
594
    mio_setpos (File.mio, &location);
671
595
    if (pSeekValue != NULL)
672
 
        *pSeekValue = ftell (File.fp);
673
 
    result = readLine (vLine, File.fp);
 
596
        *pSeekValue = mio_tell (File.mio);
 
597
    result = readLine (vLine, File.mio);
674
598
    if (result == NULL)
675
599
        error (FATAL, "Unexpected end of file: %s", vStringValue (File.name));
676
 
    fsetpos (File.fp, &orignalPosition);
 
600
    mio_setpos (File.mio, &orignalPosition);
677
601
 
678
602
    return result;
679
603
}