Adding file to specific folder in iOS application - ios

We are making a common library in c++ for database and file reading.
We have decided a common folder structure for placing the files.
For eg. Insert statement files will be in a Lib/Files/Insert/
I will pass root directory of the project to c++ library, the library then will automatically append the path of the file to root directory.
For eg. If I need insert file of Author, I will pass /path/to/root/ to library. The library will append the rest of the path and pick up file like this: /path/to/root/Lib/Files/Insert/Author.txt
I was not able to open the file from c++ code.
Then I tried this to get the path of file using Objective-C:
NSString * authorPath = [[NSBundle mainBundle] pathForResource:#"Author" ofType:#"txt"];
It is showing the following path:
/Users/homam/Library/Application Support/iPhone Simulator/7.0.3/Applications/9C882381-DAA7-464D-AA84-8DB9288AAD71/CoreLibraryDemo.app/Author.txt
Although I have placed it in the folder /Lib/Files/Insert/, it is not included in the above path before Author.txt
So how can I maintain folder structure in iOS and read file from c++ library.

Related

Is there a way to pass non-project .fs files into a command-line argument in an FSharp .exe?

Let's say that I have an exe that has a function called compareTwoThings. I'd like compareTwoThings to be able to take, as arguments, two directories that have identical file names in each: a .json and a .fs. In compareTwoThings, I want to be able to read in each .json (easy-peasy) and each .fs file. The contents of each .fs file will be known.
How can I read in each .fs file and use the values in those .fs files without them being a part of the overall project structure? Can I? I understand that to build a project and have the project "see" into the .fs files, they need to be added to the .fsproj file, but can I not use open on an external file that has a module name?
Example dir structure of the proj:
myProj
|-proj
|-compareTwoThings.fs
|-myProj.fs
|-myProj.fsproj
Thing1
|-Thing1.fs
|-Thing1.json
Thing2
|-Thing2.fs
|-Thing2.json
And ultimately, the CLI statement would be something like
myProj compareTwoThings [dir to Thing1] [dir to Thing2] [output dir]
I feel like I'm overlooking something very simple here.
Edit: I do not believe that this question is related as I'm asking how to open a non-project .fs file.

How do I create a path in code to locate a file in a specific location? Delphi XE6?

What is the procedure or method add a path to a file in the {$I ...} line?
I have a file that I want the compiler to be able to find.
For example, say I have downloaded a file named "abc.inc" that receives a file error as follows:
F1026 File not found: 'abc'
The file in code is something like this:
{$I abc.inc}
My question is how do you write a file path in code to a particular folder in my project?
For example, let's say my file "abc" is located this path: project\comps\jcl\abc.inc
How would I write in code {$I abc.inc} "is located in path comps\jcl\abc.inc ?
I think I know how {$I functions... How to add/include/incorporate "abc.inc" file into my project using code. How do I write a path in code?
The answer can be found in the documentation:
If the filename does not specify a directory path, then, in addition to searching for the file in the same directory as the current module, Delphi searches in the directories specified in the Search path input box on the Delphi Compiler page of the Project > Options dialog box.
This documentation text is vague at best. What actually happens if you specify a relative path, is that the path is taken to be relative to the file which contains the $INCLUDE directive.
So if the file to be included is not in the same directory as the source file that is including it then your options are to:
Specify the absolute path of the include file, or
Specify the path of the include file, relative to the source file containing the $INCLUDE directive, or
Add the directory containing the include file to the search path.

Which zipping library should I use to properly assemble a valid XLSX file in Objective-C?

I am trying to modify an XLSX file programmatically using Objective-C.
So far, I am only modifying the data on one of the sheets. The steps I am taking are as follows:
Copy the XLSX file to Documents folder
Unzip the XLSX container with keeping the directory structure
Parse the corresponding sheet XML file (sheet2.xml in my case)
Add some rows
Rewrite the XML structure and save it
Put the updated XML file back into the XLSX container
However, the new XLSX file becomes corrupt. I am using GDataXML for XML parsing/writing and Objective-Zip for zipping/unzipping.
I know that the XML file I have created is proper, because when I manually unzip and the re-zip the corrupt XLSX file, it opens without any errors. I have done this on both OS X (using Unarchiver) and Windows (using 7-Zip).
The problem is either with the Objective-Zip library, or the way I use it. Below is how I implement the zipping method:
ZipFile *zipFile = [[ZipFile alloc] initWithFileName:XLSXDocumentsFilePath mode:ZipFileModeAppend];
ZipWriteStream *stream= [zipFile writeFileInZipWithName:XLSX_WORKSHEET_XML_SUBPATH compressionLevel:ZipCompressionLevelNone];
[stream writeData:data];
[stream finishedWriting];
[zipFile close];
I also tried the other compressionLevel arguments available with no luck:
ZipCompressionLevelDefault
ZipCompressionLevelBest
ZipCompressionLevelFastest
My questions are:
Which zipping library should I use to create a valid XLSX file programmatically?
If Objective-Zip is suitable, what is wrong with my code?
From an answer to another question, I found out that: "The OOXML format imposes that the only compression method permitted in the package is DEFLATE".
Is it possible to force Objective-Zip to use DEFLATE? Or is there an open-source iOS zipping library that uses DEFLATE?
I found the answer upon doing some research and also having a one to one correspondence with Objective-Zip's developer, flyingdolphinstudio.
First of all, Objective-Zip uses DEFLATE as the default compression method. I also confirmed this with the developer, who told me that using ZipCompressionLevelDefault, ZipCompressionLevelFastest or ZipCompressionLevelBest for the argument compressionLevel: will guarantee a DEFLATE compression.
So, the problem is coming from the mode: argument, which is ZipFileModeAppend in my case. It seems that MiniZip does not have a method to delete the files inside a zip file and that's why I am not overwriting the existing file, but adding a new one. To make it more clear, take a look at how my xl/worksheets folder look like after zipping it using Objective-Zip:
So, the only way to create a valid XLSX container is to create the zip file from scratch, by adding all the files and also keeping the directory/file structure intact.
I hope this experience would help somebody out.

How to Deal with Same named files in supporting files directory

Say for example In Xcode I make 3 folders in my supporting files older named "a" "b" and "c".
In each of these folders I have an xml file name "file.xml".
How can I use NSMainBundle to get paths to these 3 different xml files?
To get these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see #CocoaFu's answer to this SO question.
Then in the code
NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle
From this you look in directories using pathForResource:ofType:inDirectory: e.g.
NSString* path = [bundle pathForResource:#"file.xml"
ofType:nil
inDirectory:#"a"];
The methods are given in NSBundle class reference also see the Bundle Programming guide

Loading an image programmatically in iOS

In my App for the iPad I'm trying to load an image file programmatically. The file is NOT part of my project, hence it is not referenced in XCode. There is no entry for the file in XCode's Groups and Files column.
The image has to be loaded at runtime instead, it name being read from a Property List.
I'm trying to load the file like this:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:#"MyImage" ofType:#"png" inDirectory:#"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
In this case, mydirectory lives in the main bundle like this:
MyAmazingApp.app/MyDirectory/MyImage.png
Unfortunately, the Image will not load. I can't add the image file to my project, as its name is to be determined at runtime and cannot be known in advance. The file name is read from a config file (a Property List) at runtime instead.
What am I doing wrong? Your help will be very much appreciated.
You can use NSFileManager to get the contents of a directory, or several directories. You obviously have to know something about the file you want to load so that you can identify it, but you could for example use NSFileManager to help you generate a list of images in your app bundle and in your app's Documents directory.

Resources