/* JFlexiPix - Java implementation of FlexiPix output library
 *
 * Copyright 2010-2011 Stefan Schuermans <stefan schuermans info>
 *
 * 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, version 3 of the License.
 *
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.blinkenarea.JFlexiPix;

import java.util.regex.*;

/// parser for a pixel ("x,y") - also abused for similar things
class PixelParser
{
  /**
   * @brief parse pixel - or similar thing
   * @param[in] text pixel coordinates in text form ("x,y")
   * @return pixel or null
   */
  static Pixel parsePixel(String text)
  {
    Pattern pattern;
    Matcher matcher;
    String strX, strY;
    Pixel pixel;

    // split text at comma
    pattern = Pattern.compile("^([0-9]+),([0-9]+)$");
    matcher = pattern.matcher(text);
    if (!matcher.find())
      return null;
    strX = matcher.group(1);
    strY = matcher.group(2);

    // parse numbers
    pixel = new Pixel();
    try {
      pixel.m_x = Integer.parseInt(strX);
      pixel.m_y = Integer.parseInt(strY);
    } catch (NumberFormatException e) {
      return null;
    }

    return pixel;
  }
}

