1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.jnigen.parsing; 18 19 import java.util.ArrayList; 20 21 public class JniHeaderCMethodParser implements CMethodParser { 22 private static final String C_METHOD_MARKER = "JNIEXPORT"; 23 parse(String headerFile)24 public CMethodParserResult parse (String headerFile) { 25 ArrayList<CMethod> methods = new ArrayList<CMethod>(); 26 27 int index = headerFile.indexOf(C_METHOD_MARKER); 28 if (index == -1) return null; 29 while (index >= 0) { 30 CMethod method = parseCMethod(headerFile, index); 31 if (method == null) throw new RuntimeException("Couldn't parse method"); 32 methods.add(method); 33 index = headerFile.indexOf(C_METHOD_MARKER, method.endIndex); 34 } 35 return new CMethodParserResult(methods); 36 } 37 parseCMethod(String headerFile, int start)38 private CMethod parseCMethod (String headerFile, int start) { 39 int headEnd = headerFile.indexOf('(', start); 40 String head = headerFile.substring(start, headEnd).trim(); 41 42 String returnType = head.split(" ")[1].trim(); 43 44 int argsStart = headEnd + 1; 45 int argsEnd = headerFile.indexOf(')', argsStart); 46 String[] args = headerFile.substring(argsStart, argsEnd).split(","); 47 48 return new CMethod(returnType, head, args, start, argsEnd + 1); 49 } 50 } 51