# Supporting Additional File Formats

This section describes how to create additional readers for unsupported file formats. If you are not already familiar with data source concepts, please read Data Source Concepts

## Creating Datasource Readers

If you wish to use a file format other than those currently supported, you might choose to write custom datasource readers.

All &kst; datasource readers are regular &kde; plugins. Like all &kde; plugins, each datasource reader must have a shared object file and a `*.desktop` file. Before writing the reader, the library name of the plugin must be decided. This name must be a valid variable name in C, as it will be used in the function names of the shared object. For example, the library name of the reader for ASCII files is ascii.

&kde; plugins are not the same as the &kst; plugins used to manipulate data. All references to plugins in this section are to &kde; plugins.

### The Shared Object

A datasource reader should be a subclass of the abstract `KstDataSource` class. Ensure that you include the header file for `KstDataSource` in the source code for your datasource reader:

    #include <kstdatasource.h>

There are certain requirements that a datasource reader must meet. One requirement concerns the presence of exported C functions. Other requirements are consequences of the fact that datasource readers inherit from `KstDataSource`. Both sets of requirements, along with suggestions and general explanations, are provided in the following sections.

#### Exported C Functions

The exported C functions are listed below. In the following examples, all instances of `<libname>` should be replaced with the actual library name of the plugin.

`KstDataSource *create_<libname>(const QString& `filename`, const QString& `type`)`

- This function should create a new datasource reader of type `<libname>`, where `<libname>` is the library name of the plugin. A pointer of type `KstDataSource` to the new reader should be returned.

`bool understands_<libname>(const QString& `filename`)`

- This function should return true if the file specified by `filename` is of a valid type supported by this reader, and false otherwise. The function should check the contents of the file for validity, and not rely on any filename extensions.

`QStringList provides_<libname>()`

- This function should return a `QStringList` of the file types supported by this reader. The strings returned are arbitrary, but should be descriptive of and appropriate for the actual file types.

#### Protected Member Variables

`KstDataSource` contains various protected member variables that the custom datasource reader can use. These variables are described below.

`bool _valid`

- This variable should be `true` if the custom datasource reader is valid. Most likely the reader will be valid, unless there is an error condition (such as the data file being unreadable by the reader). This variable is used by the `isValid()` function of `KstDataSource`, which is usually not reimplemented in subclasses.

`QStringList _fieldList`

- This variable should hold a list of the field names in the data source.

`QString _filename`

- This variable should hold the name of the data file this datasource reader is associated with.

`QString _source`

- This variable should hold the type name of the source.

#### Virtual Functions

The `KstDataSource` class contains many virtual functions that should be redefined in the custom datasource reader. These functions are in the template files `template.h` and `template.cpp`, listed in the Example Templates section. Descriptions of the functions follow.

`TemplateSource(const QString&amp; `filename`, const QString&amp; `type`)`

- The constructor for the datasource reader. `filename` is the name of the file to read data from, and `type` is the type of data the file contains. This constructor should most likely invoke the `KstDataSource` constructor in the constructor initializer list, and probably call the `update` function listed below to initialize member variables. In particular, the `bool _valid` variable should be set appropriately.

`virtual ~TemplateSource()`

- The destructor for the datasource reader. Any dynamically allocated memory should be freed.

`virtual KstObject::UpdateType update(`int = -1`)`

- This function should read any new data entered in the data file since the last time `update` was called, and update the member variables appropriately. The function should return `KstObject::UPDATE` if the file contained changes, or `KstObject::NO_CHANGE` otherwise.

`virtual int readField(double *`v`, const QString &`field`, int `s`, int `n`)`

- This function should read `n` frames of data, starting at frame `s` from the field specified by the field name `field`, and return the contents in the array `v`. If `n` is less than 0, the function should instead read 1 sample from the start of frame `s`. The number of samples that were read should be returned.

`virtual bool isValidField(const QString &`field`) const`

- This function should return true of the field specified by the field name `field` is a valid field in the current data source, or false if the field is not a valid field.

`virtual int samplesPerFrame(const QString& `field`)`

- This function should return the ratio of samples per frame for the field specified by the field name `field`. For data sources that do not make use of this concept, the number of samples per frame can be set to `1`.

`virtual int frameCount() const`

- This function should return the total number of frames in the data source, as of the last time `update` was called.

`virtual QString fileType() const ```

- This function should return the file type of the data file currently being used, usually the same as the `type` parameter that was passed to the constructor. Alternatively, it can contain an error message (to indicate, for example, that the file is not valid).

`virtual void save(QTextStream &`ts`)`

- This function should save the file description information to `ts`. In most cases the implementation provided by `KstDataSource` should be sufficient.

#### Example Templates

In general, the following two template files can be used to create new shared object files. Simply modify the function bodies as appropriate for your particular data source.

    /***************************************************************************
                      template.h  -  data source plugin template
                                 -------------------
        begin                : Fri Oct 17 2003
        copyright            : (C) 2003 The University of Toronto
        email                :
     ***************************************************************************/

    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/

    #ifndef TEMPLATE_H
    #define TEMPLATE_H

    #include &lt;kstdatasource.h&gt;

    class TemplateSource : public KstDataSource {
    public:
      TemplateSource(const QString& filename, const QString& type);

      virtual ~TemplateSource();

      virtual KstObject::UpdateType update(int = -1);

      virtual int readField(double *v, const QString &field, int s, int n);

      virtual bool isValidField(const QString &field) const;

      virtual int samplesPerFrame(const QString &field);

      virtual int frameCount() const;

      virtual QString fileType() const;

      virtual void save(QTextStream &ts);
    };

    #endif

    /***************************************************************************
                        template.cpp  -  data source template
                                 -------------------
        begin                : Fri Oct 17 2003
        copyright            : (C) 2003 The University of Toronto
        email                :
     ***************************************************************************/

    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/

    #include "template.h"

    TemplateSource::TemplateSource(const QString& filename, const QString& type)
    : KstDataSource(filename, type) {
    }

    TemplateSource::~TemplateSource() {
    }

    KstObject::UpdateType TemplateSource::update(int u) {
      Q_UNUSED(u)
      return KstObject::NO_CHANGE;
    }

    int TemplateSource::readField(double *v, const QString& field, int s, int n) {
      Q_UNUSED(v)
      Q_UNUSED(field)
      Q_UNUSED(s)
      Q_UNUSED(n)
      return -1;
    }

    bool TemplateSource::isValidField(const QString& field) const {
      Q_UNUSED(field)
      return false;
    }

    int TemplateSource::samplesPerFrame(const QString &field) {
      Q_UNUSED(field)
      return 0;
    }

    int TemplateSource::frameCount() const {
      return 0;
    }

    QString TemplateSource::fileType() const {
      return QString::null;
    }

    void TemplateSource::save(QTextStream &ts) {
      KstDataSource::save(ts);
    }

    extern "C" {
    KstDataSource *create_template(const QString& filename, const QString& type) {
      return new TemplateSource(filename, type);
    }

    QStringList provides_template() {
      QStringList rc;
      // create the stringlist
      return rc;
    }

    bool understands_template(const QString& filename) {
      // determine if it is an X file
      Q_UNUSED(filename)
      return false;
    }

    }

### The `.desktop` File

The following is an example of a `.desktop` file for the template plugin:

    [Desktop Entry]
    Encoding=UTF-8
    Type=Service
    ServiceTypes=Kst Data Source
    X-KDE-ModuleType=Plugin
    X-Kst-Plugin-Library=template
    X-Kst-Plugin-Author=The University of Toronto
    Name=File Reader Template
    Comment=Long description of the file reader template.

You should add translations in additional languages for the Name and Comment fields by adding additional Name and Comment fields with `[xx]` appended to the end of the field names, where `xx` is the two-letter language code. For example, to add Spanish translations, the following lines would need to be added:

    Name[es]=Plantilla de lectura de ficheros
    Comment[es]=Plantilla de código para hacer un complemento de lectura de ficheros.

The field `X-Kst-Plugin-Library` should be exactly the same as the decided library name for the plugin.

### Compiling and Copying

To compile and install the new custom datasource reader, create a new directory under `kst/datasources` of the source package. Place the source files for the object, along with the `.desktop` file in the new directory. Then, edit `kst/datasources/Makefile.am` so that `SUBDIRS` contains the name of the new subdirectory. For example, if the new subdirectory is called `template`, `SUBDIRS` might be changed to the following:

    SUBDIRS=ascii dirfile frame indirect template $(PIOLIB_SUBDIR) $(FITSIO_SUBDIR)

After the required files are in the newly created subdirectory, a `Makefile.am` needs to be created in there as well. Use the following sample as a template, replacing all instances of template with your custom library name in `kde_module_LTLIBRARIES`, `kstdata_template_la_SOURCES` and `services_DATA`.

    INCLUDES=-I$(srcdir)/../.. $(all_includes)

    kde_module_LTLIBRARIES=kstdata_template.la

    kstdata_template_la_LDFLAGS=$(all_libraries) -module -avoid-version
    kstdata_template_la_SOURCES=template.cpp

    METASOURCES=AUTO

    services_DATA=kstdata_template.desktop
    servicesdir=$(kde_servicesdir)/kst

Once this is done, you can compile and re-install &kst; from the modified source package, or alternatively, only install the new libraries, like follows (using the template subdirectory as an example). First change to the root directory of the &kst; source package. Then,

    ./configure --prefix=`kde-config --prefix`
    cd ./kst/datasources/template
    make
    make install

Restart &kst; and the new datasource reader should be loaded.