still will not work due to red comments in code
Code:
#include <string.h>
#include <stdio.h>
int int_or_ch(const char *str, unsigned int *result)
{
return sscanf(str, "%u", result) == 1;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
puts("example usage: prog 255x255");
return 0;
}
int len1 = strlen(argv[1]);
// argv is modifiable. as much as i would prefer to modify a copy,
// we'll do it on the original.
char *tokenAfterX;
char *tokenBeforeX = strtok_r(argv[1], "x", &tokenAfterX);
if (tokenAfterX == NULL)
{
puts("Error on input. x expected.");
puts("example usage: prog 255x255");
return 0;
}
unsigned int width, height;
int len2 = strlen(tokenBeforeX);
int len3 = strlen(tokenAfterX);
// remove 1 for x
if ( len1 - 1 != (len2+len3))
printf("Not equal %d : %d\n", len1 - 1, (len2+len3) );
// this does not work because it returns that same as my FindX does
// using two tok holders
// the only thing elimnatied was calling it twice
// it still returns whatever is on either side of x
// then gets sent to be chaned into a int,
// the only difference is that it will not go past
// the letter, then use what ever digit is before
// the letter
//
// it does not prevent the using of the badly
// formated data as a whole, ie 3f4.
// I'm assuming width is first
if (!int_or_ch(tokenBeforeX, &width) || !int_or_ch(tokenAfterX, &height))
{
puts("Error in input. Expected integers.");
return 0;
}
printf("Output width: %u by height: %u\n", width, height);
return 0;
}
/*
int int_or_ch(const char *str, unsigned int *result)
{
return sscanf(str, "%u", result) == 1;
}
int findX(char *c, char *returned )
{
char *tokenAfterX;
char *tokenBeforeX = strtok_r(c, "x", &tokenAfterX);
if (tokenAfterX == NULL)
{
puts("Error on input. x expected.");
puts("example usage: prog 255x255");
return 0;
}
returned = tokenAfterX;
printf(" tokenBeforeX %s tokenAfterX %s\n", tokenBeforeX, tokenAfterX);
return 0;
}
int main(int argc, char *argv[])
{
char *what_did_I_get = "\0";
if (argc < 2)
{
puts("example usage: prog 255x255");
return 0;
}
// argv is modifiable. as much as i would prefer to modify a copy,
// we'll do it on the original.
findX(argv[1], what_did_I_get);
printf("what_did_I_get %s\n", what_did_I_get );
unsigned int width, height;
// I'm assuming width is first
if (!int_or_ch(tokenBeforeX, &width) || !int_or_ch(tokenAfterX, &height))
{
puts("Error in input. Expected integers.");
return 0;
}
printf("Output width: %u by height: %u\n", width, height);
return 0;
}
**/