1 /*******************************************************************************
2 * Copyright (c) 2003, 2008 QNX Software Systems and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * QNX Software Systems - Initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.cdt.ui;
14 * This class is a helper class which takes care of implementing some of the
15 * function prototype parsing and stripping.
17 public class FunctionPrototypeSummary implements IFunctionSummary.IFunctionPrototypeSummary {
23 * Create a function prototype summary based on a prototype string.
24 * @param proto The string describing the prototype which is properly
25 * formed with following format -- returntype function(arguments)
26 * The following formats will be converted as follows:
27 * function(arguments) --> function(arguments) //constructors!
28 * returntype function --> returntype function()
29 * function --> void function()
31 public FunctionPrototypeSummary(String proto) {
32 int leftbracket = proto.indexOf('(');
33 int rightbracket = proto.lastIndexOf(')');
35 //If there are brackets missing, then assume void parameters
36 if(leftbracket == -1 || rightbracket == -1) {
37 if(leftbracket != -1) {
38 proto = proto.substring(leftbracket) + ")"; //$NON-NLS-1$
39 } else if(rightbracket != -1) {
40 proto = proto.substring(rightbracket - 1) + "()"; //$NON-NLS-1$
42 proto = proto + "()"; //$NON-NLS-1$
45 leftbracket = proto.indexOf('(');
46 rightbracket = proto.lastIndexOf(')');
49 farguments = proto.substring(leftbracket + 1, rightbracket);
52 if(farguments.equals("void")) //$NON-NLS-1$
53 farguments = ""; //$NON-NLS-1$
55 int nameend = leftbracket - 1;
56 while(proto.charAt(nameend) == ' ') {
60 int namestart = nameend;
61 while(namestart > 0 && proto.charAt(namestart) != ' ') {
65 fname = proto.substring(namestart, nameend + 1).trim();
68 //Constructors are like this, don't stick a type on them.
69 freturn = ""; //$NON-NLS-1$
71 freturn = proto.substring(0, namestart).trim();
75 public String getName() {
79 public String getReturnType() {
83 public String getArguments() {
87 public String getPrototypeString(boolean namefirst) {
88 return getPrototypeString(namefirst, true);
91 public String getPrototypeString(boolean namefirst, boolean appendReturnType) {
92 StringBuilder buffer = new StringBuilder();
93 if((!namefirst) && (appendReturnType)) {
94 buffer.append(getReturnType());
95 buffer.append(" "); //$NON-NLS-1$
97 buffer.append(getName());
98 buffer.append("("); //$NON-NLS-1$
99 if(getArguments() != null) {
100 buffer.append(getArguments());
102 buffer.append(")"); //$NON-NLS-1$
103 if((namefirst) && (appendReturnType) && getReturnType().length() > 0 ) {
104 buffer.append(" "); //$NON-NLS-1$
105 buffer.append(getReturnType());
107 buffer.append(";"); //$NON-NLS-1$
108 return buffer.toString();