upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / FunctionPrototypeSummary.java
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
7  *
8  * Contributors:
9  * QNX Software Systems - Initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.ui;
12
13 /**
14  * This class is a helper class which takes care of implementing some of the 
15  * function prototype parsing and stripping.
16  */
17 public class FunctionPrototypeSummary implements IFunctionSummary.IFunctionPrototypeSummary {
18         String fname;
19         String freturn;
20         String farguments;
21                 
22         /**
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() 
30          */
31         public FunctionPrototypeSummary(String proto) {
32                 int leftbracket = proto.indexOf('(');
33                 int rightbracket = proto.lastIndexOf(')');
34                 
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$
41                         } else {
42                                 proto = proto + "()"; //$NON-NLS-1$
43                         }
44                 
45                         leftbracket = proto.indexOf('(');
46                         rightbracket = proto.lastIndexOf(')');
47                 } 
48                 
49                 farguments = proto.substring(leftbracket + 1, rightbracket);
50                         
51                 // fix for bug #44359
52                 if(farguments.equals("void")) //$NON-NLS-1$
53                         farguments = ""; //$NON-NLS-1$
54                 
55                 int nameend = leftbracket - 1;
56                 while(proto.charAt(nameend) == ' ') {
57                         nameend--;
58                 }
59
60                 int namestart = nameend;
61                 while(namestart > 0 && proto.charAt(namestart) != ' ') {
62                         namestart--;
63                 }
64
65                 fname = proto.substring(namestart, nameend + 1).trim();
66                         
67                 if(namestart == 0) {
68                         //Constructors are like this, don't stick a type on them.
69                         freturn = ""; //$NON-NLS-1$
70                 } else {
71                         freturn = proto.substring(0, namestart).trim();
72                 }
73         }
74
75         public String getName() {
76                 return fname;
77         }
78
79         public String getReturnType() {
80                 return freturn;
81         }
82                 
83         public String getArguments() {
84                 return farguments;
85         }
86                 
87         public String getPrototypeString(boolean namefirst) {
88                 return getPrototypeString(namefirst, true);
89         }
90         
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$
96                 }
97                 buffer.append(getName());
98                 buffer.append("("); //$NON-NLS-1$
99                 if(getArguments() != null) {
100                         buffer.append(getArguments());
101                 }
102                 buffer.append(")"); //$NON-NLS-1$
103                 if((namefirst) && (appendReturnType) && getReturnType().length() > 0 ) {
104                         buffer.append(" "); //$NON-NLS-1$
105                         buffer.append(getReturnType());
106                 }
107                 buffer.append(";"); //$NON-NLS-1$
108                 return buffer.toString();
109         }
110 }