I have a simple utility expecting three command-line arguments:
./myutil -k 123 45
argv[1] is a text flag of some sort: memcmp() against allowed flags; works!
argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.
For now I'm using:
strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
atoi() to get assumed +ve number into unsigned thisuint, thatuint
Then checking thisuint >= 1, thatuint >=1
It works for the test cases I've tried, but it seems mmmmmm clumsy.
Is there a better way?
As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.
Chris
5 17243 Luuk 1,047
Recognized Expert Top Contributor
Do you really need 1000 lines of code to do this validation in C? I do not have enough experience with C, but that seems too much. Maybe you can post the code that you wrote to get tips on how to improve the code.
@Luuk
I admit to being crazy, but I'm not quite crazy enough to devote 1000 lines of code to input validation for an integer!
Was just looking for a neat way that didn't involve a huge (1000 lines...) library designed to handle every nit-picky facet of argv[] parsing.
Since posting, I remembered abs() ...doh!
Input validation is now simple: - unsigned this = abs(atoi(argv[2]));
-
unsigned that = abs(atoi(argv[3]));
-
if (this == 0 || that == 0){
-
puts("\n USAGE both this & that must be > 0\n exit...");
-
exit(EXIT_FAILURE);
-
}
Chris
Luuk 1,047
Recognized Expert Top Contributor
When entering `-123456789012345 67890` as parameter, te value `-2147483648` is returned... 😢😉
dev7060 644
Recognized Expert Contributor
I have a simple utility expecting three command-line arguments:
./myutil -k 123 45
argv[1] is a text flag of some sort: memcmp() against allowed flags; works!
argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.
For now I'm using:
strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
atoi() to get assumed +ve number into unsigned thisuint, thatuint
Then checking thisuint >= 1, thatuint >=1
It works for the test cases I've tried, but it seems mmmmmm clumsy.
Is there a better way?
As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.
What specific validations are you looking for and why? How have you tested user input before? Who is the target user? There could be many things that may not go right. Using the primitive way is probably reasonable according to the use case like dealing with simple use cases (very few known flags, assumption that valid args are present in the correct order, format, sequence, type, range, count, etc). If it gets more complex than that or if the aim is to have a robust code and add flexibility and make sure everything is handled the right way, using a library would probably be a better choice. Having a well-tested library to take care of that stuff may allow to better focus on the mainstream part of the code.
From the provided context, I guess a few nested if-else blocks may be enough. Maybe show the complete validation part of the code for optimization.
...sorry for radio-silence: some stuff came up in the real-world.
Turned out that I was the only user for the C-version of the utility!
A friend asked for something to calculate the product of the first N prime-numbers after some number X.
...run-times of a few micro-seconds did not overcome his dislike of the command-line.
Once the algorithm was ironed out in C it got translated into VBA! He's happier with that.
@Luuk: thanks for the fun overflow illustration. When I have a moment I'll check what happens when an unsigned int overflows.
@dev7060: yes, for anything more serious I will try to find a decent library. It hasn't come up yet: my teach-myself-C exercise is a rework of something I did in Perl a year or two ago (too slow AND a lousy algorithm). C-version runs with hard-coded parameters for now. Three more modules to complete ...I hope by 27 December to mark the 1st anniversary of trying to learn C.
Chris
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Johannes Nix |Johannes.Nix |
last post by:
Hi,
I have a tricky problem with Numeric. Some time ago, I have generated
a huge and complex data structure, and stored it using the cPickle
module. Now I want to evaluate it quickly again on a workstation
cluster with 64-Bit Opteron CPUs - I have no more than three days to
do this. Compiling Python and running Numeric has been no problem at
all. However, I get an error message when accessing the data pickled
before. (I can load it...
|
by: Mark |
last post by:
Hi,
Im trying to validate a form, all the validating works apart from one field.
This particular field must consist of the first 2 characters as letters, &
the following 5 as numbers. And if it dosent meet these requirments an error
message will be displayed. I have pasted the code (and highlighted the
relevant parts) below in the hope that someone can help me out with this.
Ive been trying to suss it out all week & it's driving me nuts!...
|
by: Eric |
last post by:
Is there a way to cancel the validating event on the
closing event?
I have 2 textboxes that I use the validating event to
check for numeric data. If I try to close the form
without putting a number in, it calls the validate event
and pops up my MsgBox.
Thanks!
|
by: Keith |
last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control
I searched and searched - and found other people's code - but they don't work perfectly. Some do only allow numeric - but you cannot backspace. Maybe that is "good enough" - but surely it is not that difficult to have a textbox that only accepts numeric values and backspaces.
|
by: patang |
last post by:
I have written the following code which allows user to enter only numbers and
only one decimal point. This works fine.
However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User should
only be allowed to enter two digits after decimal point.
I know I can validate it in the validate event, however, I was wondering if
some can modify the following code so...
| |
by: Wayne Phipps |
last post by:
Hi There,
I want to be able to validate data entered by a user onto a form.
Different fields will have different validation requirements.
As an example, a text box may only allow numeric, alpha or alpha numeric
entry and/or may have limitations on length or value.
I thought about inheriting a control from one of the standard forms controls
|
by: Reny |
last post by:
can any one tell how can i restrict my user to type just numeric character
in the textbox.I am using VS.NET 2003 (VB.NET)
|
by: TheSteph |
last post by:
Hi Experts !
I have a Winform Program in C# / .NET 2.0
I would like to ensure that a value in a TextBox is a valid Int32 when user
get out of it (TextBox loose focus)
|
by: Richard |
last post by:
I'm validating a date and time string which must be EXACTLY of the format
yy-mm-dd hh:mm:ss
and extracting the six numeric values using sscanf.
I'm using the format string "%2u-%2u-%2u %2u:%2u:%2u" which works, but it
allows each numeric field to be either 1 or 2 digits in length. Also the man
page for sscanf says that preceeding white space before a numeric is
ignored.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |