# qtcsv
[![Build Status](https://travis-ci.org/iamantony/qtcsv.svg?branch=master)](https://travis-ci.org/iamantony/qtcsv) [![Build status](https://ci.appveyor.com/api/projects/status/7uv7ghs9uexf08bv/branch/master?svg=true)](https://ci.appveyor.com/project/iamantony/qtcsv/branch/master)
Small easy-to-use library for reading and writing [csv-files][csvwiki]
in Qt.
Tested on:
- Ubuntu 14.04 with gcc 4.8.4, Qt 4.8 and higher
- Windows with MinGW, Qt 5.3 and higher
- OS X with clang, Qt 4.8, 5.5, 5.7 and higher
## Table of contents
* [1. Quick Example](#1-quick-example)
* [2. Usage](#2-usage)
* [2.1 Containers](#21-containers)
* [2.1.1 AbstractData](#211-abstractdata)
* [2.1.2 StringData](#212-stringdata)
* [2.1.3 VariantData](#213-variantdata)
* [2.2 Reader](#22-reader)
* [2.2.1 Reader functions](#221-reader-functions)
* [2.2.2 AbstractProcessor](#222-abstractprocessor)
* [2.3 Writer](#23-writer)
* [3. Requirements](#3-requirements)
* [4. Build](#4-build)
* [4.1 Building on Linux, OS X](#41-building-on-linux-os-x)
* [4.1.1 Using qmake](#411-using-qmake)
* [4.1.2 Using cmake](#412-using-cmake)
* [4.2 Building on Windows](#42-building-on-windows)
* [4.2.1 Prebuild step on Windows](#421-prebuild-step-on-windows)
* [4.2.2 Using qmake](#422-using-qmake)
* [4.2.3 Using cmake](#423-using-cmake)
* [5. Run tests](#5-run-tests)
* [5.1 Linux, OS X](#51-linux-os-x)
* [5.2 Windows](#52-windows)
* [6. Installation](#6-installation)
* [7. Examples](#7-examples)
* [8. Other](#8-other)
* [9. Creators](#9-creators)
## 1. Quick Example
```cpp
#include <QList>
#include <QStringList>
#include <QDir>
#include <QDebug>
#include "qtcsv/stringdata.h"
#include "qtcsv/reader.h"
#include "qtcsv/writer.h"
int main()
{
// prepare data that you want to save to csv-file
QStringList strList;
strList << "one" << "two" << "three";
QtCSV::StringData strData;
strData.addRow(strList);
strData.addEmptyRow();
strData << strList << "this is the last row";
// write to file
QString filePath = QDir::currentPath() + "/test.csv";
QtCSV::Writer::write(filePath, strData);
// read data from file
QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
for ( int i = 0; i < readData.size(); ++i )
{
qDebug() << readData.at(i).join(",");
}
return 0;
}
```
## 2. Usage
Library could be separated into three parts: **_Reader_**,
**_Writer_** and **_Containers_**.
### 2.1 Containers
*qtcsv* library can work with standard Qt containers like QList and
QStringList, but also with special ones.
#### 2.1.1 AbstractData
**[_AbstractData_][absdata]** is a pure abstract class that provide
interface for a container class. Here is how it looks:
```cpp
class AbstractData
{
public:
explicit AbstractData() {}
virtual ~AbstractData() {}
virtual void addEmptyRow() = 0;
virtual void addRow(const QStringList& values) = 0;
virtual void clear() = 0;
virtual bool isEmpty() const = 0;
virtual int rowCount() const = 0;
virtual QStringList rowValues(const int& row) const = 0;
};
```
As you can see, **_AbstractData_** declare virtual functions for adding new rows,
getting rows values, clearing all information and so on. Basic stuff for a
container class.
If you have said *Pure Abstract Class*, you must also say *Implementation*.
Don't worry, we have some:
#### 2.1.2 StringData
**[_StringData_][strdata]** have the same interface as **_AbstractData_**
class plus some useful functions for inserting rows, removing rows and
so on. It stores all data as strings. It is most convenient to use it
when information that you want to save in csv-file is represented as strings.
#### 2.1.3 VariantData
If you store information in different types - integers, floating point
values, strings or (almost) anything else (example: [1, 3.14, "check"]) -
and you don't want to manually transform each element to string, then you
can use **_QVariant_** magic. Wrap your data into **_QVariants_** and pass it to
**[_VariantData_][vardata]** class.
### 2.2 Reader
Use **[_Reader_][reader]** class to read csv-files / csv-data. Let's see what
functions it has.
#### 2.2.1 Reader functions
1. Read data to **_QList\<QStringList\>_**
```cpp
QList<QStringList> readToList(const QString& filePath,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec);
QList<QStringList> readToList(QIODevice& ioDevice,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec);
```
- *filePath* - string with absolute path to existent csv-file
(example: "/home/user/my-file.csv");
- *ioDevice* - IO Device that contains csv-formatted data;
- *separator* (optional) - delimiter symbol, that separate elements
in a row (by default it is comma - ",");
- *textDelimiter* (optional) - text delimiter symbol that enclose
each element in a row (by default it is double quoute - ");
- *codec* (optional) - pointer to the codec object that will be used
to read data from the file (by default it is UTF-8 codec).
As a result function will return **_QList\<QStringList\>_**
that holds content of the file / IO Device. If all went smooth,
list will not be empty and size of it will be equal to the number of rows
in csv-data source. Each **_QStringList_** will contain elements of
the corresponding row.
2. Read data to **_AbstractData_**-based container
```cpp
bool readToData(const QString& filePath,
AbstractData& data,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec);
bool readToData(QIODevice& ioDevice,
AbstractData& data,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec);
```
Second function is a little more advanced and, I hope, a little more useful.
- *filePath* - string with absolute path to existent csv-file;
- *ioDevice* - IO Device that contains csv-formatted data;
- *data* - reference to **_AbstractData_**-based class object;
- *separator* (optional) - delimiter symbol;
- *textDelimiter* (optional) - text delimiter symbol;
- *codec* (optional) - pointer to the codec object.
Function will save content of the file / IO Device in *data* object using virtual
function **_AbstractData::addRow(QStringList)_**. If you pass to the
function **_Reader::readToData()_** object of class **_StringData_** or
**_VariantData_**, elements of csv-data will be saved in them as strings.
If you are not happy with this fact, you can create your own
**_AbstractData_**-based container class and implement function
**_addRow(QStringList)_** in a way you want it.
For example, if you know, that each row of your csv-data contains 3 elements
(integer value, floating-point value and string), then in function
**_addRow(QStringList)_** you can convert first element of **_QStringList_**
to int, second - to double and save all three elements to some
internal container (or do with them whatever you want).
3. Read data and process it line-by-line by **_AbstractProcessor_**-based processor
```cpp
bool readToProcessor(const QString& filePath,
AbstractProcessor& processor,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec);
bool readToProcessor(QIODevice& ioDevice,
AbstractProcessor& processor,
const QString& separator,
const QString& textDelimiter,
QTextCodec* codec
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
qtcsv-master.zip (47个子文件)
qtcsv-master
.travis.yml 7KB
qtcsv.pri 668B
tests
teststringdata.cpp 9KB
testvariantdata.cpp 11KB
tst_testmain.cpp 471B
data
test-semicolon.csv 14B
test-field-with-dquotes.csv 58B
test-multirow-data.csv 60B
test-empty-fields-complex-separator.csv 40B
test-data-correctness.csv 302B
test-comma.csv 38B
test-field-with-cr.csv 75B
test-field-end-triple-quotes.csv 69B
test-empty-fields.csv 7B
test-field-with-crlf.csv 36B
test-field-with-crlf-long.csv 63B
test-data-text-delim-quotes.csv 74B
test.file.dots.csv 26B
test-data-text-delim-double-quotes.csv 74B
testvariantdata.h 645B
testreader.h 2KB
testreader.cpp 19KB
testwriter.h 960B
CMakeLists.txt 1KB
testwriter.cpp 14KB
teststringdata.h 620B
tests.pro 2KB
include
qtcsv
variantdata.h 3KB
stringdata.h 2KB
reader.h 4KB
qtcsv_global.h 296B
writer.h 2KB
abstractdata.h 2KB
LICENSE 1KB
qtcsv.pro 1KB
appveyor.yml 3KB
.gitignore 500B
CMakeLists.txt 3KB
README.md 17KB
sources
variantdata.cpp 8KB
contentiterator.h 2KB
symbols.h 192B
filechecker.h 1KB
stringdata.cpp 4KB
reader.cpp 22KB
writer.cpp 9KB
contentiterator.cpp 4KB
共 47 条
- 1
小林家的珂女仆
- 粉丝: 28
- 资源: 4656
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1