Want to check values are present or not in file [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here I have one file which contains some information, and I want to check some values of tags are present or not in file.If present, then I want to retrieve these values.
Here number of tags is fixed and lenght of tag would not be more than 16 and length of value of tag is almost fix it would not more than 10 bytes.
From below file I want to check KERNEL tag value is present or not,FS tag value is present or not, etc etc.
I want to check all values are present or not after : (colon).
My file contains text like this.
KERNEL:2.31
FS:3.4
XLOADER:1.1
UBOOT:2.2
or like this
KERNEL:2.31
FS:
XLOADER:1.3
UBOOT:
I am using this code
#include <stdio.h>
int main() {
FILE *infile = fopen("example.txt", "r");
char buffer[256];
char value[128];
while (fgets(buffer, sizeof(buffer), infile))
if (1 == sscanf(buffer, "KERNEL:%s", value)) {
printf("Value = %s\n", value);
}
return 0;
}
but in code i have to call 4 this function 4 times for different different values. like KERNEL,FS, etc.
this code output like this
Value = 2.31
i read line from the data file and than i want to parse value of every tag (Means wants to identify values is present or not).
So this is Good way to do this thing can any body suggest me ?

Your question does not make it clear what context the file is in (or did not when I started answering). Presumably, it is a text file that the application can find by some means.
You've not specified whether the contents (tags) are supposed to be fixed, whether the file can contain other information, whether there's a comment convention in place, whether blank lines are allowed, what happens if a tag is missing altogether, what happens if there's an unexpected tag in the file, what happens if a tag is repeated (with the same version, with a different version). Are the tags case-sensitive; are leading blanks allowed before the tag; before the version; after the version? What characters are allowed in the version number? These are details that matter.
Let's assume that the list of names is fixed. Let's assume that tags are not longer than 7 (8 with terminal null). Let's assume that version numbers are not longer than 15 characters in total (16 allowing for terminal null). Let's assume that you need to keep a record of which tags you found and the version that you found.
In that case, you will end up with a data structure a bit like this:
typedef struct VersionInfo
{
char tag[8];
char version[16];
} VersionInfo;
static VersionInfo version_data[] =
{
{ "KERNEL", "" },
{ "FS", "" },
{ "XLOADER", "" },
{ "UBOOT", "" },
};
Clearly, with that basic structure available, you can write a function to read each line from the data file, discarding any comment or blank lines (if that's appropriate). You can probably use fgets() for this; the expected lines are short. You should probably complain about long lines, and decide whether to ignore them or stop processing. You can look for each of the tags in the version_data array, and when you find one, note whether it has been found before or not, and then find the version information on the line and copy it into the corresponding part of the version_data array.
You might extend the structure with an 'expected version' field as well as the actual version field. This will allow you to decide what to do based on the versions you find. You might have ranges related the allowable versions, based on what was known when the program was compiled, for example. You might allow the program that was compiled with 3.0.3 of something to run with 3.0.4 and later, and maybe you're even willing to work with 3.0.0 and later, but not any version 2.
Note that version comparison is a fine art in its own right. For example, the versions 3.0.3, 3.1.2, 3.6, 3.6.1, 3.6.1.2, and 3.10.0 should probably be treated as being in ascending order of version number. Using strcmp() won't achieve that (it will place 3.10.0 ahead of all the 3.6 versions).
I see that your example code uses sscanf() with a fixed string. That probably ties you to a fixed order and certainly not as flexible as the data structure allows you to be. If using sscanf(), I've be expecting to use a format such as:
if (sscanf(buffer, "%7s:%15s", tag, version) != 2)
...something up with the input line...
...process tag and version that you did find...
You can refine the scans with the character class notations:
if (sscanf(buffer, "%7[A-Z]:%15[0-9.]", tag, version) != 2)
This only accepts upper-case letters in the tag and only accepts digits and dots in the version information (but is quite happy with version "..0...0...0" which you probably shouldn't accept as valid).

Given your samples, the first approach I'd try is:
Read a line
Remove all whitespace
If the last character on the line is a colon <do something>
Else
Do something else

Related

Icarus verilog dump memory array ($dumpvars)

I try to dump an array (reg [31:0] data [31:0]) but I can't do it successfully.
I've tried the way that is in the iverilog wiki:
integer idx;
for (idx = 0; idx < 32; idx = idx + 1)
$dumpvars(0,cpu_tb.cpu0.cpu_dp.cpu_regs.data[idx]);
It works, but 2 things happen.
A warning shows up: VCD warning: array word cpu_tb.cpu0.cpu_dp.cpu_regs.data[0] will conflict with an escaped identifier.
In GTKWave I have something like this in SST window: \data[0][31:0]
Is there any solution about that?
Thanks in advance and sorry for my English.
I have e-mailed the mailing list of Icarus Verilog. Here are some answers:
To dump an array word Icarus needs to escape the name so it is
compatible with the VCD dump format. That's what \data[0][31:0] is. It
is the zeroth 32-bit word of the data array. Because an escaped name
and an array name could now conflict Icarus produces the warning. It
would be best if it could check for an escaped identifier conflict and
only print a message when there is a problem, but as I remember this
was not possible.
We chose to use escaped identifiers so that all the dumpers could
handle array words. The other common choice is to only support them
using a special dump command that only works with certain dump
formats.
I agree it would be nice if we could make the warning more accurate,
but we are usually busy working on other things so minor annoyances
that appear to be complicated to fix do not often get fixed. As I
remember, and it has been a number of years, the issue is if you
search for the escaped identifier it find the array element and there
is no way in the VPI to search for the next occurrence. It's possible
that finding the array element in the Icarus search by name
implementation is a bug.
Cary
"To dump an array word Icarus needs to escape the name so it is
compatible with the VCD dump format. That's what \data[0][31:0] is.
It is the zeroth? 32-bit word of the data array. Because an escaped
name and an array name could now conflict Icarus produces the
warning. It would be best if it could check for an escaped identifier
conflict and only print a message when there is a problem, but as I
remember this was not possible."
...I don't think that there's a need to escape the names. Both VCS
(followed by fsdb2vcd) and CVC emit the name directly with no
problems. Cut and paste example shown below:
$var wire 5 `' IC_DrAd0 [3][4:0] $end $var wire 5 a' IC_DrAd0 [2][4:0]
$end $var wire 5 b' IC_DrAd0 [1][4:0] $end $var wire 5 c' IC_DrAd0
[0][4:0] $end
I realize the VCD spec doesn't define this, but I've had to fold in a
lot of these kinds of extensions into gtkwave over the years as other
tools generate these constructs. The escapes can cause save file
incompatibilities (missing signals) when trying to simulate on
iverilog versus VCS.
Over time, SV constructs likely will cause further things added to the
VCD files. AFAIK, the VCD part of the 1364 spec hasn't updated at all
since Verilog-XL. CVC gets around possible incompatibilities by
adding a +dump_arrays plusarg (and no, you don't have to loop on each
array element either).
-Tony
I also sent a mail to GTKWave creator Tony Bybell:
Hello,
The problem is that the compiler is not emitting those values into the
dump file. You'll have to get in contact with the iverilog
developers. I see the same problem if I run sim and compare against
another simulator such as CVC with +dump_arrays turned on which does
dump the arrays and they are visible in gtkwave.
http://iverilog.wikia.com/wiki/Release_Notes_Icarus_Verilog_0_9_2 |
Allow $dumpvars to accept array members for dumping,
...it looks like during "initial" time you might need to add a
$dumpvars statement for each array element you want dumped. I don't
know if the array name by itself works. Assigning each element to a
"wire" might work too.
I have never tried this functionality in iverilog so I don't know if
it works. You might have to experiment or ask the developers.
-Tony
I had a similar issue recently:
When dumping vars with the for cycle like the question, this vcd error happens:
ERROR: $dumpvars cannot dump a vpiConstant.
My workaround is to generate n wires with assign statement assigning it the respective array word like this:
reg [31:0] registers [31:0];
generate
genvar idx;
for(idx = 0; idx < 32; idx = idx+1) begin: register
wire [31:0] tmp;
assign tmp = registers[idx];
end
endgenerate
Now in GTKWave I have the generate blocks dumped correctly.

GSSAPI: gss_export_name returns a blank

I am having a problem with exporting a name using gss_export_name, I though that once the name is exported I should be able to just print it but I am turning up a blank Literaly
EXPORTED NAME: , EXPORTED NAME LENGTH: 47
Here is my code
OM_uint32 major_status;
gss_cred_usage_t usage;
OM_uint32 lifetime;
gss_name_t inquired_name;
major_status = gss_inquire_cred(&minor_status, GSS_C_NO_CREDENTIAL, &inquired_name,
&lifetime, &usage, &oid_set);
gss_buffer_desc exported_name_buffer;
major_status = gss_export_name(&minor_status, inquired_name, &exported_name_buffer);
printf("EXPORTED NAME: %s, EXPORTED NAME LENGTH: %d\n",
exported_name_buffer.value, exported_name_buffer.length);
for clarity I decided not to include checks, but I also take care to make sure that major_status is always == GSS_S_COMPLETE
Appreciate any ideas
Unfortunately the buffer output by gss_export_name is an ASN.1 data structure not a human-readable string. Se section 3.2 of RFC 2743. You'd need to skip over the header of that structure and then parse the name in a mechanism-dependent manner.
Some of the GSS-API developers strongly recommend doing this. As an example, the gss-api patches to Openssh do this for parsing Kerberos names. This is the theoretically correct approach. In practice though, using gss_display_name and handling the output of that call produces more portable results in practice, even though it may produce strange results in a multi-mechanism application. You'll get significant arguments over how to handle this in the GSS-API community. Everyone will agree that you should use gss_display_name for producing output for debugging and logs. The question is what should you do if you want a name for searching on an access control list. If you can directly use the output of gss_export_name and do binary comparisons, do that. However if you need to compare against input entered by a human, I'd argue that using the output of gss_display_name is better, while others will argue that parsing the gss_export_name output is better.

Hard wrap string literals at print margin in Eclipse C/C++

C/C++ Eclipse can automatically format and wrap just about any kind of code and behaviour is very configurable, except for string literals. Here is a made up example where debug output message happens to be longer than what can fit within a printable area:
if (some_kind_of_action() == TOUGH_LUCK) {
system_debug_print("Task name error: some_kind_of_action() failed due to your sloppy design.");
}
Using 79 character print margin the desirable result could be:
if (some_kind_of_action() == TOUGH_LUCK) {
system_debug_print("Task name error: some_kind_of_action() failed due to yo"
"ur sloppy design.");
}
You can do this manually by typing your string literal, then placing cursor at the desirable wrap point and pressing Enter key. Eclipse will automatically add necessary quotation marks. This is all nice, until something in your code changes and you have to manually redo the wrapping. I don't see why wrapping at print margin can't be done fully automatically like any other piece of code.
Is there any way to automate hard wrapping of string literals at print margin in Eclipse for C/C++?
Eclipse does not support this feature in any of its editors (even though it was requested nine years ago). However you may be able to avoid breaking your lines manually by using the following plugin for enabling soft wrap.
http://ahtik.com/blog/projects/eclipse-word-wrap/

Format of structs using emacs c code auto-newline

I have recently started using emacs for editing C source, and have been using the auto-newline feature of cc-mode (c-toggle-auto-newline). This works well for constructs like functions and if/else statements, but seems to act strangely when a closing brace should be followed by a semi-colon.
Using auto-newline in GNU Emacs 23.3 I get:
struct foo
{
int x;
}
;
char int[2] =
{
0, 1
}
;
I would like to instead get:
struct foo {
int x;
};
char int[2] = { 0, 1 };
How can I get the closing semi-colon to remain on the same line as the closing brace?
I don't think you can go around this problem with auto newline on. It's not a greatly thought-through feature, it simply inserts newlines after certain characters (;, {, etc.). But seriously, how hard is it to press and enter key? Any automation is always error-prone.
You can customize the "cleanup" behavior when auto-newline is turned on. This is controlled by the contents of the c-cleanup-list variable. (View help for this within Emacs by entering C-h v c-cleanup-list.
Specifically, adding defun-close-semi to c-cleanup-list will solve your problem.
If you're already defining a custom style within your ~/.emacs file, then you can probably figure out how to do this. Otherwise, the easiest way to change this setting is via Customize. In the help buffer (displayed when you ran C-h v c-cleanup-list), the last line will have a link to customize this variable.

Reading php.ini using zend for PHP extension (not PHP language)

I am trying to read some settings from php.ini using zend. The API that I am using is
long zend_ini_long(char *name, uint name_length, int orig)
But it always returns 0. I have double checked the name and also made sure that the value I am specifying in php.ini is greater then 0. Is there anything I am missing?
long maxwait = zend_ini_long("max_execution_time",
sizeof("max_execution_time"), 0);
The problem is that ZEND_STRL is not returning the right length for the way that this API is intended to be used, so don't use it.
I should add that most of the hash tables maintained internally by PHP assume that the NUL terminator character is included in the length of the string being hashed (its part of the overall binary safety concept), which is why we use sizeof() rather than strlen() or sizeof()-1.
Do you need to read php.ini file? Maybe the information is available with phpinfo()?
But if you must are the "www user" allowed to read the file at all? If you change permissions does it still return 0?
You can use the standard php function: ini_get('var-name');
Example:
ini_get('include_path');

Resources