void FunctionDeclaration::doPrint(PrintContext &context) const { printComment(context); printSignature(context); context.out() << ';'; }
int main (void) { bool inString = false, inComment = false, singleLineComment = false; char * comments[MAXCOMMENTS]; int numberOfComments = 0; // temp temporarily holds a comment before its pointer is stored in comments[] char temp[MAXCOMMENTSIZE]; // keeps track of the last character stored in temp int tempIndex = 0; // input is the current character char input, lastChar; // to loop through and print comments later on int i = 0; while ((input = getchar()) != EOF) { if (inComment) { temp[tempIndex++] = input; // if we are at the end of a line and this is a single-line comment // OR // the last character read was an asterisk and input is a slash if ((input == '\n' && singleLineComment) || (lastChar == '*' && input == '/')) { // add the null terminator to our string in temp // very important since, when printing comments, this is how we'll detect // the end of a comment temp[tempIndex] = '\0'; // we are no longer in any type of comment, so set these booleans to false singleLineComment = inComment = false; // dynamically allocate memory equal to the size of our string held in temp // and assign it to the next open index in comments, an array of pointers comments[numberOfComments] = malloc(sizeof(char) * tempIndex); strcpy(comments[numberOfComments], temp); // was going to in-line this above, I figured that was bad practice ++numberOfComments; // temp is now "empty" (not really, but we're free to write over it) tempIndex = 0; } } else { // if we are not inside a string // AND // the last character was a slash // AND // the current character is a slash OR an asterisk if (!inString && lastChar == '/' && (input == '/' || input == '*')) { // if the current character is a slash, this must be a single-line comment singleLineComment = input == '/'; // duh inComment = true; // set temp[0] to a slash (all comments start with slash) temp[0] = '/'; // set temp[1] to input (an asterisk or a slash) temp[1] = input; // temp already has two characters; set tempIndex to 2 tempIndex = 2; // if the most recent character is a quotation mark } else if (input == '\"' && lastChar != '\\') { // we are now in the opposite string "state" inString = !inString; } } // set the most recently read character to lastChar, so that it can be used // in the next iteration of the loop lastChar = input; } printf("PRINTING COMMENTS:\n"); for (i = 0; i < numberOfComments; i++) { printComment(comments[i], i + 1); // no memory leaks! free(comments[i]); } return 0; }
int main(int argc, char *argv[]) { char *str= NULL; char *fstr = NULL; int pos; //function的位置 int pos_bracket_left; //左括号位置 int pos_bracket_right; //右括号位置 char *functionName; //函数名 int functionLen; char *p; struct StringArr *head; //头节点 struct StringArr *lastnode; //尾节点 int i=0, paramNum=0, start=0, end=0; char c; char *paramName; if (argc==1) { str = " public function getTopList (int $page,int $pageSize=1, array $filter=array(), $other) { "; } else { str = argv[1]; } fstr = (char *)malloc((strlen(str)+1)*sizeof(char)); strcpy(fstr, str); //function的位置 pos = strpos(fstr, "function", 0); //左括号位置 pos_bracket_left = strpos(fstr, "(", pos); //获得函数名 functionName = (char *)malloc((pos_bracket_left-pos+1)*sizeof(char)); functionLen = strlen("function "); strncpy(functionName, fstr+pos+functionLen, pos_bracket_left-functionLen-pos); *(functionName + (pos_bracket_left-functionLen-pos)) = '\0'; //右括号位置 p = fstr+strlen(fstr)-1; pos_bracket_right = strlen(fstr); while (p>fstr && (*p!=')')) { pos_bracket_right--; p--; } //字符串链条 head = newStringArr(); //头节点 lastnode = head; //尾节点 //拆分提取每个参数名 for (i=pos_bracket_left; i<pos_bracket_right; i++) { c = *(fstr+i); if (c=='$') { start = i+1; } if (c=='=' || c==' ' || c==',' || c==')') { if (start>0) { end = i; //paramPrint(fstr, start, end); paramName = (char *)malloc(end-start+1); strncpy(paramName, fstr+start, end-start); *(paramName+(end-start)) = '\0'; lastnode = StringArrAdd(lastnode, paramName); start = end = 0; paramNum++; continue; } } } //StringArrPrint(head); //打印注释 printComment(head, functionName); //释放内存 free(fstr); free(functionName); StringArrFree(head); //StringArr测试 //void testStringArr(); //testStringArr(); return 0; }