1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Arduino > String> csvパーサ実装 (extractCsvRow)

Last updated at Posted at 2016-02-20
動作確認
ESP-WROOM-01
Arduino 1.6.6

AAA,BBB,CCCのような文字列からそれぞれの項目を取りたいので作った。

参考 http://stackoverflow.com/questions/11068450/arduino-c-language-parsing-string-with-delimiter-input-through-serial-interfa

関数名はUnity/C#で作った自作関数と合わせた。
http://qiita.com/7of9/items/f6ff497333bf558a5951

code

void Test_extractCsvRow()
{
  String csvline = "AAA,BBB,CCC";
  String tmp;
  for(int idx = 0; idx < 3; idx++) {
    tmp = extractCsvRow(csvline, idx);
    Serial.println(tmp);    
  }
}

String extractCsvRow(String srcline, int getIdx)
{
    int commaPos = -1; // to start from 0 by (commadPos + 1), this requires -1 here
    int posIdx = 0;
    int nextOfComma;

    while(1) {
      nextOfComma = commaPos + 1;
      commaPos = srcline.indexOf(',', nextOfComma);
      if (commaPos < 0) {
        break;
      }
      if (posIdx == getIdx) {
        break; 
      }
      posIdx++;
    }
    commaPos = srcline.indexOf(',', nextOfComma);
    if (commaPos < 0) {
      return srcline.substring(nextOfComma);
    } else {
      return srcline.substring(nextOfComma, commaPos);
    }
}

Test_extractCsvRow()を実行すると以下のようになる。
一応動いているようだ。

AAA
BBB
CCC

こういう関数はライブラリがもしかしたらあり、それを使うのが楽だったかもしれない。
Arduino StringのAPI勉強として実装した。

v0.2

項目数が少ない時の処理が間違っていた。

csvline = "AAA"
という文字列に対して
extractCsvRow(csvline, 1);も
extractCsvRow(csvline, 2);も
"AAA"を返してしまっていた。

以下に修正した。

// String library

/*
 * v0.2 2016 Feb 20
 *  - extractCsvRow: fix bug > "AAA" return "AAA","AAA","AAA"
 * v0.1 2016 Feb 20
 *  - add Test_extractCsvRow()
 *  - add extractCsvRow()
 */

//-------------------------------------------------------------------------

void Test_extractCsvRow()
{
  // test1 >> full
  String csvline = "AAA,BBB,CCC";
  String tmp;
  for(int idx = 0; idx < 3; idx++) {
    tmp = extractCsvRow(csvline, idx);
    Serial.println(tmp);    
  }

  // test2 >> only 1st
  csvline = "AAA";
  for(int idx = 0; idx < 3; idx++) {
    tmp = extractCsvRow(csvline, idx);
    if (tmp.length() == 0) {
      continue;
    }
    Serial.println(tmp);    
  }
}

String extractCsvRow(String srcline, int getIdx)
{
    int commaPos = -1; // to start from 0 by (commadPos + 1), this requires -1 here
    int posIdx = 0;
    int nextOfComma;

    while(1) {
      nextOfComma = commaPos + 1;
      commaPos = srcline.indexOf(',', nextOfComma);
      if (commaPos < 0) {
        break;
      }
      if (posIdx == getIdx) {
        break; 
      }
      posIdx++;
    }

    if (posIdx != getIdx) {
      return "";
    }
    
    commaPos = srcline.indexOf(',', nextOfComma);
    if (commaPos < 0) {
      return srcline.substring(nextOfComma);
    } else {
      return srcline.substring(nextOfComma, commaPos);
    }
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?