The "IT DOESN'T WORK!" thread

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Yakk
Poster with most posts but no title.
Posts: 11034
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Fri Oct 23, 2015 9:28 pm UTC

Looks like your definition in your cpp file does not match the declaration in the header file, or somesuch.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
applepi
Posts: 19
Joined: Sat Oct 17, 2015 2:30 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby applepi » Fri Oct 23, 2015 9:43 pm UTC

That's what I was thinking, but they seemed to match up?
(the function in the cpp file)

Code: Select all

vector<string> fileRead(fstream &myFile) // Reads lines from a file into a string vector, then erases empty
{                                                       //  strings from the string vector.
    vector<string> fileText;
    if (myFile.is_open())
    {
        for (int i = 0; !myFile.eof(); i++)
        {
            // We wouldn't want memory errors now, would we?
            resizer(fileText, 1);
           
            getline(myFile, fileText[i]); // Reading lines from the file into the vector
        }
       
        //myFile.close(); // It's job here is done.
       
        for (int ii = 0; ii < fileText.size(); ii++) // Looping through the vector...
        {
            if (fileText[ii].empty()) // Checking if there are any empty strings so we can...
            {
                fileText.erase(fileText.begin()+ii); // Erase them!
            }
        }
    }
    else
    {
        // Error code 002
    }
    return fileText;
}

(the definition in the header file)

Code: Select all

vector<string> fileRead(fstream &myFile);

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Sat Oct 24, 2015 6:51 am UTC

applepi wrote: "fileRead(std::__1::basic_fstream<char, std::__1::char_traits<char> >&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&)"


applepi wrote:(the definition in the header file)

Code: Select all

vector<string> fileRead(fstream &myFile);

That's not the right overload of fileRead. Here it is cleaned up:

Start with the version from the error:

Code: Select all

fileRead(std::__1::basic_fstream<char, std::__1::char_traits<char> >&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&)


Just delete __1::, which is a versioning weirdness; all of those names you'd use from std::

Code: Select all

fileRead(std::basic_fstream<char, std::char_traits<char> >&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)


Now, std::string is a typedef for std::basic_string<char, std::char_traits<char>, std::allocator<char> >, so replace that with std::string both times it appears:

Code: Select all

fileRead(std::basic_fstream<char, std::char_traits<char> >&, std::vector<std::string, std::allocator<std::string> >&)


The second parameter of vector there is default, so you probably didn't specify it; just remove it.

Code: Select all

fileRead(std::basic_fstream<char, std::char_traits<char> >&, std::vector<std::string>&)


And finally, std::fstream is a typedef for the type of the first parameter, so we get

Code: Select all

fileRead(std::fstream&, std::vector<std::string>&)


Do you have that function hanging around undefined somewhere?

User avatar
applepi
Posts: 19
Joined: Sat Oct 17, 2015 2:30 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby applepi » Sat Oct 24, 2015 1:11 pm UTC

Thank you for clearing all of that up, it's pretty overwhelming to try to read it in it's original format. :lol:

I just searched my project, but the weird thing is I didn't find it? The only function that takes both fstream and a vector in as parameters is

Code: Select all

void fileWrite(ofstream &myFile, vector<string> &fileText);
, but it's defined in both the cpp and header file (and it's a different function altogether).

User avatar
Jplus
Posts: 1686
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Jplus » Sun Oct 25, 2015 7:48 am UTC

Minor terminology nitpick: the thing in the cpp file is the definition. The smaller thing in the header file is the (forward) declaration.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
applepi
Posts: 19
Joined: Sat Oct 17, 2015 2:30 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby applepi » Sun Oct 25, 2015 6:13 pm UTC

Oh yeah, ty for the correction! :lol:

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Sun Oct 25, 2015 7:13 pm UTC

Jplus wrote:Minor terminology nitpick: the thing in the cpp file is the definition. The smaller thing in the header file is the (forward) declaration.

Wow, I've known this one backwards for a decade.
It makes more sense this way, though. :P

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Sun Oct 25, 2015 11:06 pm UTC

applepi wrote:Thank you for clearing all of that up, it's pretty overwhelming to try to read it in it's original format. :lol:

I just searched my project, but the weird thing is I didn't find it? The only function that takes both fstream and a vector in as parameters is

Code: Select all

void fileWrite(ofstream &myFile, vector<string> &fileText);
, but it's defined in both the cpp and header file (and it's a different function altogether).

Sorry, I meant to say something in answer here. I am not sure we can diagnose anything unless you were to post your full code, unfortunately.

User avatar
applepi
Posts: 19
Joined: Sat Oct 17, 2015 2:30 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby applepi » Mon Oct 26, 2015 4:22 pm UTC

I kinda figure it'd boil down to that. I'd share, but there's a whole lot of code, and I don't have a GitHub :P Thank you all for the help so far though! I've switched to using cereal anyways, and it's fantastic! The only issue that I have is for some reason, JSONOutputArchive leaves out a curly brace at the end, causing this error

Code: Select all

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
. I tried using

Code: Select all

archive.finishNode();
, but that created more errors for some reason. For now, as a super temporary fix until I figure something out, I'm just appending the missing curly brace to the end of file (which is more like a small bandaid than a fix haha).

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Mon Oct 26, 2015 5:11 pm UTC

Heh, I just now replied to your PM, but since you posted the question here too, I'll copy my reply as well for all to see:

I guess that what's going wrong is that the filestream is closed before the json writer is finished flushing its output. (the destructor of the json archive guarantees that it flushes its contents to the stream it's attached to, the destructor of the file stream guarantees that it flushes its buffer to the file system, but nothing guarantees that the file destructor is called after the archive destructor)
To guarantee this order, put all the code except for the file stream in a deeper block and forget about that finishNode.

Code: Select all

int main {
   ofstream outfile("myfile");
   {
      JSONOutputArchive oarchive(outfile);

      //do stuff
   }
   return 0;
}


I hope that is all that's missing, although only a few examples on cereal's site have it this way.

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Mon Oct 26, 2015 6:47 pm UTC

Flumble wrote:Heh, I just now replied to your PM, but since you posted the question here too, I'll copy my reply as well for all to see:

I guess that what's going wrong is that the filestream is closed before the json writer is finished flushing its output. (the destructor of the json archive guarantees that it flushes its contents to the stream it's attached to, the destructor of the file stream guarantees that it flushes its buffer to the file system, but nothing guarantees that the file destructor is called after the archive destructor)
What?

I didn't see the original code, but that block should not be necessary; destructors for objects on the stack run in reverse order of construction. I'd be very surprised if that's the problem.

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Mon Oct 26, 2015 8:15 pm UTC

EvanED wrote:destructors for objects on the stack run in reverse order of construction. I'd be very surprised if that's the problem.

Oh, I didn't know there's a proper destructor order; in that case, I have no idea what can cause the archive something along the chain to not flush properly.

User avatar
Diadem
Posts: 5609
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Wed Nov 18, 2015 3:01 pm UTC

Ok so I'm having this problem in python that you'd think would be trivial. It's trivial in c, and python is supposed to be simpler. But I can't figure it out even with google.

I have two python files. I want to call the second python file from the first. To be more precise, I want to call a function ('configure(data)' within the second file ('config.py'). If both these files are in the same directory it's simple.

Code: Select all

import config
config.configure(data)

However these files aren't in the same directory. It took me a while to figure out how to do this (apparently you can't use paths in an import statement. That would be too logical or simple).

Code: Select all

import sys
sys.path.insert(0, 'path/to/second/file')
import config
config.configure(data)

This seems to work in general. But in my specific case it still doesn't. Because I'm trying to do all this from a function also called 'configure' in a file also called 'config.py'. And python insists on recursively calling my original file, instead of the new one. Even if I do an 'import config as foo'. And despite putting the location of the second file at the front of the python path. And I can't figure out how to solve that. I can use the command line to call the second file, but then I can't pass in any arguments.

The only thing I can think of right now is to copy the second file to some temporary location, call it, then delete. But that's rather horribly ugly.

TLDR: How do I call a function 'x' in file 'y' from a function also called 'x' in a file also called 'y' in a different location.

edit: Just to be clear, changing the file or function names is not really an option, it would break backwards compatibility. If I understand the python manuals and stackexchange correctly, throwing empty __init__ files all over my system might also solve the problem, but this also isn't an option for the same reason. Plus that it would be even uglier than making a temporary copy.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

lalop
Posts: 210
Joined: Mon May 23, 2011 5:29 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby lalop » Wed Nov 18, 2015 4:17 pm UTC

What about symlinking another name to the remote file, then importing from the symlink instead?

User avatar
thoughtfully
Posts: 2243
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby thoughtfully » Wed Nov 18, 2015 6:34 pm UTC

How is your backwards compatibility broken? The easiest, least ugly thing to do is rename one of the files. You could import the renamed file and then assign that to the name "config".

Would this work?

Code: Select all

import fooconfig as config
config.configure()


If your project has a complicated directory structure, you really should be learning to use the package system. It takes a little getting used to, but it really isn't that bad.

You'd think C was crazy insane too, if you were transitioning in the other direction. Be careful about what expectations you carry over. Sometimes they really don't make a lot of sense in the second language. Python is a dramatically different language from C, being interpreted and dynamically typed, among other distinctions.

However..
Spoiler:
Significant whitespace?
FAIL! :evil:
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Thu Nov 19, 2015 1:46 am UTC

Diadem wrote:Ok so I'm having this problem in python that you'd think would be trivial. It's trivial in c, and python is supposed to be simpler. But I can't figure it out even with google.
To be fair, that's because you're rejecting the solutions...

But here are a couple thoughts.

1. If you're using Python 2, try from __future__ import absolute_import. This may or may not work, and that's the default in Python 3 I think, so it definitely won't help if you're already using Python 3.
2. execfile

User avatar
Diadem
Posts: 5609
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Thu Nov 19, 2015 12:20 pm UTC

EvanED wrote:
Diadem wrote:Ok so I'm having this problem in python that you'd think would be trivial. It's trivial in c, and python is supposed to be simpler. But I can't figure it out even with google.
To be fair, that's because you're rejecting the solutions...

I don't think I rejected any solutions. I've only specified a preference for solutions that don't break my system. If no such solutions exist, fine, then I'll have to make the extra effort of updating the rest of the system. But I'd like to explore other options first.

But here are a couple thoughts.

1. If you're using Python 2, try from __future__ import absolute_import. This may or may not work, and that's the default in Python 3 I think, so it definitely won't help if you're already using Python 3.
2. execfile

Thanks for the advice. I'm indeed using python 2, so this might potentially work. But how do I use absolute imports? Even with the might of google behind me I can't figure it out. I get the impression you can only use absolute imports with modules, i.e. if you are able (and willing) to spam your entire system with empty __init__.py files.


thoughtfully wrote:You'd think C was crazy insane too, if you were transitioning in the other direction. Be careful about what expectations you carry over. Sometimes they really don't make a lot of sense in the second language. Python is a dramatically different language from C, being interpreted and dynamically typed, among other distinctions.

I know. And I'm trying really hard to refrain from complaining about something I don't fully understand. However I am absolutely unable to comprehend why python doesn't seem to have an easy way to import one script in another if they are not in the same directory. One of the main uses of python is as a scripting language. "Call this other script now" is such a basic requirement. And it's not like directories are a recent invention.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby EvanED » Thu Nov 19, 2015 3:58 pm UTC

Diadem wrote:Thanks for the advice. I'm indeed using python 2, so this might potentially work. But how do I use absolute imports?
The semantics of import foo changed from Python 2 to Python 3; from __future__ import absolute_import in Python 2 changes the semantics "early." So if you have the __future__ import, you use absolute imports with import foo. (I don't know a way to get absolute imports without the __future__ import, actually and surprisingly-to-me.)

(The old behavior, relative imports, I think looks for foo in the current directory before sys.path. So if you say import foo from a module named foo, it will look in the directory containing the current file, finds itself, and imports that, all without looking at sys.path. That's why I think it might help.)

Even with the might of google behind me I can't figure it out. I get the impression you can only use absolute imports with modules, i.e. if you are able (and willing) to spam your entire system with empty __init__.py files.
All Python files are modules; __init__.py files give you packages.

User avatar
Diadem
Posts: 5609
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Diadem » Fri Nov 20, 2015 10:03 am UTC

EvanED wrote:
Diadem wrote:Thanks for the advice. I'm indeed using python 2, so this might potentially work. But how do I use absolute imports?
The semantics of import foo changed from Python 2 to Python 3; from __future__ import absolute_import in Python 2 changes the semantics "early." So if you have the __future__ import, you use absolute imports with import foo. (I don't know a way to get absolute imports without the __future__ import, actually and surprisingly-to-me.)

(The old behavior, relative imports, I think looks for foo in the current directory before sys.path. So if you say import foo from a module named foo, it will look in the directory containing the current file, finds itself, and imports that, all without looking at sys.path. That's why I think it might help.)

Now I get it. Sadly, it still doesn't work even with absolute_imports.

I ended up changing the duplicate filenames. Took less time then I had feared, though still more than I had planned for this story. Oh well. Thanks everyone for the help.

EvanED wrote:All Python files are modules; __init__.py files give you packages.

Ah. I got the terminology wrong then. Thanks for the correction.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Jplus
Posts: 1686
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: The "IT DOESN'T WORK!" thread

Postby Jplus » Fri Nov 20, 2015 2:34 pm UTC

Diadem wrote:
EvanED wrote:[...]The semantics of import foo changed from Python 2 to Python 3; from __future__ import absolute_import in Python 2 changes the semantics "early." So if you have the __future__ import, you use absolute imports with import foo. (I don't know a way to get absolute imports without the __future__ import, actually and surprisingly-to-me.)

(The old behavior, relative imports, I think looks for foo in the current directory before sys.path. So if you say import foo from a module named foo, it will look in the directory containing the current file, finds itself, and imports that, all without looking at sys.path. That's why I think it might help.)

Now I get it. Sadly, it still doesn't work even with absolute_imports.

Coincidentally, I walked into this recently, too. The catch is that even with absolute imports, the working directory is still searched before sys.path (note the subtle difference with the directory in which the calling script lives). So the difference between absolute and relative imports isn't in the use of the working directory, but in the use of the directory in which your script lives. If you would haved called your script from a different working directory, it would probably have worked.

The rationale behind Python's import system is that you should always have a clear reason why you're importing another module or package. That is, either the other module/package is part of your project, or it is a standard library, or you installed it as an external dependency. In each of those cases the module/package should already be in your sys.path without naming conflicts. A consequence is that it becomes more difficult to import any random module in your filesystem, but this is a conscious design choice. Python has semantic imports rather than mechanical ones. See PEP 328 for further explanation.

Welcome to Python, by the way!
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
phlip
Restorer of Worlds
Posts: 7535
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Fri Dec 18, 2015 6:03 am UTC

... MSSQL pls.

Pls.

Code: Select all

create table tst(id integer not null, data varbinary(max), content_type nvarchar(10), constraint pk_tst primary key (id));
create fulltext catalog tst_catalog;
create fulltext index on tst(data type column content_type) key index pk_tst on tst_catalog;

insert into tst values (1, 0x3C703E66EA74653C2F703E, N'.html'); -- "<p>fête</p>".encode("windows-1252")
insert into tst values (2, 0x3C703E66C3AA74653C2F703E, N'.html'); -- "<p>fête</p>".encode("utf-8")
insert into tst values (3, 0x3C703E6626237845413B74653C2F703E, N'.html'); -- "<p>f&#xEA;te</p>"
insert into tst values (4, 0x3C703E6626233233343B74653C2F703E, N'.html'); -- "<p>f&#234;te</p>"
insert into tst values (5, 0x3C703E662665636972633B74653C2F703E, N'.html'); -- "<p>f&ecirc;te</p>"

Code: Select all

select id from tst where contains(data, N'fête');
+----+
| id |
+----+
|  1 |
|  4 |
|  5 |
+----+

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Fri Dec 18, 2015 11:55 am UTC

The manual wrote:CONTAINS is a predicate ... to perform SQL Server full-text search on full-text indexed columns containing character-based data types.

phlip pls

User avatar
phlip
Restorer of Worlds
Posts: 7535
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Sun Dec 20, 2015 11:09 pm UTC

Manual wrote:A single varbinary(max) or varbinary column can store many types of documents. SQL Server supports any document type for which a filter is installed and available in the operative system. The document type of each document is identified by the file extension of the document. For example, for a .doc file extension, full-text search uses the filter that supports Microsoft Word documents. For a list of available document types, query the sys.fulltext_document_types catalog view.

Flumble pls.


It gets better, though:

Code: Select all

select id from tst where contains(data, N'fête');
+----+
| id |
+----+
|  2 |
+----+
So far what you'd expect, if you already knew UTF-8 was causing encoding issues. But then:

Code: Select all

select id from tst where freetext(data, N'fête');
+----+
| id |
+----+
|  2 |
|  3 |
+----+
How does that even happen???

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Mon Dec 21, 2015 3:18 am UTC

phlip wrote:
Manual wrote:A single varbinary(max) or varbinary column can store many types of documents. SQL Server supports any document type for which a filter is installed and available in the operative system. The document type of each document is identified by the file extension of the document. For example, for a .doc file extension, full-text search uses the filter that supports Microsoft Word documents. For a list of available document types, query the sys.fulltext_document_types catalog view.

Flumble pls.

Hmm, I didn't expect that...
Please bash my head in if I ever so much as think of using MSSQL. varbinary ought to be a simple binary blob, not the monstrosity microsoft turns it into :shock:

User avatar
phlip
Restorer of Worlds
Posts: 7535
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Mon Dec 21, 2015 3:40 am UTC

Flumble wrote:Please bash my head in if I ever so much as think of using MSSQL. varbinary ought to be a simple binary blob, not the monstrosity microsoft turns it into :shock:

You think that's bad, wait until you find out it also has an "xml" data type, and you find code using XPath predicates in WHERE clauses...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Robert'); DROP TABLE *;
Posts: 728
Joined: Mon Sep 08, 2008 6:46 pm UTC
Location: in ur fieldz

Re: The "IT DOESN'T WORK!" thread

Postby Robert'); DROP TABLE *; » Tue Mar 08, 2016 12:30 am UTC

So I decided to write an implementation of md5 in order to practice Rust, and now I have the following, based on the psuedocode on Wikipedia:

Code: Select all

pub fn compute(data : &Vec<u8>) -> [u8;16] {

      use core::ops::Not;      

       let k : [u32; 64] = [
          0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
         0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
         0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
         0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
         0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
         0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
         0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
         0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
         0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
         0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
         0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
         0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
         0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
         0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
         0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
         0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
      ];

      let s : [u32; 64] =
      [
         7, 12, 17, 22, 7, 12, 17, 22, 7, 12,
         17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5,
         9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4,
         11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
         4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21,
         6, 10, 15, 21, 6, 10, 15, 21
      ];
      
      let mut a0 : u32 = 0x67452301;   //a
      let mut b0 : u32 = 0xefcdab89;   //b
      let mut c0 : u32 = 0x98badcfe;   //c
      let mut d0 : u32 = 0x10325476;   //d

      let mut our_data = data.clone();

      our_data.push(0b10000000);
      let mod512 = our_data.len() % 64;
      let padding_length =
         if mod512 > 56
            { 64-mod512+56 }
         else    
            { 56 - mod512 };

      for _ in 0..padding_length {
         our_data.push(0u8);
      }

      let size : u64 = 8*data.len() as u64;
      for i in (0..8).rev() {
         our_data.push((size.wrapping_shr(8*i) % 256) as u8);
      }

      assert!(our_data.len() % 64 == 0);
      println!("Size: {0}", size);

      let our_data = our_data;
      for q in our_data.chunks(16) {
         for c in q {
            print!("{0:3},", c);
         }
         println!("");
      }

      for chunk in our_data.chunks(64) {

         let mut m : [u32; 16] = [0;16];

         for (i, bits) in chunk.chunks(4).enumerate() {
            let a = bits[0] as u32;
            let b = bits[1] as u32;
            let c = bits[2] as u32;
            let d = bits[3] as u32;
            m[i] = (a << 24) + (b << 16) + (c << 8) + d;
         }

         let m = m;

          let mut a = a0;
          let mut b = b0;
          let mut c = c0;
          let mut d = d0;
         let mut f : u32;
         let mut g : u32;
      //main loop:
          for i in 0..64 {
             match i {
                0 ... 15 => {
                   f = (b & c) | (b.not() & d);
                   g = i;
                }
                 16...31 => {
                     f = (d & b) | (d.not() & c);
                     g = (5*i + 1) % 16;
                 }
                 32...47 => {
                     f = b ^ c ^ d;
                     g = (3*i + 5) % 16;
                 }
                 48...63 => {
                     f = c ^ (b | d.not());
                     g = (7*i) % 16;
                 }
                 _ => panic!("Out of bounds")
             }

              let d_temp = d;
              d = c;
              c = b;
              b = b.wrapping_add(leftrotate(a.wrapping_add(f).wrapping_add(k[i as usize]).wrapping_add(m[g as usize]), s[i as usize]));
              a = d_temp;
          }
      //add this chunk's hash to result so far:
          a0 = a0.wrapping_add(a);
          b0 = b0.wrapping_add(b);
          c0 = c0.wrapping_add(c);
          d0 = d0.wrapping_add(d);
      }


      let mut result = [0u8; 16];
      let mut index = 0;
      for v in [a0, b0, c0, d0].iter() {
         let bytes = to_be_bytes(v);
         result[index] = bytes.0;
         result[index+1] = bytes.1;
         result[index+2] = bytes.2;
         result[index+3] = bytes.3;
         index += 4;
      }

      return result;
   }

   fn to_be_bytes(i: &u32) -> (u8, u8, u8, u8) {
      let q1 = (i >> 24u32) % 256;
      let q2 = (i >> 16u32) % 256;
      let q3 = (i >> 8u32)  % 256;
      let q4 = (i >> 0u32)  % 256;
      return (q1 as u8, q2 as u8, q3 as u8, q4 as u8)
   }

   fn leftrotate (x : u32, c: u32) -> u32 {
          return x.rotate_left(c);
   }

...And it doesn't work. (md5::compute([]) == 227B7F48D21283F63BC9BBC15B44EA1A, when it should produce D41D8CD98F00B204E9800998ECF8427E) So now I'm stuck with somehow debugging an algorithm that's explicitly designed to be difficult to follow! Any help would be greatly appreciated. (As would any help about the actual language style, since some parts like the destructuring of integers looks very clunky.)
...And that is how we know the Earth to be banana-shaped.

User avatar
chridd
Has a vermicelli title
Posts: 710
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: The "IT DOESN'T WORK!" thread

Postby chridd » Tue Mar 08, 2016 4:16 am UTC

This is among the search results for 227B7F48D21283F63BC9BBC15B44EA1A. And I notice:
Robert'); DROP TABLE *; wrote:

Code: Select all

         for (i, bits) in chunk.chunks(4).enumerate() {
            let a = bits[0] as u32;
            let b = bits[1] as u32;
            let c = bits[2] as u32;
            let d = bits[3] as u32;
            m[i] = (a << 24) + (b << 16) + (c << 8) + d;
         }
Wikipedia wrote:The MD5 hash is calculated according to this algorithm. All values are in little-endian.
(There may be other places with the same problem; I didn't check the rest of the code.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics? What phonotactics?) · ze or they · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
Robert'); DROP TABLE *;
Posts: 728
Joined: Mon Sep 08, 2008 6:46 pm UTC
Location: in ur fieldz

Re: The "IT DOESN'T WORK!" thread

Postby Robert'); DROP TABLE *; » Tue Mar 08, 2016 7:45 pm UTC

It somehow didn't occur to me that someone else might have encountered the same problem precisely enough that googling the result was useful. (And also that I seem to be persistantly unable to remember which way around endianess goes.) It worked as soon as I corrected that, thanks for the pointer.
...And that is how we know the Earth to be banana-shaped.

Breakfast
Posts: 117
Joined: Tue Jun 16, 2009 7:34 pm UTC
Location: Coming to a table near you

Re: The "IT DOESN'T WORK!" thread

Postby Breakfast » Fri Mar 18, 2016 7:33 pm UTC

I've got a SQL problem that I'm just not sure how to go about yet.

Say there's a table, Email, that has columns: EmailId, PersonId, CreateDate, IsPrimary (and others, but I think these are the pertinent ones).

The application that uses this table was a mess so people might have multiple emails that are marked as primary and I need to clean it up. Essentially, for the people that have multiple primary emails, I have to update all but the most recently created primary email to be not primary.

I'm not exactly a SQL expert and have been having a lot of trouble wrapping my mind around how to solve this problem.

User avatar
thoughtfully
Posts: 2243
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: The "IT DOESN'T WORK!" wool

Postby thoughtfully » Fri Mar 18, 2016 8:00 pm UTC

I'm not a database expert, but the advice I usually see is to not put too much logic into the SQL (unless there's a good reason not to, which usually implies you Know What You're Doing and wouldn't be asking), leave that in your programming language. Just stick to SELECT and UPDATE as far as the SQL goes.

Even better, use an ORM like SQLAlchemy and forget about SQL entirely.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Breakfast
Posts: 117
Joined: Tue Jun 16, 2009 7:34 pm UTC
Location: Coming to a table near you

Re: The "IT DOESN'T WORK!" wool

Postby Breakfast » Fri Mar 18, 2016 8:17 pm UTC

thoughtfully wrote:I'm not a database expert, but the advice I usually see is to not put too much logic into the SQL (unless there's a good reason not to, which usually implies you Know What You're Doing and wouldn't be asking), leave that in your programming language. Just stick to SELECT and UPDATE as far as the SQL goes.

Even better, use an ORM like SQLAlchemy and forget about SQL entirely.


I agree 100% and unfortunately that's not an option this time. Gotta do it in SQL and it sucks. I mean, I could do it with a cursor but meh... That's gross. Might be how I end up doing it though if I can't figure it out by Monday morning.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" wool

Postby headprogrammingczar » Fri Mar 18, 2016 8:48 pm UTC

Breakfast wrote:I've got a SQL problem that I'm just not sure how to go about yet.

Say there's a table, Email, that has columns: EmailId, PersonId, CreateDate, IsPrimary (and others, but I think these are the pertinent ones).

The application that uses this table was a mess so people might have multiple emails that are marked as primary and I need to clean it up. Essentially, for the people that have multiple primary emails, I have to update all but the most recently created primary email to be not primary.

I'm not exactly a SQL expert and have been having a lot of trouble wrapping my mind around how to solve this problem.

If your DBMS supports it, you can use row_number() and nested queries to select all but the most recent email address for each user, then use that in an update. See this for a bit of specifics on how. If you can alter tables after this is all cleaned up as well, consider dropping IsPrimary and make a PrimaryEmailId column in the Person table. It has a different set of edge conditions, but depending on your codebase it might be easier to manage.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

Breakfast
Posts: 117
Joined: Tue Jun 16, 2009 7:34 pm UTC
Location: Coming to a table near you

Re: The "IT DOESN'T WORK!" wool

Postby Breakfast » Fri Mar 18, 2016 9:38 pm UTC

headprogrammingczar wrote:
Breakfast wrote:I've got a SQL problem that I'm just not sure how to go about yet.

Say there's a table, Email, that has columns: EmailId, PersonId, CreateDate, IsPrimary (and others, but I think these are the pertinent ones).

The application that uses this table was a mess so people might have multiple emails that are marked as primary and I need to clean it up. Essentially, for the people that have multiple primary emails, I have to update all but the most recently created primary email to be not primary.

I'm not exactly a SQL expert and have been having a lot of trouble wrapping my mind around how to solve this problem.

If your DBMS supports it, you can use row_number() and nested queries to select all but the most recent email address for each user, then use that in an update. See this for a bit of specifics on how. If you can alter tables after this is all cleaned up as well, consider dropping IsPrimary and make a PrimaryEmailId column in the Person table. It has a different set of edge conditions, but depending on your codebase it might be easier to manage.


Thanks for the row_number() tip. I hadn't considered that yet.

We actually do have a PrimaryEmailId column on the Person table but the PM on this project did some Oracle about ten years ago so she "know things" and is demanding that we remove the column and do it this way. She'll actually check too. I'm a consultant at this place and have been in touch with my company to try and get out of there because this PM won't trust my (or any of the other developers) experience, gives us five-word requirements for very large tasks, and then berates us when thing aren't exactly how she wants them.

So yeah, I'll mull over the row_number() idea and worst comes to worst it'll be cursors.

*edit*
Aside from the mod-madness, this is a little off still as it won't update per-person. It'll just update every email except the first...

Code: Select all

with cte as (
   select *,
      row_number() over (order by Created desc) as rn
   from Email as e
   where e.IsActive = 1
      and e.IsFlaggedImportant = 1
      and e.PersonId in (select e.PersonId
               from Email as e
               where e.IsActive = 1
                  and e.IsFlaggedImportant = 1
               group by e.PersonId
               having count(e.PersonId) > 1))
update cte
set IsFlaggedImportant = 0
where rn > 1

Rysto
Posts: 1458
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Rysto » Thu Mar 02, 2017 5:33 pm UTC

Argh...

I'm trying to track down the source of a nasty bug that takes hours to reproduce, and we're just before the release. Last night I wrote up this logging script and let it run overnight along side the repro script. Today I see that it did repro overnight, and look at the output.

It's completely blank.

There was a stupid error in the script and it never ran at all. Now I have to wait several more hours for the repro. Damn it.

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Thu Mar 02, 2017 8:20 pm UTC

I thought using Visual Studio would solve my C++ problems, but it only adds an extra level of confusion.
E.g., having source and header trees and being able to (re)scan them seems like the obvious way of working with an IDE. And including files from another project should be trivial with a huge IDE, right?
I just want to use SVL in a one-cpp-file DLL. Am I going about this the wrong way? :oops:

Let's see if VS Code handles it well.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5247
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: The "IT DOESN'T WORK!" thread

Postby Thesh » Thu Mar 02, 2017 8:29 pm UTC

Coincidentally, I did something very similar to you in Visual Studio with Eigen (header-only template library) over the weekend, so here's a convenient screenshot of the property page for my project:

vs_project_properties.png


And then you include it like any other non-local library, which in my case is this:

Code: Select all

#include <Eigen/Eigen> //F:/Eigen/Eigen/Eigen
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

User avatar
Flumble
Yes Man
Posts: 1781
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Flumble » Fri Mar 03, 2017 12:19 am UTC

Thanks for pointing me to Eigen (thought it would be more cumbersome to set up than the simple vector library). :D
That was indeed one of the steps I took for SVL too, but unlike Eigen it's not a header-only library. And it seemed to not properly import the headers, because nearly all types were missing.

Anyway, Eigen works. Maybe a bit overkill for just producing a forward and up vector given euler angles, but it works.

Next problem: the built DLL isn't recognized by Mumble. I swear it has the whole /LD flag and ".dll" file extension, but Mumble right out rejects its existence. :?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests