LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2012, 11:51 AM   #1
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Rep: Reputation: Disabled
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;
}
 
Old 08-18-2012, 12:04 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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).
 
Old 08-18-2012, 01:05 PM   #3
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-18-2012, 01:26 PM   #4
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-18-2012, 01:56 PM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I'd say enum is what you want:
http://en.cppreference.com/w/cpp/language/enum
 
Old 08-18-2012, 02:17 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 08-18-2012, 02:29 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mreff555 View Post
I'm fairly new to c++ or programming in general.
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 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing data in array in FORTRAN vjramana Programming 4 10-14-2011 04:29 PM
adding different data types khodeir Programming 6 06-10-2011 05:29 PM
PHP: Problems comparing data from a DB with data from http request eantoranz Programming 3 08-14-2008 07:55 PM
LXer: On data models, data types and dangerous liaisons LXer Syndicated Linux News 0 07-22-2006 10:33 PM
Prolog data types..(?) sachitha Programming 2 04-08-2006 12:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration