static gchar* parseParentClassIdent(void) { g_message("Line %d: Error in class declaration", g_scanner_cur_line(gScanner)); cleanupAndExit(0); return NULL; }
static void my_handler(GScanner *scanner, gchar *message, gboolean error) { char *mymsg; mymsg = g_strdup_printf("%s:%d:%d: %s", scanner->input_name, g_scanner_cur_line(scanner), g_scanner_cur_position(scanner), message); puts(mymsg); if (error) { g_set_error((GError **)scanner->user_data, MDT_ERROR, MDT_ERROR_FAILED, "%s", mymsg); } g_free(mymsg); }
/*----------------------- read conf (.guvcviewrc(-videoX)) file -----------------------*/ int readConf(struct GLOBAL *global) { int ret=0; // int signal=1; /*1=>+ or -1=>-*/ GScanner *scanner; GTokenType ttype; GScannerConfig config = { " \t\r\n", /* characters to skip */ G_CSET_a_2_z "_" G_CSET_A_2_Z, /* identifier start */ G_CSET_a_2_z "_." G_CSET_A_2_Z G_CSET_DIGITS,/* identifier cont. */ "#\n", /* single line comment */ FALSE, /* case_sensitive */ TRUE, /* skip multi-line comments */ TRUE, /* skip single line comments */ FALSE, /* scan multi-line comments */ TRUE, /* scan identifiers */ TRUE, /* scan 1-char identifiers */ FALSE, /* scan NULL identifiers */ FALSE, /* scan symbols */ FALSE, /* scan binary */ FALSE, /* scan octal */ TRUE, /* scan float */ TRUE, /* scan hex */ FALSE, /* scan hex dollar */ TRUE, /* scan single quote strings */ TRUE, /* scan double quote strings */ TRUE, /* numbers to int */ FALSE, /* int to float */ TRUE, /* identifier to string */ TRUE, /* char to token */ FALSE, /* symbol to token */ FALSE, /* scope 0 fallback */ TRUE /* store int64 */ }; int fd = g_open (global->confPath, O_RDONLY, 0); if (fd < 0 ) { printf("Could not open %s for read,\n will try to create it\n",global->confPath); ret=writeConf(global, global->videodevice); } else { scanner = g_scanner_new (&config); g_scanner_input_file (scanner, fd); scanner->input_name = global->confPath; for (ttype = g_scanner_get_next_token (scanner); ttype != G_TOKEN_EOF; ttype = g_scanner_get_next_token (scanner)) { if (ttype == G_TOKEN_STRING) { //printf("reading %s...\n",scanner->value.v_string); char *name = g_strdup (scanner->value.v_string); ttype = g_scanner_get_next_token (scanner); if (ttype != G_TOKEN_EQUAL_SIGN) { g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, NULL, FALSE); } else { ttype = g_scanner_get_next_token (scanner); /*check for signed integers*/ if(ttype == '-') { //signal = -1; ttype = g_scanner_get_next_token (scanner); } if (ttype == G_TOKEN_STRING) { //signal=1; /*reset signal*/ //if (g_strcmp0(name,"video_device")==0) //{ // g_snprintf(global->videodevice,15,"%s",scanner->value.v_string); //} /*must check for defaults since ReadOpts runs before ReadConf*/ if (g_strcmp0(name,"resolution")==0) { if(global->flg_res < 1) sscanf(scanner->value.v_string,"%ix%i", &(global->width), &(global->height)); } else if (g_strcmp0(name,"windowsize")==0) { sscanf(scanner->value.v_string,"%ix%i", &(global->winwidth), &(global->winheight)); } else if (g_strcmp0(name,"mode")==0) { if(global->flg_mode < 1) { /*use fourcc but keep it compatible with luvcview*/ if(g_strcmp0(scanner->value.v_string,"yuv") == 0) g_snprintf(global->mode,5,"yuyv"); else g_snprintf(global->mode,5,"%s",scanner->value.v_string); } } else if (g_strcmp0(name,"fps")==0) { sscanf(scanner->value.v_string,"%i/%i", &(global->fps_num), &(global->fps)); } #if 0 else if (g_strcmp0(name,"image_path")==0) { if(global->flg_imgFPath < 1) { global->imgFPath = splitPath(scanner->value.v_string,global->imgFPath); /*get the file type*/ global->imgFormat = check_image_type(global->imgFPath[0]); } } else if ((g_strcmp0(name,"video_path")==0) || (g_strcmp0(name,"avi_path")==0)) { if(global->vidfile == NULL) { global->vidFPath=splitPath(scanner->value.v_string,global->vidFPath); /*get the file type (0-avi 1-matroska)*/ global->VidFormat = check_video_type(global->vidFPath[0]); } } else { printf("unexpected string value (%s) for %s\n", scanner->value.v_string, name); } #endif } else if (ttype==G_TOKEN_INT) { if (g_strcmp0(name,"stack_size")==0) { global->stack_size = scanner->value.v_int; } else if (g_strcmp0(name,"vid_sleep")==0) { global->vid_sleep = scanner->value.v_int; } else if (g_strcmp0(name,"cap_meth")==0) { if(!(global->flg_cap_meth)) global->cap_meth = scanner->value.v_int; } else if (g_strcmp0(name,"vpane")==0) { global->boxvsize = scanner->value.v_int; } else if (g_strcmp0(name,"spinbehave")==0) { global->spinbehave = scanner->value.v_int; } else if (g_strcmp0(name,"fps")==0) { /*parse non-quoted fps values*/ int line = g_scanner_cur_line(scanner); global->fps_num = scanner->value.v_int; ttype = g_scanner_peek_next_token (scanner); if(ttype=='/') { /*get '/'*/ ttype = g_scanner_get_next_token (scanner); ttype = g_scanner_peek_next_token (scanner); if(ttype==G_TOKEN_INT) { ttype = g_scanner_get_next_token (scanner); global->fps = scanner->value.v_int; } else if (scanner->next_line>line) { /*start new loop*/ break; } else { ttype = g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, G_TOKEN_NONE, NULL, NULL, NULL, "bad value for fps", FALSE); } } } else if (strcmp(name,"fps_display")==0) { if(global->flg_FpsCount < 1) global->FpsCount = (short) scanner->value.v_int; } else if (g_strcmp0(name,"auto_focus")==0) { global->autofocus = scanner->value.v_int; } else if (g_strcmp0(name,"bpp")==0) { global->bpp = scanner->value.v_int; } else if (g_strcmp0(name,"hwaccel")==0) { if(global->flg_hwaccel < 1) global->hwaccel = scanner->value.v_int; } else if (g_strcmp0(name,"vid_codec")==0 || (g_strcmp0(name,"avi_format")==0)) { global->VidCodec = scanner->value.v_int; } else if ((g_strcmp0(name,"vid_inc")==0) || (g_strcmp0(name,"avi_inc")==0)) { global->vid_inc = (DWORD) scanner->value.v_int; g_snprintf(global->vidinc_str,20,_("File num:%d"),global->vid_inc); } else if (g_strcmp0(name,"frame_flags")==0) { global->Frame_Flags = scanner->value.v_int; } else if (g_strcmp0(name,"image_inc")==0) { if(global->image_timer <= 0) { global->image_inc = (DWORD) scanner->value.v_int; g_snprintf(global->imageinc_str,20,_("File num:%d"),global->image_inc); } } /* else { printf("unexpected integer value (%lu) for %s\n", scanner->value.v_int, name); printf("Strings must be quoted\n"); } */ } else if (ttype==G_TOKEN_FLOAT) { printf("unexpected float value (%f) for %s\n", scanner->value.v_float, name); } else if (ttype==G_TOKEN_CHAR) { printf("unexpected char value (%c) for %s\n", scanner->value.v_char, name); } else { g_scanner_unexp_token (scanner, G_TOKEN_NONE, NULL, NULL, NULL, "string values must be quoted - skiping", FALSE); int line = g_scanner_cur_line (scanner); int stp=0; do { ttype = g_scanner_peek_next_token (scanner); if(scanner->next_line > line) { //printf("next line reached\n"); stp=1; break; } else { ttype = g_scanner_get_next_token (scanner); } } while (!stp); } } g_free(name); } } g_scanner_destroy (scanner); close (fd); if (global->debug) { g_printf("video_device: %s\n",global->videodevice); // g_printf("vid_sleep: %i\n",global->vid_sleep); g_printf("cap_meth: %i\n",global->cap_meth); g_printf("resolution: %i x %i\n",global->width,global->height); // g_printf("windowsize: %i x %i\n",global->winwidth,global->winheight); // g_printf("vert pane: %i\n",global->boxvsize); // g_printf("spin behavior: %i\n",global->spinbehave); // g_printf("mode: %s\n",global->mode); g_printf("fps: %i/%i\n",global->fps_num,global->fps); g_printf("Display Fps: %i\n",global->FpsCount); g_printf("bpp: %i\n",global->bpp); g_printf("hwaccel: %i\n",global->hwaccel); // g_printf("avi_format: %i\n",global->VidCodec); g_printf("Pan Step: %i degrees\n",global->PanStep); g_printf("Tilt Step: %i degrees\n",global->TiltStep); g_printf("Video Filter Flags: %i\n",global->Frame_Flags); // g_printf("image inc: %d\n",global->image_inc); } } return (ret); }
static GTokenType _xfdashboard_css_selector_parse_css_rule(XfdashboardCssSelector *self, GScanner *inScanner) { XfdashboardCssSelectorPrivate *priv; GTokenType token; XfdashboardCssSelectorRule *rule, *parentRule; g_return_val_if_fail(XFDASHBOARD_IS_CSS_SELECTOR(self), G_TOKEN_ERROR); g_return_val_if_fail(inScanner, G_TOKEN_ERROR); priv=self->priv; /* Parse comma-seperated selectors until a left curly bracket is found */ parentRule=NULL; rule=NULL; token=g_scanner_peek_next_token(inScanner); while(token!=G_TOKEN_EOF) { switch((guint)token) { case G_TOKEN_IDENTIFIER: case '*': case '#': case '.': case ':': /* Set last selector as parent if available */ if(rule) parentRule=rule; else parentRule=NULL; /* Create new selector */ rule=_xfdashboard_css_selector_rule_new(inScanner->input_name, priv->priority, g_scanner_cur_line(inScanner), g_scanner_cur_position(inScanner)); priv->rule=rule; /* Check if there was a previous selector and if so, the new one * should use the previous selector to match an ancestor */ if(parentRule) { rule->parentRule=parentRule; rule->parentRuleMode=XFDASHBOARD_CSS_SELECTOR_RULE_MODE_ANCESTOR; } /* Parse selector */ token=_xfdashboard_css_selector_parse_css_simple_selector(self, inScanner, rule); if(token!=G_TOKEN_NONE) return(token); break; case '>': g_scanner_get_next_token(inScanner); /* Set last selector as parent selector */ if(!rule) { g_scanner_unexp_token(inScanner, G_TOKEN_IDENTIFIER, NULL, NULL, NULL, _("No parent when parsing '>'"), TRUE); return(G_TOKEN_ERROR); } parentRule=rule; /* Create new selector */ rule=_xfdashboard_css_selector_rule_new(inScanner->input_name, priv->priority, g_scanner_cur_line(inScanner), g_scanner_cur_position(inScanner)); priv->rule=rule; /* Link parent to the new selector as parent selector */ rule->parentRule=parentRule; rule->parentRuleMode=XFDASHBOARD_CSS_SELECTOR_RULE_MODE_PARENT; /* Parse selector */ token=_xfdashboard_css_selector_parse_css_simple_selector(self, inScanner, rule); if(token!=G_TOKEN_NONE) return(token); break; default: /* Stop at first invalid character in stream and * return with success result. */ return(G_TOKEN_NONE); } /* Continue parsing with next token */ token=g_scanner_peek_next_token(inScanner); } /* Eat "eof" token */ if(token==G_TOKEN_EOF) token=g_scanner_get_next_token(inScanner); /* Successfully parsed */ return(G_TOKEN_EOF); }
/* Parse selector */ static GTokenType _xfdashboard_css_selector_parse_css_simple_selector(XfdashboardCssSelector *self, GScanner *inScanner, XfdashboardCssSelectorRule *ioRule) { GTokenType token; g_return_val_if_fail(XFDASHBOARD_IS_CSS_SELECTOR(self), G_TOKEN_ERROR); g_return_val_if_fail(inScanner, G_TOKEN_ERROR); g_return_val_if_fail(ioRule, G_TOKEN_ERROR); /* Parse type of selector. It is optional as '*' can be used as wildcard */ token=g_scanner_peek_next_token(inScanner); switch((guint)token) { case '*': g_scanner_get_next_token(inScanner); ioRule->type=g_strdup("*"); /* Check if next token follows directly after this identifier. * It is determined by checking if scanner needs to move more than * one (the next) character. If there is a gap then either a new * selector follows or it is a new typeless selector. */ token=g_scanner_peek_next_token(inScanner); if(inScanner->next_line==g_scanner_cur_line(inScanner) && (inScanner->next_position-g_scanner_cur_position(inScanner))>1) { return(G_TOKEN_NONE); } break; case G_TOKEN_IDENTIFIER: g_scanner_get_next_token(inScanner); ioRule->type=g_strdup(inScanner->value.v_identifier); /* Check if next token follows directly after this identifier. * It is determined by checking if scanner needs to move more than * one (the next) character. If there is a gap then either a new * selector follows or it is a new typeless selector. */ token=g_scanner_peek_next_token(inScanner); if(inScanner->next_line==g_scanner_cur_line(inScanner) && (inScanner->next_position-g_scanner_cur_position(inScanner))>1) { return(G_TOKEN_NONE); } break; default: break; } /* Here we look for '#', '.' or ':' and return if we find anything else */ token=g_scanner_peek_next_token(inScanner); while(token!=G_TOKEN_NONE) { switch((guint)token) { /* Parse ID */ case '#': g_scanner_get_next_token(inScanner); token=g_scanner_get_next_token(inScanner); if(token!=G_TOKEN_IDENTIFIER) { g_scanner_unexp_token(inScanner, G_TOKEN_IDENTIFIER, NULL, NULL, NULL, _("Invalid name identifier"), TRUE); return(G_TOKEN_ERROR); } ioRule->id=g_strdup(inScanner->value.v_identifier); break; /* Parse class */ case '.': g_scanner_get_next_token(inScanner); token=g_scanner_get_next_token(inScanner); if(token!=G_TOKEN_IDENTIFIER) { g_scanner_unexp_token(inScanner, G_TOKEN_IDENTIFIER, NULL, NULL, NULL, _("Invalid class identifier"), TRUE); return(G_TOKEN_ERROR); } if(ioRule->classes) { /* Remember old classes as it can only be freed afterwards */ gchar *oldClasses=ioRule->classes; /* Create new list of classes */ ioRule->classes=g_strconcat(ioRule->classes, ".", inScanner->value.v_identifier, NULL); /* Now free old classes */ g_free(oldClasses); } else { ioRule->classes=g_strdup(inScanner->value.v_identifier); } break; /* Parse pseudo-class */ case ':': g_scanner_get_next_token(inScanner); token=g_scanner_get_next_token(inScanner); if(token!=G_TOKEN_IDENTIFIER) { g_scanner_unexp_token(inScanner, G_TOKEN_IDENTIFIER, NULL, NULL, NULL, _("Invalid pseudo-class identifier"), TRUE); return(G_TOKEN_ERROR); } if(ioRule->pseudoClasses) { /* Remember old pseudo-classes as it can only be freed afterwards */ gchar *oldPseudoClasses=ioRule->pseudoClasses; /* Create new list of pseudo-classes */ ioRule->pseudoClasses=g_strconcat(ioRule->pseudoClasses, ":", inScanner->value.v_identifier, NULL); /* Now free old pseudo-classes */ g_free(oldPseudoClasses); } else { ioRule->pseudoClasses=g_strdup(inScanner->value.v_identifier); } break; default: return(G_TOKEN_NONE); } /* Get next token */ token=g_scanner_peek_next_token(inScanner); } /* Successfully parsed */ return(G_TOKEN_NONE); }
static void doClassDeclaration(void) { PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data; PINTERFACE pif; gchar *chrTemp=pParseInfo->pCurInterface->chrName; /* Check if we already have a (maybe forward) declaration */ pif=findInterfaceFromName(pParseInfo->pCurInterface->chrName); if(pif) { if(pif->fIsForwardDeclaration) { /* Remove the forward declaration and insert the real thing afterwards. */ deRegisterInterface(pif); } else { /* It˚s the declaration from the *.h file. Save a pointer to this information. */ pParseInfo->pClassDefinition=pif; deRegisterInterface(pif); } } pParseInfo->pCurInterface->chrName=chrTemp; pParseInfo->pCurInterface->chrSourceFileName=g_strdup(pParseInfo->chrCurrentSourceFile); /* It's save to register the interface right here even if the struct is almost empty. If anything goes wrong later we will exit anyway. */ registerInterface(); /* The class definition in *.nom files does not contain all the stuff an interface may define. We use the found interface to fill the gaps. If we don˚t have an interface something went wrong and we quit. */ if(!pParseInfo->pClassDefinition) { g_message("Line %d: Error during class parsing. No class definition found. MAke sure you included the *.ih file.", g_scanner_cur_line(gScanner)); cleanupAndExit(0); } pParseInfo->pCurInterface->chrParent=g_strdup(pParseInfo->pClassDefinition->chrParent); /* Note: We don˚t support subclasses yet. */ if(matchNext(':')) { parseParentClassIdent(); } parseClassBody(); }
void printToFile(GTokenType token) { GTokenValue value=gScanner->value; guint uiCurLine=g_scanner_cur_line(gScanner); PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data; return; if(!pParseInfo->fPrintToken) return; if(0==pParseInfo->uiCurFileLine) { pParseInfo->uiCurPos=1; } else { while(pParseInfo->uiCurFileLine < uiCurLine) { g_printf("\n"); pParseInfo->uiCurFileLine++; pParseInfo->uiCurPos=1; } } pParseInfo->uiCurFileLine=uiCurLine; //g_printf("-> %d %d\n", gScanner->position, pParseInfo->uiCurPos); //g_printf("-> %d %d\n", g_scanner_cur_position(gScanner), pParseInfo->uiCurPos); // uiCurLine+=pParseInfo->uiLineCorrection; //g_printf("-> %d", gScanner->position); switch(token) { case G_TOKEN_SYMBOL: { PSYMBOL pCurSymbol=value.v_symbol; insertSpaces(g_scanner_cur_position(gScanner)-strlen(pCurSymbol->chrSymbolName)+1); g_printf("%s", pCurSymbol->chrSymbolName); // pParseInfo->uiCurPos+=strlen(pCurSymbol->chrSymbolName); break; } case G_TOKEN_STRING: insertSpaces(g_scanner_cur_position(gScanner)-strlen(value.v_string)-1); g_print("\"%s\"", value.v_string); pParseInfo->uiCurPos+=strlen(value.v_string); break; case G_TOKEN_INT: insertSpaces(g_scanner_cur_position(gScanner)); g_print("%d", value.v_int); break; case G_TOKEN_IDENTIFIER: insertSpaces(g_scanner_cur_position(gScanner)-strlen(value.v_identifier)+1); g_print("%s", value.v_identifier); pParseInfo->uiCurPos+=strlen(value.v_identifier); break; #if 0 case G_TOKEN_IDENTIFIER: g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s (LINE %d)", token, value.v_identifier, uiCurLine); break; case G_TOKEN_STRING: g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string); break; case G_TOKEN_LEFT_PAREN: g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t\t( (LINE %d)", token, uiCurLine); break; case G_TOKEN_RIGHT_PAREN: g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t\t) (LINE %d)", token, uiCurLine); break; case G_TOKEN_LEFT_CURLY: g_message("Token: %d (G_TOKEN_LEFT_CURLY)\t\t\t{ (LINE %d)", token, uiCurLine); break; case G_TOKEN_RIGHT_CURLY: g_message("Token: %d (G_TOKEN_RIGHT_CURLY)\t\t\t} (LINE %d)", token, uiCurLine); break; case ':': g_message("Token: %d (colon)\t\t:", token); break; case ';': g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, uiCurLine); break; case '#': g_message("Token: %d (hash)\t\t\t#", token); break; case '/': g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment); break; case G_TOKEN_COMMA: g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token); break; case G_TOKEN_INT: g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int); break; #endif default: { insertSpaces(g_scanner_cur_position(gScanner)); g_print("%c", token); pParseInfo->uiCurPos++; break; } /* default */ } /* switch */ //fprintf(fh, "#define %s_%s(nomSelf, ", pif->chrName, pm->chrName); }
/* Print current token info. */ void printToken(GTokenType token) { GTokenValue value=gScanner->value; guint uiCurLine=g_scanner_cur_line(gScanner); PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data; //g_message("line correction: %d", pParseInfo->uiLineCorrection); uiCurLine+=pParseInfo->uiLineCorrection; //return; switch(token) { case G_TOKEN_SYMBOL: { PSYMBOL pCurSymbol=value.v_symbol; switch(pCurSymbol->uiSymbolToken) { case NOMC_SYMBOL_CLASS: g_message("Token: %d (NOMC_SYMBOL_CLASS)\t\t\t (LINE %d)", pCurSymbol->uiSymbolToken, uiCurLine); break; case IDL_SYMBOL_DEFINE: g_message("Token: %d (IDL_SYMBOL_DEFINE)\t\t\t", pCurSymbol->uiSymbolToken); break; case IDL_SYMBOL_IFDEF: g_message("Token: %d (IDL_SYMBOL_IFDEF)\t\t\t", pCurSymbol->uiSymbolToken); break; case IDL_SYMBOL_ENDIF: g_message("Token: %d (IDL_SYMBOL_ENDIF)\t\t\t", pCurSymbol->uiSymbolToken); break; default: { g_message("Token: %d (%s)\t\t\t (LINE %d)", pCurSymbol->uiSymbolToken, pCurSymbol->chrSymbolName, uiCurLine); break; } }/* switch */ break; } case G_TOKEN_IDENTIFIER: g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s (LINE %d)", token, value.v_identifier, uiCurLine); break; case G_TOKEN_STRING: g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string); break; case G_TOKEN_LEFT_PAREN: g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t\t( (LINE %d)", token, uiCurLine); break; case G_TOKEN_RIGHT_PAREN: g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t\t) (LINE %d)", token, uiCurLine); break; case G_TOKEN_LEFT_CURLY: g_message("Token: %d (G_TOKEN_LEFT_CURLY)\t\t\t{ (LINE %d)", token, uiCurLine); break; case G_TOKEN_RIGHT_CURLY: g_message("Token: %d (G_TOKEN_RIGHT_CURLY)\t\t\t} (LINE %d)", token, uiCurLine); break; case ':': g_message("Token: %d (colon)\t\t:", token); break; case ';': g_message("Token: %d (semicolon)\t\t\t; (LINE %d)", token, uiCurLine); break; case '#': g_message("Token: %d (hash)\t\t\t#", token); break; case '/': g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment); break; case G_TOKEN_COMMA: g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token); break; case G_TOKEN_INT: g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int); break; default: { g_message("Token: %d (---)\t\t\t (LINE %d)", token, uiCurLine); break; } /* default */ } /* switch */ }
GPtrArray * read_langconf_from (gchar *filename) { GScanner *scanner; GTokenType ttype; gint fd; struct langconf *conf = NULL; enum parser_state state; GPtrArray *lang_conf; gboolean error_occured = FALSE; fd = open (filename, O_RDONLY); if (fd < 0) { /*g_printf ("%s: couldn't open\n", filename);*/ return NULL; } state = PARSER_INIT; lang_conf = g_ptr_array_new (); scanner = g_scanner_new (&scanner_conf); /* scanner = g_scanner_new (NULL);*/ g_scanner_input_file (scanner, fd); scanner->input_name = filename; for (ttype = g_scanner_get_next_token (scanner); ttype != G_TOKEN_EOF; ttype = g_scanner_get_next_token (scanner)) { gint line = g_scanner_cur_line (scanner); /* guint pos = g_scanner_cur_position (scanner);*/ error_occured = FALSE; if(state == PARSER_INIT || state == PARSER_LETTER) { if (ttype == G_TOKEN_STRING && strcmp (scanner->value.v_string, "language") == 0) { if (conf) { g_ptr_array_add (lang_conf, conf); DEBUGMSG ("lang %s added\n", conf->lang); } state = PARSER_LANG; conf = g_new0 (struct langconf, 1); conf->alphabet = g_ptr_array_new (); conf->weights = g_array_new (TRUE, TRUE, sizeof (gint)); continue; } else if (state == PARSER_INIT) { LANGCONF_ERROR_MSG ("keyword language expected", filename, line); error_occured = TRUE; break; } } switch (state) { case PARSER_INIT: g_assert_not_reached (); break; case PARSER_LANG: if (ttype != G_TOKEN_STRING) { LANGCONF_ERROR_MSG ("language name expected after keyword language", filename, line); error_occured = TRUE; break; } conf->lang = g_strdup (scanner->value.v_string); state = PARSER_DICTFILE_KEYWORD; break; case PARSER_DICTFILE_KEYWORD: if (ttype != G_TOKEN_STRING || strcmp (scanner->value.v_string, "dictfile") != 0) { LANGCONF_ERROR_MSG ("keyword \"dictfile\" expected", filename, line); error_occured = TRUE; break; } state = PARSER_DICTFILE; break; case PARSER_DICTFILE: if (ttype != G_TOKEN_STRING) { LANGCONF_ERROR_MSG ("dict filename expected", filename, line); error_occured = TRUE; break; } conf->dictf = g_strdup (scanner->value.v_string); state = PARSER_LETTER; break; case PARSER_LETTER: if (ttype != G_TOKEN_STRING || strlen(scanner->value.v_string) > MAX_LETTER_LENGTH) { LANGCONF_ERROR_MSG ("letter expected", filename, line); error_occured = TRUE; break; } g_ptr_array_add (conf->alphabet, g_strdup (scanner->value.v_string)); state = PARSER_WEIGHT; break; case PARSER_WEIGHT: if (ttype != G_TOKEN_INT) { LANGCONF_ERROR_MSG ("number expected", filename, line); error_occured = TRUE; break; } g_array_append_val (conf->weights, scanner->value.v_int); state = PARSER_LETTER; break; } if (error_occured) break; } /* for get_next_token */
/*----------------------- read conf (.config/guvcview/videoX) file -----------------------*/ int readConf(struct GLOBAL *global) { int ret=0; //int signal=1; /*1=>+ or -1=>-*/ GScanner *scanner; GTokenType ttype; GScannerConfig config = { " \t\r\n", /* characters to skip */ G_CSET_a_2_z "_" G_CSET_A_2_Z, /* identifier start */ G_CSET_a_2_z "_." G_CSET_A_2_Z G_CSET_DIGITS,/* identifier cont. */ "#\n", /* single line comment */ FALSE, /* case_sensitive */ TRUE, /* skip multi-line comments */ TRUE, /* skip single line comments */ FALSE, /* scan multi-line comments */ TRUE, /* scan identifiers */ TRUE, /* scan 1-char identifiers */ FALSE, /* scan NULL identifiers */ FALSE, /* scan symbols */ FALSE, /* scan binary */ FALSE, /* scan octal */ TRUE, /* scan float */ TRUE, /* scan hex */ FALSE, /* scan hex dollar */ TRUE, /* scan single quote strings */ TRUE, /* scan double quote strings */ TRUE, /* numbers to int */ FALSE, /* int to float */ TRUE, /* identifier to string */ TRUE, /* char to token */ FALSE, /* symbol to token */ FALSE, /* scope 0 fallback */ TRUE /* store int64 */ }; int fd = g_open (global->confPath, O_RDONLY, 0); if (fd < 0 ) { printf("Could not open %s for read,\n will try to create it\n",global->confPath); ret=writeConf(global, global->videodevice); } else { scanner = g_scanner_new (&config); g_scanner_input_file (scanner, fd); scanner->input_name = global->confPath; //temp codec values int ac_bit_rate =-1, vc_bit_rate=-1, vc_fps=-1, vc_qmax=-1, vc_qmin=-1, vc_max_qdiff=-1, vc_dia=-1; int vc_pre_dia=-1, vc_pre_me=-1, vc_me_pre_cmp=-1, vc_me_cmp=-1, vc_me_sub_cmp=-1, vc_last_pred=-1; int vc_gop_size=-1, vc_subq=-1, vc_framerefs=-1, vc_mb_decision=-1, vc_trellis=-1, vc_me_method=-1; int vc_mpeg_quant=-1, vc_max_b_frames=-1, vc_num_threads=-1, vc_flags=-1, vc_monotonic_pts=-1; float vc_qcompress=-1, vc_qblur=-1; int VMAJOR =-1, VMINOR=-1, VMICRO=-1; for (ttype = g_scanner_get_next_token (scanner); ttype != G_TOKEN_EOF; ttype = g_scanner_get_next_token (scanner)) { if (ttype == G_TOKEN_STRING) { //printf("reading %s...\n",scanner->value.v_string); char *name = g_strdup (scanner->value.v_string); ttype = g_scanner_get_next_token (scanner); if (ttype != G_TOKEN_EQUAL_SIGN) { g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, NULL, FALSE); } else { ttype = g_scanner_get_next_token (scanner); /*check for signed integers*/ if(ttype == '-') { //signal = -1; ttype = g_scanner_get_next_token (scanner); } if (ttype == G_TOKEN_STRING) { if (g_strcmp0(name,"version")==0) { sscanf(scanner->value.v_string,"%i.%i.%i", &(VMAJOR), &(VMINOR), &(VMICRO)); } else if (g_strcmp0(name,"resolution")==0) { if(global->flg_res < 1) /*must check for defaults since ReadOpts runs before ReadConf*/ sscanf(scanner->value.v_string,"%ix%i", &(global->width), &(global->height)); } else if (g_strcmp0(name,"windowsize")==0) { sscanf(scanner->value.v_string,"%ix%i", &(global->winwidth), &(global->winheight)); } else if (g_strcmp0(name,"mode")==0) { if(global->flg_mode < 1) { /*use fourcc but keep it compatible with luvcview*/ if(g_strcmp0(scanner->value.v_string,"yuv") == 0) g_snprintf(global->mode,5,"yuyv"); else g_snprintf(global->mode,5,"%s",scanner->value.v_string); } } else if (g_strcmp0(name,"fps")==0) { sscanf(scanner->value.v_string,"%i/%i", &(global->fps_num), &(global->fps)); } else if (g_strcmp0(name,"image_path")==0) { if(global->flg_imgFPath < 1) { global->imgFPath = splitPath(scanner->value.v_string,global->imgFPath); /*get the file type*/ global->imgFormat = check_image_type(global->imgFPath[0]); } else { /* check if new file != old file */ gchar * newPath = g_strjoin ("/", global->imgFPath[1], global->imgFPath[0], NULL); //printf("image path: %s\n old path: %s\n", newPath, scanner->value.v_string); if(g_strcmp0(scanner->value.v_string, newPath) !=0) { /* reset counter */ //printf("reset counter from: %i\n", global->image_inc); if(global->image_inc > 0) { global->image_inc = 1; //g_snprintf(global->imageinc_str,20,_("File num:%d"),global->image_inc); } } g_free(newPath); } } else if ((g_strcmp0(name,"video_path")==0) || (g_strcmp0(name,"avi_path")==0)) { if(global->vidfile == NULL) { global->vidFPath=splitPath(scanner->value.v_string,global->vidFPath); } } else if (g_strcmp0(name,"profile_path")==0) { if(global->lprofile < 1) global->profile_FPath=splitPath(scanner->value.v_string, global->profile_FPath); } else { printf("unexpected string value (%s) for %s\n", scanner->value.v_string, name); } } else if (ttype==G_TOKEN_INT) { if (g_strcmp0(name,"stack_size")==0) { global->stack_size = scanner->value.v_int; } else if (g_strcmp0(name,"vid_sleep")==0) { global->vid_sleep = scanner->value.v_int; } else if (g_strcmp0(name,"cap_meth")==0) { if(!(global->flg_cap_meth)) global->cap_meth = scanner->value.v_int; } else if (g_strcmp0(name,"spinbehave")==0) { global->spinbehave = scanner->value.v_int; } else if (g_strcmp0(name,"default_action")==0) { global->default_action = scanner->value.v_int; } else if (g_strcmp0(name,"fps")==0) { /*parse non-quoted fps values*/ int line = g_scanner_cur_line(scanner); global->fps_num = scanner->value.v_int; ttype = g_scanner_peek_next_token (scanner); if(ttype=='/') { /*get '/'*/ ttype = g_scanner_get_next_token (scanner); ttype = g_scanner_peek_next_token (scanner); if(ttype==G_TOKEN_INT) { ttype = g_scanner_get_next_token (scanner); global->fps = scanner->value.v_int; } else if (scanner->next_line>line) { /*start new loop*/ break; } else { ttype = g_scanner_get_next_token (scanner); g_scanner_unexp_token (scanner, G_TOKEN_NONE, NULL, NULL, NULL, "bad value for fps", FALSE); } } } else if (strcmp(name,"fps_display")==0) { if(global->flg_FpsCount < 1) global->FpsCount = (short) scanner->value.v_int; } else if (g_strcmp0(name,"auto_focus")==0) { global->autofocus = scanner->value.v_int; } else if (g_strcmp0(name,"bpp")==0) { global->bpp = scanner->value.v_int; } else if (g_strcmp0(name,"hwaccel")==0) { if(global->flg_hwaccel < 1) global->hwaccel = scanner->value.v_int; } else if (g_strcmp0(name,"vid_codec")==0 || (g_strcmp0(name,"avi_format")==0)) { global->VidCodec = scanner->value.v_int; } else if (g_strcmp0(name,"vid_format")==0) { global->VidFormat = scanner->value.v_int; } else if ((g_strcmp0(name,"vid_inc")==0) || (g_strcmp0(name,"avi_inc")==0)) { global->vid_inc = (DWORD) scanner->value.v_int; } else if (g_strcmp0(name,"sound")==0) { global->Sound_enable = (short) scanner->value.v_int; } else if (g_strcmp0(name,"snd_api")==0) { global->Sound_API = scanner->value.v_int; } else if (g_strcmp0(name,"snd_device")==0) { global->Sound_UseDev = scanner->value.v_int; } else if (g_strcmp0(name,"snd_samprate")==0) { global->Sound_SampRateInd = scanner->value.v_int; } else if (g_strcmp0(name,"snd_numchan")==0) { global->Sound_NumChanInd = scanner->value.v_int; } else if (g_strcmp0(name,"snd_delay")==0) { global->Sound_delay = scanner->value.v_int64; } else if (g_strcmp0(name,"aud_codec")==0) { global->AudCodec = scanner->value.v_int; global->Sound_Format = get_aud4cc(global->AudCodec); } else if (g_strcmp0(name,"frame_flags")==0) { global->Frame_Flags = scanner->value.v_int; } else if (g_strcmp0(name,"osd_flags")==0) { global->osdFlags = scanner->value.v_int; } else if (g_strcmp0(name,"image_inc")==0) { global->image_inc = (DWORD) scanner->value.v_int; } else if (g_strcmp0(name,"acodec_bit_rate")==0) { ac_bit_rate = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_bit_rate")==0) { vc_bit_rate = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_fps")==0) { vc_fps = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_monotonic_pts")==0) { vc_monotonic_pts = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_qmax")==0) { vc_qmax = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_qmin")==0) { vc_qmin = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_max_qdiff")==0) { vc_max_qdiff = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_dia")==0) { vc_dia = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_pre_dia")==0) { vc_pre_dia = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_pre_me")==0) { vc_pre_me= scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_me_pre_cmp")==0) { vc_me_pre_cmp = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_me_cmp")==0) { vc_me_cmp = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_me_sub_cmp")==0) { vc_me_sub_cmp = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_last_pred")==0) { vc_last_pred = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_gop_size")==0) { vc_gop_size = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_subq")==0) { vc_subq = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_framerefs")==0) { vc_framerefs = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_mb_decision")==0) { vc_mb_decision = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_trellis")==0) { vc_trellis = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_me_method")==0) { vc_me_method = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_mpeg_quant")==0) { vc_mpeg_quant = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_max_b_frames")==0) { vc_max_b_frames = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_flags")==0) { vc_flags = scanner->value.v_int; } else if (g_strcmp0(name,"vcodec_num_threads")==0) { vc_num_threads = scanner->value.v_int; } else { printf("unexpected integer value (%lu) for %s\n", scanner->value.v_int, name); printf("Strings must be quoted\n"); } } else if (ttype==G_TOKEN_FLOAT) { if (g_strcmp0(name,"vcodec_qcompress")==0) { vc_qcompress = scanner->value.v_float; } else if (g_strcmp0(name,"vcodec_qblur")==0) { vc_qblur = scanner->value.v_float; } else printf("unexpected float value (%f) for %s\n", scanner->value.v_float, name); } else if (ttype==G_TOKEN_CHAR) { printf("unexpected char value (%c) for %s\n", scanner->value.v_char, name); } else { g_scanner_unexp_token (scanner, G_TOKEN_NONE, NULL, NULL, NULL, "string values must be quoted - skiping", FALSE); int line = g_scanner_cur_line (scanner); int stp=0; do { ttype = g_scanner_peek_next_token (scanner); if(scanner->next_line > line) { //printf("next line reached\n"); stp=1; break; } else { ttype = g_scanner_get_next_token (scanner); } } while (!stp); } } g_free(name); } } g_scanner_destroy (scanner); close (fd); //get pointers to codec properties vcodecs_data *vcodec_defaults = get_codec_defaults(global->VidCodec); acodecs_data *acodec_defaults = get_aud_codec_defaults(get_ind_by4cc(global->Sound_Format)); if (ac_bit_rate >= 0) acodec_defaults->bit_rate = ac_bit_rate; if (vc_bit_rate >= 0) vcodec_defaults->bit_rate = vc_bit_rate; if (vc_fps >= 0) vcodec_defaults->fps = vc_fps; //from 1.5.3 onwards we set version on conf file and monotonic is set by default for all codecs if ((vc_monotonic_pts >= 0) && (VMAJOR > 0)) vcodec_defaults->monotonic_pts = vc_monotonic_pts; if (vc_qmax >= 0) vcodec_defaults->qmax = vc_qmax; if (vc_qmin >= 0) vcodec_defaults->qmin = vc_qmin; if (vc_max_qdiff >=0) vcodec_defaults->max_qdiff = vc_max_qdiff; if (vc_dia >=0) vcodec_defaults->dia = vc_dia; if (vc_pre_dia >=0) vcodec_defaults->pre_dia = vc_pre_dia; if (vc_pre_me >=0) vcodec_defaults->pre_me = vc_pre_me; if (vc_me_pre_cmp >=0) vcodec_defaults->me_pre_cmp = vc_me_pre_cmp; if (vc_me_cmp >=0) vcodec_defaults->me_cmp = vc_me_cmp; if (vc_me_sub_cmp >=0) vcodec_defaults->me_sub_cmp = vc_me_sub_cmp; if (vc_last_pred >= 0) vcodec_defaults->last_pred = vc_last_pred; if (vc_gop_size >= 0) vcodec_defaults->gop_size = vc_gop_size; if (vc_subq >=0) vcodec_defaults->subq = vc_subq; if (vc_framerefs >=0) vcodec_defaults->framerefs = vc_framerefs; if (vc_mb_decision >=0) vcodec_defaults->mb_decision = vc_mb_decision; if (vc_trellis >=0) vcodec_defaults->trellis = vc_trellis; if (vc_me_method >=0) vcodec_defaults->me_method = vc_me_method; if (vc_mpeg_quant >=0) vcodec_defaults->mpeg_quant = vc_mpeg_quant; if (vc_max_b_frames >=0) vcodec_defaults->max_b_frames = vc_max_b_frames; if (vc_num_threads >=0) vcodec_defaults->num_threads = vc_num_threads; if (vc_flags >=0) vcodec_defaults->flags = vc_flags; if (vc_qcompress >= 0) vcodec_defaults->qcompress = vc_qcompress; if (vc_qblur >=0) vcodec_defaults->qblur = vc_qblur; if(global->vid_inc>0) { uint64_t suffix = get_file_suffix(global->vidFPath[1], global->vidFPath[0]); fprintf(stderr, "Video file suffix detected: %" PRIu64 "\n", suffix); if(suffix >= G_MAXUINT64) { global->vidFPath[0] = add_file_suffix(global->vidFPath[0], suffix); suffix = 0; } if(suffix > 0) global->vid_inc = suffix + 1; } if(global->image_inc>0) { uint64_t suffix = get_file_suffix(global->imgFPath[1], global->imgFPath[0]); fprintf(stderr, "Image file suffix detected: %" PRIu64 "\n", suffix); if(suffix >= G_MAXUINT64) { global->imgFPath[0] = add_file_suffix(global->imgFPath[0], suffix); suffix = 0; } if(suffix > 0) global->image_inc = suffix + 1; } if (global->debug) { g_print("video_device: %s\n",global->videodevice); g_print("vid_sleep: %i\n",global->vid_sleep); g_print("cap_meth: %i\n",global->cap_meth); g_print("resolution: %i x %i\n",global->width,global->height); g_print("windowsize: %i x %i\n",global->winwidth,global->winheight); g_print("spin behavior: %i\n",global->spinbehave); g_print("default action: %i\n",global->default_action); g_print("mode: %s\n",global->mode); g_print("fps: %i/%i\n",global->fps_num,global->fps); g_print("Display Fps: %i\n",global->FpsCount); g_print("bpp: %i\n",global->bpp); g_print("hwaccel: %i\n",global->hwaccel); g_print("avi_format: %i\n",global->VidCodec); g_print("sound: %i\n",global->Sound_enable); g_print("sound Device: %i\n",global->Sound_UseDev); g_print("sound samp rate: %i\n",global->Sound_SampRateInd); g_print("sound Channels: %i\n",global->Sound_NumChanInd); g_print("Sound delay: %llu nanosec\n",(unsigned long long) global->Sound_delay); g_print("Sound Format: %i \n",global->Sound_Format); g_print("Pan Step: %i degrees\n",global->PanStep); g_print("Tilt Step: %i degrees\n",global->TiltStep); g_print("Video Filter Flags: %i\n",global->Frame_Flags); g_print("image inc: %" PRIu64 "\n",global->image_inc); g_print("profile(default):%s/%s\n",global->profile_FPath[1],global->profile_FPath[0]); } } return (ret); }