How can I get the Date modified from a file or stream using DXL? - ibm-doors

I have been searching through basically all of google and the DXL reference documentation to find a function where I can get a Date object from a filename or stream object but I was unable to find anything.

That would be a Stat
Declaration
Stat create(Stream s)
Stat create(string filename)
Operation
Returns a status handle for the stream or file name, which is used in the other Stat functions
bool regular (Stat)
Date modified (Stat)
bool symbolic (Stat)
void delete (Stat)
Date changed (Stat)
string user (Stat)
int mode (Stat)
bool directory (Stat)
int size (Stat)
Date accessed (Stat)

Related

Using ClangTool to parse a string

The following code successfully sets up a ClangTool for parsing:
std::vector<std::string> source_files;
cl::OptionCategory option_category("options");
CommonOptionsParser options_parser(argc, argv, option_category);
ClangTool tool(options_parser.getCompilations(), source_files);
However, I created argc and argv on my own -- they didn't come from main(). Instead of an input file, is it possible to get the ClangTool to parse the contents of a string? E.g.,
const char *str = "extern int foo;\n";
mapVirtualFile() looked like what I wanted, but when I tried, foo didn't come out of it.
tool.mapVirtualFile(StringRef("faux_file.c"), StringRef(str));
// Then proceed to tool.run(...), etc.
I could create a temporary file in /tmp, but I'd like to simply read from the string.
Update:
Creating a temporary file works. It's kind of hack-ish, but it's stable and consistent. It would be nice to have a way of inputting from a string, but it doesn't seem possible with the existing implementation.

Azure WebJobs QueueTrigger attempts (and fails) to convert message's byte[] body to string

I have a storage queue to which I post messages constructed using the CloudQueueMessage(byte[]) constructor. I then tried to process the messages in a webjob function with the following signature:
public static void ConsolidateDomainAuditItem([QueueTrigger("foo")] CloudQueueMessage msg)
I get a consistent failure with exception
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Program.ConsolidateDomainAuditItem ---> System.InvalidOperationException: Exception binding parameter 'msg' ---> System.Text.DecoderFallbackException: Unable to translate bytes [FF] at index -1 from specified code page to Unicode.
at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes)
at System.Text.UTF8Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDecoder)
at System.String.CreateStringFromEncoding(Byte* bytes, Int32 byteLength, Encoding encoding)
at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
at Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage.get_AsString()
at Microsoft.Azure.WebJobs.Host.Storage.Queue.StorageQueueMessage.get_AsString()
at Microsoft.Azure.WebJobs.Host.Queues.Triggers.UserTypeArgumentBindingProvider.UserTypeArgumentBinding.BindAsync(IStorageQueueMessage value, ValueBindingContext context)
at Microsoft.Azure.WebJobs.Host.Queues.Triggers.QueueTriggerBinding.<BindAsync>d__0.MoveNext()
Looking at the code of UserTypeArgumentBindingProvider.BindAsync, it clearly expects to be passed a message whose body is a JSON object. And the UserType... of the name also implies that it expects to bind a POCO.
Yet the MSDN article How to use Azure queue storage with the WebJobs SDK clearly states that
You can use QueueTrigger with the following types:
string
A POCO type serialized as JSON
byte[]
CloudQueueMessage
So why is it not binding to my message?
The WebJobs SDK parameter binding relies heavily on magic parameter names. Although [QueueTrigger(...)] string seems to permit any parameter name (and the MSDN article includes as examples logMessage, inputText, queueMessage, blobName), [QueueTrigger(...)] CloudQueueMessage requires that the parameter be named message. Changing the name of the parameter from msg to message fixes the binding.
Unfortunately, I'm not aware of any documentation which states this explicitly.
Try this instead:
public static void ConsolidateDomainAuditItem([QueueTrigger("foo")] byte[] message)
CloudQueueMessage is a wrapper, usually the bindings get rid of the wrapper and allow you to deal with the content instead.

How does redirection in javac work?

I am currently new with javaC. I have installed JDK and set the path to make it work. I have already done several test programs and they worked.
Let's say I have a java file called Read.java and a text file called Numbers.txt
I have already set my directory to where the files are and I enter to command
javac Read.java
then
java Read < input.txt
Problem is how I can set Read.java program to receive the input.txt file?
I know you can read the file from the program itself without redirection. But I want to learn how you can read a file using redirection.
Java's main method looks something like:
public static void main(String[] args)
{
// method body
}
args is an array of parameters that the user can pass to the program - the first parameter would be args[0], the second args[1] and so on.
To receive the input text file, you can have the user type java Read input.txt. input.txt will be the first parameter, and so you can access it by using args[0] in your main method.
A simple example of command line arguments:
public static void main(String[] args)
{
String input = args[0];
System.out.println("You entered: " + input);
}
You can run this by typing java ProgramName hello, and the output will be You entered hello.
You need to read from standard input:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IORedirection {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = in.readLine()) != null){
System.out.println(line);
}
}
}
> echo "hello stdin" | java IORedirection
> hello stdin
how I can set Read.java program to receive the input.txt file? I know you can read the file from the program itself without redirection. But I want to learn how you can read a file using redirection.
There are several ways to get input to your program.
This isn't about "Java", but rather what are the ways for the caller to write data to "standard input" (or "stdin"). Within any Java program, you can read stdin with System.in.
So, use System.in within your program, and then use a pipe (|) or a redirect (<). Below are two working examples from an answer I posted on a related question:
% cat input.txt | java SystemInExample.java
% java SystemInExample.java < input.txt

How to run AwReporting in Eclipse?

I am getting Error
ERROR|com.google.api.ads.adwords.awreporting.AwReporting|AwReporting] Missing required option: 'file'
When I try to run AwReporting.java
You have to provide some information as command line argument to the program for it to run (-file, -startDate, -endDate).
Solution 1:
Right click the inside the AwReporting.java in eclipe, go to Run
As->Run Configuration.
Click Arguments tab and add the following in the Program Arguments box
-file adword property file location -startDate start date -endDate end date
Example:
-file src/main/resources/aw-report-sample.properties -startDate 20160126 -endDate 20160127
Solution 2:
You can replace the args variable by the below code in the first line on the main() function on AwReporting.java.
args = new String[] {"-file","<adword property file location>","-startDate","<YYYYMMDD>","-endDate","<YYYYMMDD>"};
Example:
public static void main(String args[])
{
args = new String[] {"-file","src/main/resources/aw-report-sample.properties","-startDate","20160126","-endDate","20160127"};

System.IO.Stream in favor of HttpPostedFileBase

I have a site where I allow members to upload photos. In the MVC Controller I take the FormCollection as the parameter to the Action. I then read the first file as type HttpPostedFileBase. I use this to generate thumbnails. This all works fine.
In addition to allowing members to upload their own photos, I would like to use the System.Net.WebClient to import photos myself.
I am trying to generalize the method that processes the uploaded photo (file) so that it can take a general Stream object instead of the specific HttpPostedFileBase.
I am trying to base everything off of Stream since the HttpPostedFileBase has an InputStream property that contains the stream of the file and the WebClient has an OpenRead method that returns Stream.
However, by going with Stream over HttpPostedFileBase, it looks like I am loosing ContentType and ContentLength properties which I use for validating the file.
Not having worked with binary stream before, is there a way to get the ContentType and ContentLength from a Stream? Or is there a way to create a HttpPostedFileBase object using the Stream?
You're right to look at it from a raw stream perspective because then you can create one method that handles streams and therefore many scenarios from which they come.
In the file upload scenario, the stream you're acquiring is on a separate property from the content-type. Sometimes magic numbers (also a great source here) can be used to detect the data type by the stream header bytes but this might be overkill since the data is already available to you through other means (i.e. the Content-Type header, or the .ext file extension, etc).
You can measure the byte length of the stream just by virtue of reading it so you don't really need the Content-Length header: the browser just finds it useful to know what size of file to expect in advance.
If your WebClient is accessing a resource URI on the Internet, it will know the file extension like http://www.example.com/image.gif and that can be a good file type identifier.
Since the file info is already available to you, why not open up one more argument on your custom processing method to accept a content type string identifier like:
public static class Custom {
// Works with a stream from any source and a content type string indentifier.
static public void SavePicture(Stream inStream, string contentIdentifer) {
// Parse and recognize contentIdentifer to know the kind of file.
// Read the bytes of the file in the stream (while counting them).
// Write the bytes to wherever the destination is (e.g. disk)
// Example:
long totalBytesSeen = 0L;
byte[] bytes = new byte[1024]; //1K buffer to store bytes.
// Read one chunk of bytes at a time.
do
{
int num = inStream.Read(bytes, 0, 1024); // read up to 1024 bytes
// No bytes read means end of file.
if (num == 0)
break; // good bye
totalBytesSeen += num; //Actual length is accumulating.
/* Can check for "magic number" here, while reading this stream
* in the case the file extension or content-type cannot be trusted.
*/
/* Write logic here to write the byte buffer to
* disk or do what you want with them.
*/
} while (true);
}
}
Some useful filename parsing features are in the IO namespace:
using System.IO;
Use your custom method in the scenarios you mentioned like so:
From an HttpPostedFileBase instance named myPostedFile
Custom.SavePicture(myPostedFile.InputStream, myPostedFile.ContentType);
When using a WebClient instance named webClient1:
var imageFilename = "pic.gif";
var stream = webClient1.DownloadFile("http://www.example.com/images/", imageFilename)
//...
Custom.SavePicture(stream, Path.GetExtension(imageFilename));
Or even when processing a file from disk:
Custom.SavePicture(File.Open(pathToFile), Path.GetExtension(pathToFile));
Call the same custom method for any stream with a content identifer that you can parse and recognize.

Resources