char *slashsplit(char *line) { char *pch, *linedup; char *fname = ""; // we need to create a copy of input string because strtok() changes string // while splitting. Need to call free() when linedup is not needed. linedup = strduplicate(line); pch = strtok(linedup, "/"); while (pch != NULL) { fname = pch; pch = strtok(NULL, "/"); } // We need to get a copy of a filename because fname is a pointer to the // start of filename in linedup string which will be free'd. We need to // call free() when return value of func will not be needed. char *retval = strduplicate(fname); free(linedup); return retval; }
char *strappend(char *dest, const char *src) { if (src == NULL) return dest; else if (dest == NULL) { return strduplicate(src); } else { char *result = malloc(strlen(dest)+strlen(src)+1); strcpy(result,dest); // free(dest); /* this should work */ return strcat(result,src); } }
/* * process animations */ int parse_animation() { int numbers[1000]; int numbers_max = 0; char *overlay_code = NULL; token = yylex(); if (token == OVERLAY_CODE) { overlay_code = strduplicate(string); token = yylex(); } while (token == NUMBER) { numbers[numbers_max++] = int_val; token = yylex(); if (token == COMMA) token = yylex(); } if (token == SQUARE_O_BRACE) { char *properties; if ((properties = parse_properties()) != NULL) { if ((token = yylex()) == VALUE) { if (overlay_code != NULL) fprintf(ofile,"\\only%s{\\begin{figure}\n",overlay_code); else fprintf(ofile,"\\begin{figure}\n"); for (int i=0;i<numbers_max;i++) { fprintf(ofile,"\\includegraphics<%d>[%s,page=%d]{%s}\n", i+1, properties, numbers[i], string); } if (overlay_code != NULL) fprintf(ofile,"\\end{figure}}\n"); else fprintf(ofile,"\\end{figure}\n"); free(properties); token = yylex(); return 1; } else { free(properties); fprintf(stderr, "[%d] Figure mode: Missing image, found %s instead\n", yylineno, get_token_name(token)); return 0; } } else { return 0; } } else { fprintf(stderr, "[%d] Figure mode: Missing [, found %s instead\n", yylineno, get_token_name(token)); return 0; } }
/* * process figures */ int parse_figure_inner(int index) { char *overlay_code; token = yylex(); if (token == OVERLAY_CODE) { overlay_code = strduplicate(string); token = yylex(); } else overlay_code = strduplicate(""); if (token == SQUARE_O_BRACE) { char *properties; if ((properties = parse_properties()) != NULL) { if ((token = yylex()) == VALUE) { fprintf(ofile,"\\includegraphics%s[%s]{%s}\n", overlay_code, properties, string); free(overlay_code); free(properties); token = yylex(); if (token == IMAGE) // is an image group parse_figure_inner(index+1); return 1; } else { free(properties); free(overlay_code); fprintf(stderr, "[%d] Figure mode: Missing image, found %s instead\n", yylineno, get_token_name(token)); return 0; } } else { free(overlay_code); return 0; } } else { free(overlay_code); fprintf(stderr, "[%d] Figure mode: Missing [, found %s instead\n", yylineno, get_token_name(token)); return 0; } }
int parse_listing() { int result = 0; if ((token = yylex()) == ID) { char* language = strduplicate(string); char* highlighting; char* caption; if ((token = yylex()) == LST_FRAME) { /* highlight rows in frame */ highlighting = parse_highlighting(); token = yylex(); } else highlighting = strduplicate(""); switch (token) { case LST_SEPARATOR: caption = strduplicate(parse_label()); result = parse_listing_text(language,caption,highlighting); free(caption); break; case NEWLINE: result = parse_listing_text(language,NULL,highlighting); break; default: fprintf(stderr, "[%d] Listing mode: Separator '-' expected, found %s \"%s\" instead\n", yylineno, get_token_name(token), yytext); result = 0; break; } free(highlighting); free(language); } else { fprintf(stderr, "[%d] Listing mode: Language name expected, but %s \"%s\"\n", yylineno, get_token_name(token), yytext); result = 0; } return result; }
char* parse_highlighting() { char* highlighting = strduplicate("linebackgroundcolor={"); while ((token = yylex()) != SQUARE_C_BRACE) { if (token == NUMBER) { int number = int_val; #define MAX_NUM_LENGTH 4 char* lines = parse_hl_lines(); if (lines != NULL) { char *frame = malloc(MAX_NUM_LENGTH+strlen(lines)+11+1); sprintf(frame,"\\btLstHL<%d>{%s}",number,lines); highlighting = strappend(highlighting,frame); // free(frame); /* should work */ free(lines); } } else { fprintf(stderr, "[%d] Listing mode: Number expected, but %s \"%s\" found\n", yylineno, get_token_name(token), yytext); return NULL; } } return strappend(highlighting,"},"); }
/* * process listings */ int parse_listing_text(const char* language, const char* caption, const char* highlighting) { char* buffer = strduplicate(""); while ((token = yylex()) != LST_END) { if (token == LST_LINE) { buffer = strappend(buffer,string); } else { free(buffer); fprintf(stderr, "[%d] Listing mode: End of listing or listing expected, but %s \"%s\" found\n", yylineno, get_token_name(token), yytext); return 0; } } fprintf(ofile,"\\begin{lstlisting}[%sescapechar=\\%%,",highlighting); if (caption == NULL) fprintf(ofile, "language=%s]\n", language); else fprintf(ofile, "language=%s,caption=%s]\n", language, caption); fprintf(ofile,"%s\\end{lstlisting}\n", buffer); // free(buffer); /* should work */ token = yylex(); return 1; }
matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode) { long i, max; double score; matchobj_t returnobj; // Make a copy of input string to replace all backslashes. // We need to create a copy because PyString_AsString returns // string that must not be changed. // We will free() it later char *temp_string; temp_string = strduplicate(PyString_AsString(str)); // Replace all backslashes for (i = 0; i < strlen(temp_string); i++) { if (temp_string[i] == '\\') { temp_string[i] = '/'; } } matchinfo_t m; if (strcmp(mmode, "filename-only") == 0) { // get file name by splitting string on slashes m.haystack_p = slashsplit(temp_string); m.haystack_len = strlen(m.haystack_p); } else { m.haystack_p = temp_string; m.haystack_len = PyString_Size(str); } m.needle_p = PyString_AsString(abbrev); m.needle_len = PyString_Size(abbrev); m.max_score_per_char = (1.0 / m.haystack_len + 1.0 / m.needle_len) / 2; m.dot_file = 0; // calculate score score = 1.0; // special case for zero-length search string if (m.needle_len == 0) { // filter out dot files for (i = 0; i < m.haystack_len; i++) { char c = m.haystack_p[i]; if (c == '.' && (i == 0 || m.haystack_p[i - 1] == '/')) { score = 0.0; break; } } } else if (m.haystack_len > 0) { // normal case // prepare for memoization double memo[m.haystack_len * m.needle_len]; for (i = 0, max = m.haystack_len * m.needle_len; i < max; i++) memo[i] = DBL_MAX; m.memo = memo; score = ctrlp_recursive_match(&m, 0, 0, 0, 0.0); } // need to free memory because strdump() function in slashsplit() uses // malloc to allocate memory, otherwise memory will leak if (strcmp(mmode, "filename-only") == 0) { free(m.haystack_p); } // Free memory after strdup() free(temp_string); returnobj.str = str; returnobj.score = score; return returnobj; }