~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_file.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_file.c 782 2007-03-21 05:01:11Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_file.c $
 
3
 *
 
4
 * plugin to perform simple file operations
 
5
 *
 
6
 * Copyright (C) 2006 Chris Maj <cmaj@freedomcorpse.com>
 
7
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
11
 * LCD4Linux is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2, or (at your option)
 
14
 * any later version.
 
15
 *
 
16
 * LCD4Linux is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * exported functions:
 
29
 *
 
30
 * int plugin_init_file (void)
 
31
 *  adds various functions
 
32
 *
 
33
 */
 
34
 
 
35
 
 
36
/* define the include files you need */
 
37
#include "config.h"
 
38
 
 
39
#include <stdio.h>
 
40
#include <string.h>
 
41
 
 
42
/* these should always be included */
 
43
#include "debug.h"
 
44
#include "plugin.h"
 
45
 
 
46
#ifdef WITH_DMALLOC
 
47
#include <dmalloc.h>
 
48
#endif
 
49
 
 
50
 
 
51
/* function 'readline' */
 
52
/* takes two arguments, file name and line number */
 
53
/* returns text of that line */
 
54
 
 
55
static void my_readline(RESULT * result, RESULT * arg1, RESULT * arg2)
 
56
{
 
57
    char value[80], val2[80];
 
58
    FILE *fp;
 
59
    int reqline, i, size;
 
60
 
 
61
    reqline = R2N(arg2);
 
62
    fp = fopen(R2S(arg1), "r");
 
63
    if (!fp) {
 
64
        error("readline couldn't open file '%s'", R2S(arg1));
 
65
        value[0] = '\0';
 
66
    } else {
 
67
        i = 0;
 
68
        while (!feof(fp) && i++ < reqline) {
 
69
            fgets(val2, sizeof(val2), fp);
 
70
            size = strcspn(val2, "\r\n");
 
71
            strncpy(value, val2, size);
 
72
            value[size] = '\0';
 
73
            /* more than 80 chars, chew up rest of line */
 
74
            while (!feof(fp) && strchr(val2, '\n') == NULL) {
 
75
                fgets(val2, sizeof(val2), fp);
 
76
            }
 
77
        }
 
78
        fclose(fp);
 
79
        if (i <= reqline) {
 
80
            error("readline requested line %d but file only had %d lines", reqline, i - 1);
 
81
            value[0] = '\0';
 
82
        }
 
83
    }
 
84
 
 
85
    /* store result */
 
86
    SetResult(&result, R_STRING, &value);
 
87
}
 
88
 
 
89
/* plugin initialization */
 
90
/* MUST NOT be declared 'static'! */
 
91
int plugin_init_file(void)
 
92
{
 
93
 
 
94
    /* register all our cool functions */
 
95
    /* the second parameter is the number of arguments */
 
96
    /* -1 stands for variable argument list */
 
97
    AddFunction("file::readline", 2, my_readline);
 
98
 
 
99
    return 0;
 
100
}
 
101
 
 
102
void plugin_exit_file(void)
 
103
{
 
104
    /* free any allocated memory */
 
105
    /* close filedescriptors */
 
106
}