Whats the most elegant way of comparing different data types
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Whats the most elegant way of comparing different data types
I'm fairly new to c++ or programming in general.
I am trying to avoid a bunch of conditional statements and use the most elegant method to solve my problem
I have a function that will currently determines a file extension and stores it in a character array called ext.
What I would like it to do, is output a number symbolic of a list of accepted extensions eg:
0= if null or not supported
1=tif
2=jpg
Is there a way to do this with enum or typedef, or some other good way of doing this.
Also, what is the best way to make it ignore case.
Thanks.
Code:
int validExt(){
int len= strlen(chararg);
int dot=len-1;
for(int i=0;i <=( len-1); i++){
if(chararg[i]=='.') dot=i;
}
int extlen= (len-1) - dot;
char ext[extlen+1];
strcpy (ext,&chararg[dot+1]);
cout<<ext<<endl;
///extnum????
return extnum;
}
i think the most elegent way would be for your program to read/parse the /usr/share/misc/magic file (you would essentially be recreating the file command).
that way if someone mistakenly names something like watch-this-video.mkv.exe.jpg you will be able to determine the real file type by examining the header of the actual file (not just the file name suffix).
Excellent idea, however I need this to work across multiple platforms, including windows 7
In the future I might pursue some method of reading the header but for now I'll settle for something
that is 90% robust.
So is there a way I can evaluate a character array and set up some kind of enumeration to turn char* values into numbers?
If not I can just use conditionals.
Excellent idea, however I need this to work across multiple platforms, including windows 7
In the future I might pursue some method of reading the header but for now I'll settle for something
that is 90% robust.
So is there a way I can evaluate a character array and set up some kind of enumeration to turn char* values into numbers?
If not I can just use conditionals.
in c you can reverse the array and use strncmp to see if the first 3 chars equal "gpj" or "fit" and return the value that corresponds to the non/match.
It would appear that you have previous experience writing code in C. Consider using C++ strings, not char arrays for strings. Also, avoid global variables.
Quote:
Originally Posted by mreff555
I am trying to avoid a bunch of conditional statements and use the most elegant method to solve my problem
I have a function that will currently determines a file extension and stores it in a character array called ext.
What I would like it to do, is output a number symbolic of a list of accepted extensions eg:
0= if null or not supported
1=tif
2=jpg
Is there a way to do this with enum or typedef, or some other good way of doing this.
Also, what is the best way to make it ignore case.
Thanks.
You could consider storing various file types, and an identifying number, within a STL vector.
Here's an example program:
Code:
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctype.h>
class FileUtil
{
public:
FileUtil(const std::string& extDataFile)
{
// These extensions should come from a flat-file; not be hard-coded as shown below.
m_knownExtensions.push_back("jpg");
m_knownExtensions.push_back("cpp");
m_knownExtensions.push_back("c");
m_knownExtensions.push_back("mov");
}
bool knownExt(const std::string& filename)
{
// search for the dot
const size_t dot = filename.rfind('.');
if (dot == std::string::npos)
{
return false;
}
// extract the filename extension
std::string ext = filename.substr(dot + 1);
// convert extension to all lowercase letters
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
// search for extension within our list of known extensions; return true if found.
for (std::vector<std::string>::const_iterator it = m_knownExtensions.begin();
it != m_knownExtensions.end(); ++it)
{
if (ext == *it)
return true;
}
// if here, we could not locate the filename's extension.
return false;
}
void displayKnownExtensions() const
{
for (size_t i = 0; i < m_knownExtensions.size(); ++i)
{
std::cout << i << " -- " << m_knownExtensions[i] << std::endl;
}
}
private:
std::vector<std::string> m_knownExtensions;
};
int main()
{
FileUtil fn("MyExtDataFile.dat"); // see note in constructor
fn.displayKnownExtensions();
std::cout << "JPEG is" << (fn.knownExt("File.jpeg") ? "" : " NOT") << " a known extension."
<< std::endl
<< "JPG is" << (fn.knownExt("File.jpg" ) ? "" : " NOT") << " a known extension."
<< std::endl;
}
Last edited by dwhitney67; 08-18-2012 at 09:45 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.