static GtkWidget * create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad) { const gchar *uid = NULL, *info = NULL; gchar *buf; GtkWidget *label; if (!uid_hint) uid = _("[no user id]"); else uid = uid_hint; if (!pass_hint) info = ""; else info = pass_hint; buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n" " %.*s \n" "(%.*s)\n"), prev_bad ? _("Bad passphrase! Try again...\n\n") : "", linelen (uid), uid, linelen (info), info); label = gtk_label_new (buf); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); g_free (buf); return label; }
static GtkWidget * create_description (const gchar *desc) { const gchar *cmd = NULL, *uid = NULL, *info = NULL; gchar *buf; GtkWidget *label; cmd = desc; uid = strchr (cmd, '\n'); if (uid) { info = strchr (++uid, '\n'); if (info ) info++; } if (!uid) uid = _("[no user id]"); if (!info) info = ""; buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n" " %.*s \n" "(%.*s)\n"), !strncmp (cmd, "TRY_AGAIN", 9 ) ? _("Bad passphrase! Try again...\n\n") : "", linelen (uid), uid, linelen (info), info); label = gtk_label_new (buf); g_free (buf); return label; }
int recli_fprintf_words(void *ctx, const char *fmt, ...) { int cols = linenoiseCols(); size_t len = 0; va_list args; char *p, buffer[8192]; va_start(args, fmt); if (cols <= 0) cols = 80; vsnprintf(buffer, sizeof(buffer), fmt, args); p = buffer; while (*p) { len = linelen(p, cols - 1); if ((len > 0) && (p[len - 1] < ' ')) { recli_fprintf(ctx, "%.*s", len, p); } else { recli_fprintf(ctx, "%.*s\r\n", len, p); } p += len; while (*p == ' ') p++; } va_end(args); return len; }
void remove_comment( char ** p, unsigned int * i, int * len, unsigned int data_sz ) { if ( **p == '/' && *(*p+1) == '/' ) { *i += *len; *p += *len; } else if ( **p == '#' ) { *i += *len; *p += *len; } *i += trim_whitespace( p, *i, data_sz ); *len = linelen( *p, *i, data_sz ); }
/** * @brief Match regular expression list against given difference. * This function matches the regular expression list against the difference * (given as start line and end line). Matching the diff requires that all * lines in difference match. * @param [in] StartPos First line of the difference. * @param [in] endPos Last line of the difference. * @param [in] FileNo File to match. * return true if any of the expressions matches. */ bool DiffUtils::RegExpFilter(int StartPos, int EndPos, int FileNo) { if (m_pFilterList == NULL) { _RPTF0(_CRT_ERROR, "DiffUtils::RegExpFilter() called when " "filterlist doesn't exist (=NULL)"); return false; } bool linesMatch = true; // set to false when non-matching line is found. int line = StartPos; while (line <= EndPos && linesMatch == true) { const char *string = files[FileNo].linbuf[line]; size_t stringlen = linelen(string); if (!m_pFilterList->Match(stringlen, string, m_codepage)) { linesMatch = false; } ++line; } return linesMatch; }
int main( int argc, char ** argv ) { if ( argc != 2 ) { err( "usage shrinkMap <mapfile.map>" ); } // read input into memory FILE * fp = fopen( argv[1], "rb" ); if ( !fp ) { err( "could open input" ); } char * in_data = (char *) malloc ( 1024 * 1024 ); char * p = in_data; unsigned int n, data_sz = 0 ; do { n = fread( p, 1, 4096, fp ); if ( n <= 0 ) break; data_sz += n; p += n; } while(1); fclose( fp ); // create outfilename char oname[256]; char tmp[256]; strcpy( tmp, argv[1] ); char * f = (char *) strstr( tmp, ".map" ); if ( !f ) err( "f**k, I dont get it" ); *f = '\0'; sprintf( oname, "%s-shrunk.map", tmp ); char buf[256]; // open output fp = fopen( oname, "wb" ); if ( !fp ) { sprintf( buf, "couldn't open %s for writing", oname ); err( buf ); } unsigned int i = 0; bool w = false; while ( i < data_sz ) { if ( !white( in_data[i] ) ) { // just remove comment if ( is_comment(&in_data[i]) ) { remove_line( &in_data[i], &i, data_sz ); } else // print the line { unsigned int len = linelen( &in_data[i], i, data_sz ); strncpy( buf, &in_data[i], len ); buf[len] = '\0'; fputs( buf, fp ); i += len; } if ( i >= data_sz ) break; } // skip all white chars w = false; while ( white( in_data[i] ) ) { i++; w = true; if ( i >= data_sz ) { w = false; break; } } if ( !white(in_data[i]) && !is_comment( &in_data[i] ) && w ) fputc( ' ', fp ); } fclose(fp); free( in_data ); fprintf( stdout, "created %s\n", oname ); return 0; }
void remove_line( char * p, unsigned int * i, unsigned int data_sz ) { unsigned int len = linelen( p, *i, data_sz ); if ( *p == '/' && *(p+1) == '/' ) { *i += len; } }
/** @brief The main entry for post filtering. Performs post-filtering, by setting comment blocks to trivial @param [in] LineNumberLeft - First line number to read from left file @param [in] QtyLinesLeft - Number of lines in the block for left file @param [in] LineNumberRight - First line number to read from right file @param [in] QtyLinesRight - Number of lines in the block for right file @param [in,out] Op - This variable is set to trivial if block should be ignored. @param [in] filtercommentsset - Comment marker set used to indicate comment blocks. @param [in] FileNameExt - The file name extension. Needs to be lower case string ("cpp", "java", "c") */ static void PostFilter(int LineNumberLeft, int QtyLinesLeft, int LineNumberRight, int QtyLinesRight, OP_TYPE &Op, const FilterCommentsManager &filtercommentsmanager, const TCHAR *FileNameExt) { if (Op == OP_TRIVIAL) return; //First we need to get lowercase file name extension FilterCommentsSet filtercommentsset = filtercommentsmanager.GetSetForFileType(FileNameExt); if (filtercommentsset.StartMarker.empty() && filtercommentsset.EndMarker.empty() && filtercommentsset.InlineMarker.empty()) { return; } if (Op == OP_LEFTONLY) {//Only check left side if (PostFilter(LineNumberLeft, files[0].valid_lines, 1, QtyLinesLeft, Op, 0, filtercommentsset)) { PostFilter(LineNumberLeft, -1, -1, QtyLinesLeft, Op, 0, filtercommentsset); } if (Op != OP_TRIVIAL && !filtercommentsset.InlineMarker.empty()) { bool AllLinesAreComments = true; for(int i = LineNumberLeft;i < LineNumberLeft + QtyLinesLeft;++i) { OP_TYPE TestOp = OP_NONE; PostFilterSingleLine(files[0].linbuf[i], TestOp, filtercommentsset, QtyLinesLeft > 1); if (TestOp != OP_TRIVIAL) { AllLinesAreComments = false; break; } } if (AllLinesAreComments) Op = OP_TRIVIAL; } } else if (Op == OP_RIGHTONLY) {//Only check right side if (PostFilter(LineNumberRight, files[1].valid_lines, 1, QtyLinesRight, Op, 1, filtercommentsset)) { PostFilter(LineNumberRight, -1, -1, QtyLinesRight, Op, 1, filtercommentsset); } if (Op != OP_TRIVIAL && !filtercommentsset.InlineMarker.empty()) { bool AllLinesAreComments = true; for(int i = LineNumberRight;i < LineNumberRight + QtyLinesRight;++i) { OP_TYPE TestOp = OP_NONE; PostFilterSingleLine(files[1].linbuf[i], TestOp, filtercommentsset, QtyLinesRight > 1); if (TestOp != OP_TRIVIAL) { AllLinesAreComments = false; break; } } if (AllLinesAreComments) Op = OP_TRIVIAL; } } else { OP_TYPE LeftOp = OP_NONE; if (PostFilter(LineNumberLeft, files[0].valid_lines, 1, QtyLinesLeft, LeftOp, 0, filtercommentsset)) PostFilter(LineNumberLeft, -1, -1, QtyLinesLeft, LeftOp, 0, filtercommentsset); OP_TYPE RightOp = OP_NONE; if (PostFilter(LineNumberRight, files[1].valid_lines, 1, QtyLinesRight, RightOp, 1, filtercommentsset)) PostFilter(LineNumberRight, -1, -1, QtyLinesRight, RightOp, 1, filtercommentsset); if (LeftOp == OP_TRIVIAL && RightOp == OP_TRIVIAL) Op = OP_TRIVIAL; else if (!filtercommentsset.InlineMarker.empty() && QtyLinesLeft == 1 && QtyLinesRight == 1) { //Lets test if only a post line comment is different. const char *LineStrLeft = files[0].linbuf[LineNumberLeft]; const char *LineStrRight = files[1].linbuf[LineNumberRight]; std::string LineDataLeft(LineStrLeft, linelen(LineStrLeft)); std::string LineDataRight(LineStrRight, linelen(LineStrRight)); const char *CommentStrLeft = FindCommentMarker(LineDataLeft.c_str(), filtercommentsset.InlineMarker.c_str()); const char *CommentStrRight = FindCommentMarker(LineDataRight.c_str(), filtercommentsset.InlineMarker.c_str()); //If neither side has comment string, then lets assume significant difference, and return if (CommentStrLeft == NULL && CommentStrRight == NULL) { return; } //Do a quick test to see if both sides begin with comment character if (CommentStrLeft == LineDataLeft.c_str() && CommentStrRight == LineDataRight.c_str()) {//If both sides begin with comment character, then this is a trivial difference Op = OP_TRIVIAL; return; } //Lets remove comments, and see if lines are equal if (CommentStrLeft != NULL) LineDataLeft.erase(CommentStrLeft - LineDataLeft.c_str()); if (CommentStrRight != NULL) LineDataRight.erase(CommentStrRight - LineDataRight.c_str()); if (LineDataLeft == LineDataRight) {//If they're equal now, then only difference is comments, and that's a trivial difference Op = OP_TRIVIAL; return; } } } }
/** @brief Performs post-filtering, by setting comment blocks to trivial @param [in] StartPos - First line number to read @param [in] EndPos - The line number PASS the last line number to read @param [in] QtyLinesInBlock - Number of lines in diff block. Not needed in backward direction. @param [in] Direction - This should be 1 or -1, to indicate which direction to read (backward or forward) @param [in,out] Op - This variable is set to trivial if block should be ignored. @param [in] FileNo - Should be 0 or 1, to indicate left or right file. @param [in] filtercommentsset - Comment marker set used to indicate comment blocks. @return Always returns true in reverse direction. In forward direction, returns false if none trivial data is found within QtyLinesInBlock */ static bool PostFilter(int StartPos, int EndPos, int Direction, int QtyLinesInBlock, OP_TYPE &Op, int FileNo, const FilterCommentsSet& filtercommentsset) { const char* EolIndicators = "\r\n"; //List of characters used as EOL if (Op == OP_TRIVIAL) //If already set to trivial, then exit. return true; bool OpShouldBeTrivial = false; int QtyTrivialLines = 0; for(int i = StartPos + ((Direction == -1)?-1:0); i != EndPos;i += Direction) { if ((i - StartPos) == QtyLinesInBlock && QtyLinesInBlock == QtyTrivialLines) { OpShouldBeTrivial = true; break; } const char *LineStr = files[FileNo].linbuf[i]; std::string LineData(LineStr, linelen(LineStr)); const char * StartOfComment = FindCommentMarker(LineData.c_str(), filtercommentsset.StartMarker.c_str()); const char * EndOfComment = FindCommentMarker(LineData.c_str(), filtercommentsset.EndMarker.c_str()); const char * InLineComment = FindCommentMarker(LineData.c_str(), filtercommentsset.InlineMarker.c_str()); //The following logic determines if the entire block is a comment block, and only marks it as trivial //if all the changes are within a comment block. if (Direction == -1) { if (!StartOfComment && EndOfComment) break; if (StartOfComment && (!EndOfComment || EndOfComment < StartOfComment) && (!InLineComment || InLineComment > StartOfComment)) { OpShouldBeTrivial = true; break; } } else if (Direction == 1) { if (IsTrivialBytes(LineData.c_str(), LineData.c_str()+LineData.size(), filtercommentsset) || IsTrivialLine(LineData, StartOfComment, EndOfComment, InLineComment, filtercommentsset)) { ++QtyTrivialLines; } if (!EndOfComment && StartOfComment) { if (i == (StartPos + QtyTrivialLines) ) { if (StartOfComment == LineData.c_str()) {//If this is at the beginning of the first line, then lets continue continue; } if (IsTrivialBytes(LineData.c_str(), StartOfComment, filtercommentsset)) {//If only trivial bytes before comment marker, then continue continue; } break; } //If this is not the first line, then assume //previous lines are non-trivial, and return true. return false; } if (EndOfComment && (!StartOfComment || StartOfComment > EndOfComment) && (!InLineComment || InLineComment > EndOfComment) ) { if (!IsTrivialBytes(EndOfComment+filtercommentsset.EndMarker.size(), LineData.c_str()+LineData.size(), filtercommentsset)) { return false; } if ((i - StartPos) >= (QtyLinesInBlock-1)) { OpShouldBeTrivial = true; break; } //Lets check if the remaining lines only contain trivial data bool AllRemainingLinesContainTrivialData = true; int TrivLinePos = i+1; for(; TrivLinePos != (StartPos + QtyLinesInBlock);++TrivLinePos) { std::string LineDataTrvCk(files[FileNo].linbuf[TrivLinePos]); size_t EolPos = LineDataTrvCk.find_first_of(EolIndicators); if (EolPos != std::string::npos) { LineDataTrvCk.erase(EolPos); } if (LineDataTrvCk.size() && !IsTrivialBytes(LineDataTrvCk.c_str(), LineDataTrvCk.c_str() + LineDataTrvCk.size(), filtercommentsset)) { AllRemainingLinesContainTrivialData = false; break; } } if (AllRemainingLinesContainTrivialData) { OpShouldBeTrivial = true; break; } if (TrivLinePos != (StartPos + QtyLinesInBlock) ) { return PostFilter(TrivLinePos, EndPos, Direction, QtyLinesInBlock - (TrivLinePos - StartPos), Op, FileNo, filtercommentsset); } } } } if (OpShouldBeTrivial) { Op = OP_TRIVIAL; } return true; }
int main(int argc, char **argv) { if ( argc < 2 ) { err( "usage: gimp_palette_convert <*.map> <output>" ); } unsigned int data_sz = 0; char * data = NULL; if ( !(data=get_gimp_palette( argv[1], &data_sz)) ) { char buf[100]; sprintf( buf, "couldn't get palette: \"%s\"\n", argv[1] ); err( buf ); } // what to call output char outfile[256]; if ( argc > 2 ) { strcpy( outfile, argv[2] ); } else { sprintf( outfile, "%s.map", argv[1] ); } FILE *fp = fopen( outfile, "wb" ); header( fp ); printf( "data size of input file: %u\n", data_sz ); char * p = data; unsigned int i = 0; while ( i < data_sz ) { // trim any white space i += trim_whitespace( &p, i, data_sz ); if ( i >= data_sz ) break; // get length of line to newline int len = linelen( p, i, data_sz ); // check if line is a comment, if so remove it remove_comment( &p, &i, &len, data_sz ); if ( i >= data_sz ) break; // get a line char line[256]; strncpy( line, p, len ); line[len] = '\0'; // translate line values into 1 MapTile definition and print it printTile( line, len, fp ); // advance past end of line, to first character past the '\n' //i += eatline( &data ); p += len; i += len; } fclose( fp ); free( data ); printf( "creating: %s\n", outfile ); return 0; }
static cstring cutline(cstring& input) { size_t nlpos = linelen(input); return cut(input, 0, nlpos, (input[nlpos]=='\r') ? 2 : (input[nlpos]=='\n') ? 1 : 0); }